Skip to content

Commit 29a9f75

Browse files
committed
test: add useVisibleItems tests
1 parent 65fc79f commit 29a9f75

2 files changed

Lines changed: 150 additions & 1 deletion

File tree

app/composables/useVisibleItems.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export function useVisibleItems<T>(items: MaybeRefOrGetter<T[]>, limit: number) {
1+
import { computed, shallowRef, toValue } from 'vue'
2+
import type { MaybeRefOrGetter } from 'vue'
3+
4+
export function useVisibleItems<T>(
5+
items: MaybeRefOrGetter<T[]>,
6+
limit: number,
7+
) {
28
const showAll = shallowRef(false)
39

410
const visibleItems = computed(() => {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { computed, ref } from 'vue'
3+
import { useVisibleItems } from '~/composables/useVisibleItems'
4+
5+
describe('useVisibleItems', () => {
6+
describe('initial state', () => {
7+
it('returns all items when list is within limit', () => {
8+
const { visibleItems, hasMore, hiddenCount } = useVisibleItems(['a', 'b', 'c'], 5)
9+
10+
expect(visibleItems.value).toEqual(['a', 'b', 'c'])
11+
expect(hasMore.value).toBe(false)
12+
expect(hiddenCount.value).toBe(0)
13+
})
14+
15+
it('returns exactly limit items when list exceeds limit', () => {
16+
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
17+
const { visibleItems, hasMore, hiddenCount } = useVisibleItems(items, 5)
18+
19+
expect(visibleItems.value).toEqual([1, 2, 3, 4, 5])
20+
expect(hasMore.value).toBe(true)
21+
expect(hiddenCount.value).toBe(5)
22+
})
23+
24+
it('returns all items when list length equals limit exactly', () => {
25+
const items = [1, 2, 3]
26+
const { visibleItems, hasMore, hiddenCount } = useVisibleItems(items, 3)
27+
28+
expect(visibleItems.value).toEqual([1, 2, 3])
29+
expect(hasMore.value).toBe(false)
30+
expect(hiddenCount.value).toBe(0)
31+
})
32+
33+
it('handles empty list', () => {
34+
const { visibleItems, hasMore, hiddenCount } = useVisibleItems([], 5)
35+
36+
expect(visibleItems.value).toEqual([])
37+
expect(hasMore.value).toBe(false)
38+
expect(hiddenCount.value).toBe(0)
39+
})
40+
})
41+
42+
describe('expand()', () => {
43+
it('shows all items after expand', () => {
44+
const items = [1, 2, 3, 4, 5, 6]
45+
const { visibleItems, hasMore, expand } = useVisibleItems(items, 3)
46+
47+
expect(visibleItems.value).toEqual([1, 2, 3])
48+
49+
expand()
50+
51+
expect(visibleItems.value).toEqual([1, 2, 3, 4, 5, 6])
52+
expect(hasMore.value).toBe(false)
53+
})
54+
})
55+
56+
describe('collapse()', () => {
57+
it('hides items again after collapse', () => {
58+
const items = [1, 2, 3, 4, 5, 6]
59+
const { visibleItems, hasMore, expand, collapse } = useVisibleItems(items, 3)
60+
61+
expect(visibleItems.value).toEqual([1, 2, 3])
62+
63+
expand()
64+
65+
expect(visibleItems.value).toEqual([1, 2, 3, 4, 5, 6])
66+
67+
collapse()
68+
69+
expect(visibleItems.value).toEqual([1, 2, 3])
70+
expect(hasMore.value).toBe(true)
71+
})
72+
})
73+
74+
describe('toggle()', () => {
75+
it('expands on first toggle', () => {
76+
const items = [1, 2, 3, 4, 5]
77+
const { visibleItems, toggle } = useVisibleItems(items, 3)
78+
79+
expect(visibleItems.value).toEqual([1, 2, 3])
80+
81+
toggle()
82+
83+
expect(visibleItems.value).toEqual([1, 2, 3, 4, 5])
84+
})
85+
86+
it('collapses on second toggle', () => {
87+
const items = [1, 2, 3, 4, 5]
88+
const { visibleItems, toggle } = useVisibleItems(items, 3)
89+
90+
expect(visibleItems.value).toEqual([1, 2, 3])
91+
92+
toggle()
93+
94+
expect(visibleItems.value).toEqual([1, 2, 3, 4, 5])
95+
96+
toggle()
97+
98+
expect(visibleItems.value).toEqual([1, 2, 3])
99+
})
100+
})
101+
102+
describe('reactivity', () => {
103+
it('reacts to ref source changes', () => {
104+
const items = ref([1, 2, 3])
105+
const { visibleItems, hasMore, hiddenCount } = useVisibleItems(items, 2)
106+
107+
expect(visibleItems.value).toEqual([1, 2])
108+
expect(hasMore.value).toBe(true)
109+
expect(hiddenCount.value).toBe(1)
110+
111+
items.value = [1, 2]
112+
113+
expect(visibleItems.value).toEqual([1, 2])
114+
expect(hasMore.value).toBe(false)
115+
expect(hiddenCount.value).toBe(0)
116+
})
117+
118+
it('reacts to computed source changes', () => {
119+
const source = ref([1, 2, 3, 4, 5])
120+
const filtered = computed(() => source.value.filter(n => n % 2 === 0))
121+
const { visibleItems, hasMore } = useVisibleItems(filtered, 3)
122+
123+
expect(visibleItems.value).toEqual([2, 4])
124+
expect(hasMore.value).toBe(false)
125+
126+
source.value = [1, 2, 3, 4, 5, 6, 7, 8]
127+
128+
expect(visibleItems.value).toEqual([2, 4, 6])
129+
expect(hasMore.value).toBe(true)
130+
})
131+
132+
it('reacts to getter function source changes', () => {
133+
const count = ref(2)
134+
const { visibleItems } = useVisibleItems(() => Array.from({ length: count.value }, (_, i) => i), 3)
135+
136+
expect(visibleItems.value).toEqual([0, 1])
137+
138+
count.value = 5
139+
140+
expect(visibleItems.value).toEqual([0, 1, 2])
141+
})
142+
})
143+
})

0 commit comments

Comments
 (0)