Skip to content

Commit

Permalink
feat(teach-frontend): 04&05-测试 useRouter 的最佳测试&重构test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jun 16, 2023
1 parent b406022 commit 854f527
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 23 deletions.
2 changes: 2 additions & 0 deletions apps/teach-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@unocss/reset": "^0.48.3",
"@vitejs/plugin-vue": "^4.0.0",
"@vitejs/plugin-vue-jsx": "^3.0.0",
"@vue/test-utils": "^2.2.10",
"@vueuse/core": "^9.10.0",
"cross-env": "^7.0.3",
"flush-promises": "^1.0.2",
Expand All @@ -42,6 +43,7 @@
"unocss": "^0.48.3",
"vite": "^4.0.4",
"vitest": "^0.27.0",
"vue-router-mock": "^0.2.0",
"vue-tsc": "^1.0.24"
}
}
35 changes: 13 additions & 22 deletions apps/teach-frontend/src/components/header/theHeader.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useRouter } from 'vue-router'
import { describe, expect, it } from 'vitest'
import { useGoto } from './theHeader'
import { useSetup } from '@/tests/helper'

vi.mock('vue-router')

const pushFn = vi.fn()
vi.mocked(useRouter as () => { push: Function }).mockImplementation(() => {
return {
push: pushFn,
}
})

describe('theHeader', () => {
beforeEach(() => {
pushFn.mockClear()
})
describe('the header', () => {
it('should be go to home page', () => {
const { goToHome } = useGoto()

goToHome()
const { router } = useSetup(() => {
const { goToHome } = useGoto()
goToHome()
})

expect(pushFn).toBeCalledWith({ name: 'Home' })
expect(router.push).toBeCalledWith({ name: 'Home' })
})

it('should be go to settings page', () => {
const { goToSettings } = useGoto()
const { router } = useSetup(() => {
const { goToSettings } = useGoto()

goToSettings()
goToSettings()
})

expect(pushFn).toBeCalledWith({ name: 'Settings' })
expect(router.push).toBeCalledWith({ name: 'Settings' })
})
})
14 changes: 14 additions & 0 deletions apps/teach-frontend/src/tests/helper/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { mount } from '@vue/test-utils'
export function useSetup(setup: () => void) {
const Comp = {
render() {},
setup,
}

const wrapper = mount(Comp)

return {
wrapper,
router: wrapper.router,
}
}
1 change: 1 addition & 0 deletions apps/teach-frontend/src/tests/helper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './component'
6 changes: 5 additions & 1 deletion apps/teach-frontend/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import path from 'node:path'
import { defineConfig } from 'vite'
import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Unocss from 'unocss/vite'

export default defineConfig({
test: {
environment: 'happy-dom',
setupFiles: './vitest.setup.ts',
},
server: {
proxy: {
'/api': {
Expand Down
25 changes: 25 additions & 0 deletions apps/teach-frontend/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
VueRouterMock,
createRouterMock,
injectRouterMock,
} from 'vue-router-mock'
import { beforeEach, vi } from 'vitest'
import { config } from '@vue/test-utils'

function setupRouterMock() {
const router = createRouterMock({
spy: {
create: fn => vi.fn(fn),
reset: spy => spy.mockClear(),
},
})

beforeEach(() => {
router.reset()
injectRouterMock(router)
})

config.plugins.VueWrapper.install(VueRouterMock)
}

setupRouterMock()
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 854f527

Please sign in to comment.