Skip to content

Commit

Permalink
feat(teach-frontend): 12-群居测试 search 功能
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jul 6, 2023
1 parent 4fb88d7 commit c9eac9c
Showing 1 changed file with 186 additions and 0 deletions.
186 changes: 186 additions & 0 deletions apps/teach-frontend/src/components/command/tests/search.new.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import { createTestingPinia } from '@pinia/testing'
import { useSearch } from '../search'
import { useSearchTasks } from '../searchTasks'
import { useSearchCommands } from '../searchCommands'
import { completeSmartProject, useListProjectsStore, useTasksStore } from '@/store'
import { liveListProject, tasks } from '@/tests/fixture'
import { useCommand } from '@/composables/command'

describe('search new ', () => {
beforeAll(() => {
const { addCommand } = useCommand()

addCommand({
name: '回到主页',
execute() {},
})

addCommand({
name: '切换皮肤',
execute() {},
})
})
beforeEach(() => {
vi.useFakeTimers()

createTestingPinia({
createSpy: vi.fn,
})

const tasksStore = useTasksStore()
vi.mocked(tasksStore.findAllTasksNotRemoved).mockImplementation(async () => tasks)

const listProjectsStore = useListProjectsStore()
vi.mocked(listProjectsStore.findProject).mockImplementation(() => liveListProject)
})

describe('ui state', () => {
it('should be loading is true when search is start', async () => {
const { search, loading } = useSearch()

search.value = '吃饭'

await vi.advanceTimersToNextTimerAsync()

expect(loading.value).toBe(true)
})

it('should be loading is false when search is complete', async () => {
const { search, loading } = useSearch()

search.value = '吃饭'

await vi.runAllTimersAsync()

expect(loading.value).toBe(false)
})

it('should be searching is true when search is complete', async () => {
const { search, searching } = useSearch()

search.value = '吃饭'

await vi.runAllTimersAsync()

expect(searching.value).toBe(true)
})
})

describe('search tasks', () => {
it('should be search a task by title', async () => {
const { search } = useSearch()
const { filteredTasks } = useSearchTasks()

search.value = '吃饭'

await vi.runAllTimersAsync()

expect(filteredTasks.value.length).toBe(1)
const item = filteredTasks.value[0].item
expect(item.title).toBe('吃饭')
expect(item).toHaveProperty('id')
expect(item).toHaveProperty('desc')
expect(item).toHaveProperty('done')
expect(item).toHaveProperty('from')
})

it('should be search a task by desc', async () => {
const { search } = useSearch()
const { filteredTasks } = useSearchTasks()

search.value = '吃什么'
await vi.runAllTimersAsync()

expect(filteredTasks.value.length).toBe(1)
expect(filteredTasks.value[0].item.title).toBe('吃饭')
})

it('should not be found when the task does not exist', async () => {
const { search } = useSearch()
const { filteredTasks } = useSearchTasks()

search.value = '运动'
await vi.runAllTimersAsync()

expect(filteredTasks.value.length).toBe(0)
})

it('should be task\'s project is listProject when status is active', async () => {
const { search } = useSearch()
const { filteredTasks } = useSearchTasks()

search.value = '吃饭'
await vi.runAllTimersAsync()

expect(filteredTasks.value[0].item.done).toBe(false)
expect(filteredTasks.value[0].item.from?.name).toBe('生活')
})
it('should be task\'s project is completeSmartProject when status is complete', async () => {
const { search } = useSearch()
const { filteredTasks } = useSearchTasks()

search.value = '写代码'
await vi.runAllTimersAsync()

expect(filteredTasks.value[0].item.done).toBe(true)
expect(filteredTasks.value[0].item.from?.name).toBe(completeSmartProject.name)
})
})

describe('search commands', () => {
it('normal', async () => {
const { search } = useSearch()
const { filteredCommands } = useSearchCommands()

search.value = '>主页'

await vi.runAllTimersAsync()

expect(filteredCommands.value.length).toBe(1)
expect(filteredCommands.value[0].name).toBe('回到主页')
})

it('removes the trailing white space', async () => {
const { search } = useSearch()
const { filteredCommands } = useSearchCommands()

search.value = '>主页 '

await vi.runAllTimersAsync()

expect(filteredCommands.value.length).toBe(1)
expect(filteredCommands.value[0].name).toBe('回到主页')
})

it('should be search all commands ', async () => {
const { search } = useSearch()
const { filteredCommands } = useSearchCommands()

search.value = '>'

await vi.runAllTimersAsync()
expect(filteredCommands.value.length).toBe(2)
})
})

it('should be reset when search is empty', async () => {
const { search, searching, loading } = useSearch()
const { filteredTasks } = useSearchTasks()
const { filteredCommands } = useSearchCommands()

search.value = '吃饭'
await vi.runAllTimersAsync()

search.value = '>主页'
await vi.runAllTimersAsync()

search.value = ''
await vi.runAllTimersAsync()

expect(searching.value).toBe(false)
expect(loading.value).toBe(false)
expect(filteredTasks.value.length).toBe(0)
expect(filteredCommands.value.length).toBe(2)
})
})

1 comment on commit c9eac9c

@vercel
Copy link

@vercel vercel bot commented on c9eac9c Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.