From bc2131214253c0d6356c67c3f750dae32e54f236 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Thu, 4 Apr 2024 16:20:23 +0800 Subject: [PATCH] 1,419th Commit --- .../crud-operations/__tests__/+page.spec.ts | 32 +++++++++++++++++++ mock/src/routes/todos/+handler.ts | 13 ++------ mock/src/routes/todos/response.ts | 15 +++++++++ 3 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 app/src/routes/(backstage)/(playground)/crud-operations/__tests__/+page.spec.ts create mode 100644 mock/src/routes/todos/response.ts diff --git a/app/src/routes/(backstage)/(playground)/crud-operations/__tests__/+page.spec.ts b/app/src/routes/(backstage)/(playground)/crud-operations/__tests__/+page.spec.ts new file mode 100644 index 00000000..eec4ba89 --- /dev/null +++ b/app/src/routes/(backstage)/(playground)/crud-operations/__tests__/+page.spec.ts @@ -0,0 +1,32 @@ +import type { VueWrapper } from '@vue/test-utils'; +import { mount, flushPromises } from '@vue/test-utils'; +import * as xui from '@x/ui'; +import todos from 'mock/todos/response'; + +import localer from '~/plugins/localer'; +import router from '~/plugins/router'; + +import Page from '../+page.vue'; +import useStore from '../store'; + +let wrapper: VueWrapper; + +afterEach(() => { + wrapper.unmount(); +}); + +test('initial', async () => { + vi.spyOn(xui, 'request').mockImplementation((url, { method }: any): any => { + if (url === '/todos' && method === 'POST') { + return { _data: todos.basic, status: 200 }; + } + }); + + wrapper = mount(Page, { global: { plugins: [router, localer] } }); + + await flushPromises(); + + const store = useStore(); + expect(store.state.todosRows).toStrictEqual(todos.basic.result); + expect(store.state.todosCount).toStrictEqual(todos.basic.total); +}); diff --git a/mock/src/routes/todos/+handler.ts b/mock/src/routes/todos/+handler.ts index 3a9ac1bb..857c6ca0 100644 --- a/mock/src/routes/todos/+handler.ts +++ b/mock/src/routes/todos/+handler.ts @@ -1,16 +1,9 @@ import type { FastifyInstance } from 'fastify'; +import response from './response'; + export default async (app: FastifyInstance) => { app.post('', async () => { - return { - result: [ - { - _id: '643e4d8f178309ee94a8dcb8', - title: 'Vue', - completed: true, - }, - ], - total: 1, - }; + return response.basic; }); }; diff --git a/mock/src/routes/todos/response.ts b/mock/src/routes/todos/response.ts new file mode 100644 index 00000000..e3af1649 --- /dev/null +++ b/mock/src/routes/todos/response.ts @@ -0,0 +1,15 @@ +export default { + basic: { + result: [ + { _id: '1', title: 'Vue', completed: true }, + { _id: '2', title: 'Tauri', completed: true }, + { _id: '3', title: 'Fastify', completed: true }, + { _id: '4', title: 'Pulumi', completed: true }, + { _id: '5', title: 'TypeScript', completed: true }, + { _id: '6', title: 'Node.js', completed: true }, + { _id: '7', title: 'MongoDB', completed: true }, + { _id: '8', title: 'Redis', completed: true }, + ], + total: 8, + }, +};