-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(teach-frontend): 04&05-测试 useRouter 的最佳测试&重构test helper
- Loading branch information
1 parent
b406022
commit 854f527
Showing
7 changed files
with
64 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 13 additions & 22 deletions
35
apps/teach-frontend/src/components/header/theHeader.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './component' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.