Skip to content

Commit c9eac9c

Browse files
committed
feat(teach-frontend): 12-群居测试 search 功能
1 parent 4fb88d7 commit c9eac9c

1 file changed

Lines changed: 186 additions & 0 deletions

File tree

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
2+
import { createTestingPinia } from '@pinia/testing'
3+
import { useSearch } from '../search'
4+
import { useSearchTasks } from '../searchTasks'
5+
import { useSearchCommands } from '../searchCommands'
6+
import { completeSmartProject, useListProjectsStore, useTasksStore } from '@/store'
7+
import { liveListProject, tasks } from '@/tests/fixture'
8+
import { useCommand } from '@/composables/command'
9+
10+
describe('search new ', () => {
11+
beforeAll(() => {
12+
const { addCommand } = useCommand()
13+
14+
addCommand({
15+
name: '回到主页',
16+
execute() {},
17+
})
18+
19+
addCommand({
20+
name: '切换皮肤',
21+
execute() {},
22+
})
23+
})
24+
beforeEach(() => {
25+
vi.useFakeTimers()
26+
27+
createTestingPinia({
28+
createSpy: vi.fn,
29+
})
30+
31+
const tasksStore = useTasksStore()
32+
vi.mocked(tasksStore.findAllTasksNotRemoved).mockImplementation(async () => tasks)
33+
34+
const listProjectsStore = useListProjectsStore()
35+
vi.mocked(listProjectsStore.findProject).mockImplementation(() => liveListProject)
36+
})
37+
38+
describe('ui state', () => {
39+
it('should be loading is true when search is start', async () => {
40+
const { search, loading } = useSearch()
41+
42+
search.value = '吃饭'
43+
44+
await vi.advanceTimersToNextTimerAsync()
45+
46+
expect(loading.value).toBe(true)
47+
})
48+
49+
it('should be loading is false when search is complete', async () => {
50+
const { search, loading } = useSearch()
51+
52+
search.value = '吃饭'
53+
54+
await vi.runAllTimersAsync()
55+
56+
expect(loading.value).toBe(false)
57+
})
58+
59+
it('should be searching is true when search is complete', async () => {
60+
const { search, searching } = useSearch()
61+
62+
search.value = '吃饭'
63+
64+
await vi.runAllTimersAsync()
65+
66+
expect(searching.value).toBe(true)
67+
})
68+
})
69+
70+
describe('search tasks', () => {
71+
it('should be search a task by title', async () => {
72+
const { search } = useSearch()
73+
const { filteredTasks } = useSearchTasks()
74+
75+
search.value = '吃饭'
76+
77+
await vi.runAllTimersAsync()
78+
79+
expect(filteredTasks.value.length).toBe(1)
80+
const item = filteredTasks.value[0].item
81+
expect(item.title).toBe('吃饭')
82+
expect(item).toHaveProperty('id')
83+
expect(item).toHaveProperty('desc')
84+
expect(item).toHaveProperty('done')
85+
expect(item).toHaveProperty('from')
86+
})
87+
88+
it('should be search a task by desc', async () => {
89+
const { search } = useSearch()
90+
const { filteredTasks } = useSearchTasks()
91+
92+
search.value = '吃什么'
93+
await vi.runAllTimersAsync()
94+
95+
expect(filteredTasks.value.length).toBe(1)
96+
expect(filteredTasks.value[0].item.title).toBe('吃饭')
97+
})
98+
99+
it('should not be found when the task does not exist', async () => {
100+
const { search } = useSearch()
101+
const { filteredTasks } = useSearchTasks()
102+
103+
search.value = '运动'
104+
await vi.runAllTimersAsync()
105+
106+
expect(filteredTasks.value.length).toBe(0)
107+
})
108+
109+
it('should be task\'s project is listProject when status is active', async () => {
110+
const { search } = useSearch()
111+
const { filteredTasks } = useSearchTasks()
112+
113+
search.value = '吃饭'
114+
await vi.runAllTimersAsync()
115+
116+
expect(filteredTasks.value[0].item.done).toBe(false)
117+
expect(filteredTasks.value[0].item.from?.name).toBe('生活')
118+
})
119+
it('should be task\'s project is completeSmartProject when status is complete', async () => {
120+
const { search } = useSearch()
121+
const { filteredTasks } = useSearchTasks()
122+
123+
search.value = '写代码'
124+
await vi.runAllTimersAsync()
125+
126+
expect(filteredTasks.value[0].item.done).toBe(true)
127+
expect(filteredTasks.value[0].item.from?.name).toBe(completeSmartProject.name)
128+
})
129+
})
130+
131+
describe('search commands', () => {
132+
it('normal', async () => {
133+
const { search } = useSearch()
134+
const { filteredCommands } = useSearchCommands()
135+
136+
search.value = '>主页'
137+
138+
await vi.runAllTimersAsync()
139+
140+
expect(filteredCommands.value.length).toBe(1)
141+
expect(filteredCommands.value[0].name).toBe('回到主页')
142+
})
143+
144+
it('removes the trailing white space', async () => {
145+
const { search } = useSearch()
146+
const { filteredCommands } = useSearchCommands()
147+
148+
search.value = '>主页 '
149+
150+
await vi.runAllTimersAsync()
151+
152+
expect(filteredCommands.value.length).toBe(1)
153+
expect(filteredCommands.value[0].name).toBe('回到主页')
154+
})
155+
156+
it('should be search all commands ', async () => {
157+
const { search } = useSearch()
158+
const { filteredCommands } = useSearchCommands()
159+
160+
search.value = '>'
161+
162+
await vi.runAllTimersAsync()
163+
expect(filteredCommands.value.length).toBe(2)
164+
})
165+
})
166+
167+
it('should be reset when search is empty', async () => {
168+
const { search, searching, loading } = useSearch()
169+
const { filteredTasks } = useSearchTasks()
170+
const { filteredCommands } = useSearchCommands()
171+
172+
search.value = '吃饭'
173+
await vi.runAllTimersAsync()
174+
175+
search.value = '>主页'
176+
await vi.runAllTimersAsync()
177+
178+
search.value = ''
179+
await vi.runAllTimersAsync()
180+
181+
expect(searching.value).toBe(false)
182+
expect(loading.value).toBe(false)
183+
expect(filteredTasks.value.length).toBe(0)
184+
expect(filteredCommands.value.length).toBe(2)
185+
})
186+
})

0 commit comments

Comments
 (0)