Skip to content

Commit

Permalink
feat(teach-frontend): 03-测试第一个组件 TheHeader - 测试 useRouter 的策略
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jun 11, 2023
1 parent 6b5b180 commit 25f51b5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
6 changes: 4 additions & 2 deletions apps/teach-frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "frontend",
"name": "teach-frontend",
"type": "module",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview"
"preview": "vite preview",
"test": "vitest"
},
"dependencies": {
"@imengyu/vue3-context-menu": "^1.1.10",
Expand Down Expand Up @@ -38,6 +39,7 @@
"typescript": "^4.9.5",
"unocss": "^0.48.3",
"vite": "^4.0.4",
"vitest": "^0.27.0",
"vue-router-mock": "^0.2.0",
"vue-tsc": "^1.0.24"
}
Expand Down
14 changes: 8 additions & 6 deletions apps/teach-frontend/src/components/header/TheHeader.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<script setup lang="ts">
import { useGoto } from '@/composables/goto'
import { useGoto } from './theHeader'
const { gotoHome, gotoGithub, gotoSettings } = useGoto()
const { goToHome, goToSettings, goToGithub } = useGoto()
</script>

<template>
<div class="w-full base-color h-40px px-1% flex justify-between items-center text-16px">
<div class="cursor-pointer" aria-label="Go Home" @click="gotoHome()">
<div
class="w-full base-color h-40px px-1% flex justify-between items-center text-16px"
>
<div class="cursor-pointer" aria-label="Go Home" @click="goToHome">
DiDa
</div>
<div class="flex items-center justify-start">
<button class="mx-2 !outline-none" @click="gotoSettings()">
<button class="mx-2 !outline-none" @click="goToSettings">
<div i="carbon-settings" />
</button>
<button class="mx-2 !outline-none" @click="gotoGithub()">
<button class="mx-2 !outline-none" @click="goToGithub">
<div i="mdi-github" />
</button>
</div>
Expand Down
33 changes: 33 additions & 0 deletions apps/teach-frontend/src/components/header/theHeader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useRouter } from 'vue-router'
import { useGoto } from './theHeader'

vi.mock('vue-router')

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

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

goToHome()

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

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

goToSettings()

expect(pushFn).toBeCalledWith({ name: 'Settings' })
})
})
27 changes: 27 additions & 0 deletions apps/teach-frontend/src/components/header/theHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useRouter } from 'vue-router'

export function useGoto() {
const router = useRouter()

function goToHome() {
router.push({
name: 'Home',
})
}

function goToSettings() {
router.push({
name: 'Settings',
})
}

function goToGithub() {
window.open('https://github.com/cuixueshe/dida')
}

return {
goToHome,
goToSettings,
goToGithub,
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

1 comment on commit 25f51b5

@vercel
Copy link

@vercel vercel bot commented on 25f51b5 Jun 11, 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.