Skip to content

Commit

Permalink
refactor: command test case
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jul 6, 2023
1 parent c9eac9c commit 61f2700
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 62 deletions.
1 change: 1 addition & 0 deletions apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@iconify-json/carbon": "^1.1.13",
"@iconify-json/mdi": "^1.1.41",
"@iconify/vue": "^4.0.2",
"@pinia/testing": "^0.1.2",
"@types/lodash-es": "^4.17.6",
"@unocss/reset": "^0.48.3",
"@vitejs/plugin-vue": "^4.0.0",
Expand Down
63 changes: 31 additions & 32 deletions apps/frontend/src/components/command/tests/commandModal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,63 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { computed } from 'vue'
import { useCommandModal } from '../commandModal'
import * as misc from '@/composables/misc'
import { fireEvent, useSetup } from '@/tests/helper'
import * as misc from '@/composables/misc'

describe('CommandModal', () => {
describe('command modal', () => {
beforeEach(() => {
const { closeCommandModal } = useCommandModal()
closeCommandModal()
})

it('should be open command modal', () => {
const { openCommandModal, showCommandModal } = useCommandModal()

openCommandModal()

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

it('should be close command modal', () => {
const { openCommandModal, closeCommandModal, showCommandModal } = useCommandModal()
openCommandModal()
const { closeCommandModal, showCommandModal } = useCommandModal()

closeCommandModal()

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

describe('KeyboardShortcut', () => {
it('should be open command modal when use command + k on Mac', () => {
vi.spyOn(misc, 'useIsMac').mockImplementation(() => computed(() => true))
const { registerKeyboardShortcut, showCommandModal } = useCommandModal()

const { wrapper } = useSetup(() => {
registerKeyboardShortcut()
})

fireEvent.keydown({
key: 'k',
metaKey: true,
})
it('should be open command modal when press cmd+k on Mac', () => {
vi.spyOn(misc, 'useIsMac').mockReturnValue(computed(() => true))
const { registerKeyboardShortcut, showCommandModal } = useCommandModal()

expect(showCommandModal.value).toBe(true)
const { wrapper } = useSetup(() => {
registerKeyboardShortcut()
})

wrapper.unmount()
fireEvent.keyDown({
key: 'k',
metaKey: true,
})
it('should be open command modal when use ctrl + k on Win', () => {
vi.spyOn(misc, 'useIsMac').mockImplementation(() => computed(() => false))
const { registerKeyboardShortcut, showCommandModal } = useCommandModal()

const { wrapper } = useSetup(() => {
registerKeyboardShortcut()
})
expect(showCommandModal.value).toBe(true)

wrapper.unmount()
})

fireEvent.keydown({
key: 'k',
ctrlKey: true,
})
it('should be open command modal when press ctrl+k on Win', () => {
vi.spyOn(misc, 'useIsMac').mockReturnValue(computed(() => false))
const { registerKeyboardShortcut, showCommandModal } = useCommandModal()

expect(showCommandModal.value).toBe(true)
const { wrapper } = useSetup(() => {
registerKeyboardShortcut()
})

wrapper.unmount()
fireEvent.keyDown({
key: 'k',
ctrlKey: true,
})

expect(showCommandModal.value).toBe(true)

wrapper.unmount()
})
})
186 changes: 186 additions & 0 deletions apps/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)
})
})
48 changes: 19 additions & 29 deletions apps/frontend/src/components/command/tests/searchCommands.spec.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,39 @@
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { useSearchCommands } from '../searchCommands'
import { useCommand } from '@/composables/command'

describe('SearchCommands', () => {
describe('search commands', () => {
beforeEach(() => {
const { resetSearchCommands } = useSearchCommands()
resetSearchCommands()
})
beforeAll(() => {
const { addCommand } = useCommand()

const mockCommand = {
name: '前往主页',
addCommand({
name: '回到主页',
execute() {},
}

addCommand(mockCommand)
})
})

afterAll(() => {
const { resetCommand } = useCommand()
resetCommand()
addCommand({
name: '切换皮肤',
execute() {},
})
})

it('Should search for the go to home command', () => {
it('should be search a command', () => {
const { searchCommands, filteredCommands } = useSearchCommands()
searchCommands('前往主页')

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

it('should be reset filtered commands', () => {
const { commands } = useCommand()
const { resetSearchCommands, searchCommands, filteredCommands } = useSearchCommands()

searchCommands('前往主页')
searchCommands('主页')

resetSearchCommands()

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

it('should return all commands when input is empty', () => {
const { commands } = useCommand()
it('should be search all commands ', () => {
const { searchCommands, filteredCommands } = useSearchCommands()

searchCommands('')

expect(filteredCommands.value.length).toBe(commands.length)
expect(filteredCommands.value.length).toBe(2)
})
})
2 changes: 2 additions & 0 deletions apps/frontend/src/tests/fixture/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './tasks'
export * from './listProject'
8 changes: 8 additions & 0 deletions apps/frontend/src/tests/fixture/listProject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { ListProject } from '@/store'
import { TasksSelectorType } from '@/store'

export const liveListProject: ListProject = {
id: '1',
name: '生活',
type: TasksSelectorType.listProject,
}
20 changes: 20 additions & 0 deletions apps/frontend/src/tests/fixture/tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TaskStatus } from '@/store'

export const tasks = [
{
id: '0',
title: '吃饭',
content: '今天吃什么',
status: TaskStatus.ACTIVE,
projectId: '1',
position: 1,
},
{
id: '1',
title: '写代码',
content: '今天写代码了嘛',
status: TaskStatus.COMPLETED,
projectId: '1',
position: 2,
},
]
2 changes: 1 addition & 1 deletion apps/frontend/src/tests/helper/fireEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const fireEvent = {
keydown(options: KeyboardEventInit) {
keyDown(options: KeyboardEventInit) {
const event = new KeyboardEvent('keydown', options)
window.dispatchEvent(event)
},
Expand Down
Loading

1 comment on commit 61f2700

@vercel
Copy link

@vercel vercel bot commented on 61f2700 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.