Skip to content

Commit

Permalink
test: refactor search tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jun 13, 2023
1 parent 4daa9eb commit 2ec3c3a
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const props = defineProps<{
title: string
desc: string
done: boolean
from: TasksSelector
from: TasksSelector | undefined
id: string
}>()
Expand Down
3 changes: 2 additions & 1 deletion apps/frontend/src/components/command/searchTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface SearchTaskItem {
title: string
desc: string
done: boolean
from: TasksSelector
from: TasksSelector | undefined
}

export const filteredTasks = ref<Fuse.FuseResult<SearchTaskItem>[]>([])
Expand All @@ -19,6 +19,7 @@ const fuse = new Fuse([] as SearchTaskItem[], {
export async function searchTasks(input: string) {
const tasksStore = useTasksStore()
const projectsStore = useListProjectsStore()

const rawTasks = await tasksStore.findAllTasksNotRemoved()
const tasks = rawTasks.map((task) => {
const done = task.status === TaskStatus.COMPLETED
Expand Down
46 changes: 20 additions & 26 deletions apps/frontend/src/components/command/tests/searchTasks.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { filteredTasks, resetSearchTasks, searchTasks } from '../searchTasks'
import { completeSmartProject } from '@/store'

describe.skip('SearchTasks', () => {
beforeEach(() => {
// TODO 需要去解决2个依赖
// 1. projectsStore.findProject 需要返回一个 Project
// 2. tasksStore.findAllTasksNotRemoved 需要返回没有处理过的 task
// vi.spyOn(TaskService, 'findAllTasksNotRemoved').mockResolvedValue([
// createMockTask('吃饭'),
// createMockTask('写代码', '一杯茶,一包烟,一行代码写一天'),
// ])
})

afterEach(() => {
resetSearchTasks()
})
vi.mock('@/store/tasks')
vi.mock('@/store/listProjects')

describe('SearchTasks', () => {
it('should be search a task by title', async () => {
await searchTasks('写代码')

Expand All @@ -31,7 +21,7 @@ describe.skip('SearchTasks', () => {
})

it('should not be found when the task does not exist', async () => {
await searchTasks('睡觉')
await searchTasks('运动')

expect(filteredTasks.value.length).toBe(0)
})
Expand All @@ -43,14 +33,18 @@ describe.skip('SearchTasks', () => {

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

// function createMockTask(title: string, content = '') {
// return {
// id: 0,
// title,
// content,
// project: undefined,
// index: 1,
// }
// }
it('should be task’s project is listProject when status is active', async () => {
await searchTasks('写代码')

const task = filteredTasks.value[0].item
expect(task.from!.type).toBe('listProject')
})

it('should be task’s project is completeSmartProject when status is completed', async () => {
await searchTasks('睡觉')

const task = filteredTasks.value[0].item
expect(task.from!.name).toBe(completeSmartProject.name)
})
})
2 changes: 1 addition & 1 deletion apps/frontend/src/components/task/TaskItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NPopover } from 'naive-ui'
import { ref } from 'vue'
import { useTaskOperationMessage, useTaskRightContextMenu } from '@/composables'
import { TaskStatus, useTasksStore, useThemeStore } from '@/store'
import type { Project, Task } from '@/store'
import type { ListProject, Task } from '@/store'
interface Props {
task: Task
Expand Down
14 changes: 14 additions & 0 deletions apps/frontend/src/store/__mocks__/listProjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { vi } from 'vitest'
import type { ListProject } from '../listProjects'

export const useListProjectsStore = vi.fn(() => {
return {
findProject() {
return {
id: '1',
name: '集草器',
type: 'listProject',
} as ListProject
},
}
})
39 changes: 39 additions & 0 deletions apps/frontend/src/store/__mocks__/tasks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { vi } from 'vitest'
export { TaskStatus } from '../tasks'

// 这里还是缺了一个类型
// TODO 这里要等后端处理好返回的类型之后在解决
const tasks = [
{
_id: '0',
title: '吃饭',
content: '今天吃什么',
status: 'active',
projectId: '1',
position: 0,
},
{
_id: '1',
title: '写代码',
content: '一杯茶,一包烟,一行代码写一天',
status: 'active',
projectId: '1',
position: 1,
},
{
_id: '2',
title: '睡觉',
content: '一睡睡一天',
status: 'completed',
projectId: '1',
position: 2,
},
]

export const useTasksStore = vi.fn(() => {
return {
findAllTasksNotRemoved() {
return tasks
},
}
})
24 changes: 12 additions & 12 deletions apps/frontend/src/store/listProjects.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { TaskStatus } from './tasks'
import { TasksSelectorType } from './tasksSelector'
import { fetchAllProjects, fetchAllTasks, fetchCreateProject } from '@/api'
import { useTasksSelectorStore } from '@/store'

type ProjectType = 'listProject' | 'smartProject'
export interface Project {
export interface ListProject {
id: string
name: string
type: ProjectType
type: TasksSelectorType.listProject
}

export const useListProjectsStore = defineStore('newProjects', () => {
const tasksSelectorStore = useTasksSelectorStore()
const projects = ref<Project[]>([])
const projects = ref<ListProject[]>([])

async function init() {
const rawProjects: any = await fetchAllProjects()
Expand All @@ -23,11 +23,11 @@ export const useListProjectsStore = defineStore('newProjects', () => {
tasksSelectorStore.setCurrentSelector(projects.value[0])
}

function selectProject(project: Project): void
function selectProject(projectId: Project['id']): void
function selectProject(projectName: Project['name']): void
function selectProject(projectOrNameOrId: Project | string): void {
let project: Project | undefined
function selectProject(project: ListProject): void
function selectProject(projectId: ListProject['id']): void
function selectProject(projectName: ListProject['name']): void
function selectProject(projectOrNameOrId: ListProject | string): void {
let project: ListProject | undefined

if (typeof projectOrNameOrId === 'string')
project = findProject(projectOrNameOrId)
Expand All @@ -39,7 +39,7 @@ export const useListProjectsStore = defineStore('newProjects', () => {
tasksSelectorStore.setCurrentSelector(project)
}

function findProject(projectIdOrName: string): Project | undefined {
function findProject(projectIdOrName: string): ListProject | undefined {
return projects.value.find(p => p.name === projectIdOrName || p.id === projectIdOrName)
}

Expand Down Expand Up @@ -71,11 +71,11 @@ export const useListProjectsStore = defineStore('newProjects', () => {
})

// TODO 需要提供后端返回的 project 的 type shape
function normalizeProject(rawProject: any): Project {
function normalizeProject(rawProject: any): ListProject {
return {
id: `${rawProject._id}`,
name: rawProject.name,
type: 'listProject',
type: TasksSelectorType.listProject,
}
}

Expand Down
5 changes: 3 additions & 2 deletions apps/frontend/src/store/smartProjects.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { defineStore } from 'pinia'
import { TaskStatus } from './tasks'
import { TasksSelectorType } from './tasksSelector'
import { fetchAllTasks } from '@/api'
import { useTasksSelectorStore } from '@/store'

export interface SmartProject {
name: string
type: 'smartProject'
type: TasksSelectorType.smartProject
}

function createSmartProject(name: string): SmartProject {
return {
name,
type: 'smartProject',
type: TasksSelectorType.smartProject,
}
}

Expand Down
1 change: 0 additions & 1 deletion apps/frontend/src/store/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export const useTasksStore = defineStore('tasksStore', () => {
async function addTask(title: string) {
if (!tasksSelectorStore.currentSelector)
return

if (tasksSelectorStore.currentSelector.type !== 'listProject')
return

Expand Down
8 changes: 6 additions & 2 deletions apps/frontend/src/store/tasksSelector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { Project, SmartProject } from '@/store'
import type { ListProject, SmartProject } from '@/store'
import { loadListProjectTasks, loadSmartProjectTasks, useTasksStore } from '@/store'

export type TasksSelector = Project | SmartProject | undefined
export type TasksSelector = ListProject | SmartProject
export enum TasksSelectorType {
listProject = 'listProject',
smartProject = 'smartProject',
}

export const useTasksSelectorStore = defineStore('tasksSelectorStore', () => {
const tasksStore = useTasksStore()
Expand Down

1 comment on commit 2ec3c3a

@vercel
Copy link

@vercel vercel bot commented on 2ec3c3a Jun 13, 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.