197 lines
8.1 KiB
TypeScript
197 lines
8.1 KiB
TypeScript
// @vitest-environment jsdom
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { computed, defineComponent, ref } from 'vue'
|
||
import { flushPromises, mount } from '@vue/test-utils'
|
||
import { createPinia, setActivePinia } from 'pinia'
|
||
import { createMemoryHistory, createRouter } from 'vue-router'
|
||
import { createI18n } from 'vue-i18n'
|
||
import zh from '@/i18n/locales/zh'
|
||
import { useTabsStore } from '@/stores/tabs'
|
||
import { useTabTitle } from '@/composables/useTabTitle'
|
||
import AppLayout from './AppLayout.vue'
|
||
|
||
/**
|
||
* 页签这一套真正要守住的是三句话:多张单子各占一格、切回去还是那一屏、
|
||
* 列表页的条件留得住而单据页不留活(留活反而会串数据,见 AppLayout 里那段注释)。
|
||
* 所以这里挂着真的 AppLayout 跑一遍导航,不去测 store 的边角。
|
||
*/
|
||
const shell = vi.hoisted(() => ({ phoneShell: false }))
|
||
vi.mock('@/composables/useMobileShell', () => ({
|
||
useChromeless: () => ({ chromeless: computed(() => false) }),
|
||
useMobileShell: () => ({
|
||
isPhone: computed(() => shell.phoneShell),
|
||
desktopPinned: computed(() => false),
|
||
phoneShell: computed(() => shell.phoneShell),
|
||
pinDesktop: vi.fn(),
|
||
fontScale: computed(() => 'm'),
|
||
setFontScale: vi.fn()
|
||
})
|
||
}))
|
||
|
||
vi.mock('@/api', () => ({
|
||
supportAPI: {
|
||
listNotifications: vi.fn(async () => ({ items: [] })),
|
||
unreadCount: vi.fn(async () => ({ count: 0 })),
|
||
readNotification: vi.fn(async () => undefined),
|
||
readAllNotifications: vi.fn(async () => undefined)
|
||
}
|
||
}))
|
||
vi.mock('@/api/task', () => ({ countTodo: vi.fn(async () => ({ count: 0 })) }))
|
||
vi.mock('@/api/work', () => ({ countRead: vi.fn(async () => ({ count: 0 })) }))
|
||
vi.mock('@/api/support', () => ({
|
||
listNotifications: vi.fn(async () => ({ items: [] })),
|
||
readAllNotifications: vi.fn(async () => undefined),
|
||
readNotification: vi.fn(async () => undefined),
|
||
unreadCount: vi.fn(async () => ({ count: 0 }))
|
||
}))
|
||
|
||
const ListPage = defineComponent({
|
||
setup() {
|
||
const keyword = ref('')
|
||
return { keyword }
|
||
},
|
||
template: '<div class="list-page"><input v-model="keyword" data-test="list-filter"><p data-test="list-echo">{{ keyword }}</p></div>'
|
||
})
|
||
|
||
const DetailPage = defineComponent({
|
||
setup() {
|
||
const jobs = ref(0)
|
||
// 与真单据页同一句话:把这张单子的标题递给页签,页签条上不该只写「单据详情」
|
||
useTabTitle(computed(() => '差旅报销 ' + String(jobs.value)))
|
||
return { jobs }
|
||
},
|
||
template: '<div class="detail-page"><p data-test="detail-jobs">{{ jobs }}</p></div>'
|
||
})
|
||
|
||
async function open(path: string) {
|
||
sessionStorage.clear()
|
||
const pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
const router = createRouter({
|
||
history: createMemoryHistory(),
|
||
routes: [
|
||
{ path: '/', name: 'workbench', component: ListPage, meta: { titleKey: 'nav.workbench' } },
|
||
{ path: '/todo', name: 'my-todo', component: ListPage, meta: { titleKey: 'nav.myTodo', cache: true } },
|
||
{ path: '/work/:job', name: 'work-detail', component: DetailPage, meta: { titleKey: 'work.detail' } }
|
||
]
|
||
})
|
||
await router.push(path)
|
||
const wrapper = mount(AppLayout, {
|
||
global: {
|
||
plugins: [pinia, router, createI18n({ legacy: false, locale: 'zh', messages: { zh } })],
|
||
// 详情页要能改自己的计数,所以不 stub 路由出口
|
||
stubs: { AppSidebar: true, AppHeader: true, MobileTabBar: true, Icon: true }
|
||
}
|
||
})
|
||
await flushPromises()
|
||
return { wrapper, router, tabs: useTabsStore() }
|
||
}
|
||
|
||
function tabLabels(wrapper: ReturnType<typeof mount>): string[] {
|
||
return Array.from(wrapper.findAll('[data-test="tab-bar"] li button')).map((node) => node.text())
|
||
}
|
||
|
||
describe('AppLayout:一次打开多张单据', () => {
|
||
beforeEach(() => vi.clearAllMocks())
|
||
|
||
it('两张单子各占一格,页签上写的是单据标题而不是页名', async () => {
|
||
const { wrapper, router } = await open('/work/A')
|
||
await router.push('/work/B')
|
||
await flushPromises()
|
||
const labels = tabLabels(wrapper)
|
||
expect(labels.some((one) => one.includes('差旅报销 0'))).toBe(true)
|
||
expect(wrapper.findAll('[data-test="tab-bar"] li')).toHaveLength(2)
|
||
})
|
||
|
||
it('点页签回到那一张的地址,另一张的计数不会被带过来', async () => {
|
||
const { wrapper, router } = await open('/work/A')
|
||
await router.push('/work/B')
|
||
await flushPromises()
|
||
const first = wrapper.findAll('[data-test="tab-bar"] li')[0].find('button')
|
||
await first!.trigger('click')
|
||
await flushPromises()
|
||
expect(router.currentRoute.value.fullPath).toBe('/work/A')
|
||
})
|
||
|
||
it('关掉当前这一格,落回隔壁那一张', async () => {
|
||
const { wrapper, router } = await open('/todo')
|
||
await router.push('/work/A')
|
||
await flushPromises()
|
||
const closeButtons = wrapper.findAll('[data-test="tab-bar"] li button[aria-label]')
|
||
await closeButtons[closeButtons.length - 1]!.trigger('click')
|
||
await flushPromises()
|
||
expect(router.currentRoute.value.path).toBe('/todo')
|
||
expect(useTabsStore().tabs.map((tab) => tab.path)).toEqual(['/todo'])
|
||
})
|
||
|
||
it('列表页留活:切去看单子再切回来,筛选那一格还是写着的样子', async () => {
|
||
const { wrapper, router } = await open('/todo')
|
||
await wrapper.find('[data-test="list-filter"]').setValue('待审')
|
||
await router.push('/work/A')
|
||
await flushPromises()
|
||
await router.push('/todo')
|
||
await flushPromises()
|
||
expect(wrapper.find('[data-test="list-echo"]').text()).toBe('待审')
|
||
})
|
||
|
||
it('单据详情不留活:每回点开都是重新拉的那一张', async () => {
|
||
const { wrapper, router } = await open('/work/A')
|
||
const vm = wrapper.findComponent(DetailPage).vm as unknown as { jobs: number }
|
||
vm.jobs = 1
|
||
await flushPromises()
|
||
await router.push('/todo')
|
||
await flushPromises()
|
||
await router.push('/work/A')
|
||
await flushPromises()
|
||
expect(wrapper.find('[data-test="detail-jobs"]').text()).toBe('0')
|
||
})
|
||
|
||
it('右键叫出菜单那一手不该顺手把这一格关掉(右键也派发 auxclick)', async () => {
|
||
const { wrapper, router } = await open('/work/A')
|
||
await router.push('/work/B')
|
||
await flushPromises()
|
||
const tabs = useTabsStore()
|
||
const first = wrapper.findAll('[data-test="tab-bar"] li')[0].find('button')
|
||
// button=2 是右键:它也会派发 auxclick,早先的写法是「auxclick 就关」,
|
||
// 于是菜单是给一张刚从列表里消失的页签打开的
|
||
await first!.trigger('auxclick', { button: 2 })
|
||
await flushPromises()
|
||
expect(tabs.tabs.map((tab) => tab.path)).toEqual(['/work/A', '/work/B'])
|
||
// 中键才是关
|
||
await first!.trigger('auxclick', { button: 1 })
|
||
await flushPromises()
|
||
expect(tabs.tabs.map((tab) => tab.path)).toEqual(['/work/B'])
|
||
})
|
||
|
||
it('关掉一张列表页签再点开,是干净的一屏而不是上一趟那份筛选', async () => {
|
||
const { wrapper, router } = await open('/todo')
|
||
await wrapper.find('[data-test="list-filter"]').setValue('待审')
|
||
await router.push('/work/A')
|
||
await flushPromises()
|
||
const tabs = useTabsStore()
|
||
tabs.close('/todo')
|
||
await router.push('/todo')
|
||
await flushPromises()
|
||
// 关掉就是关掉了:缓存里那一份要作废,重新点开不该把「待审」偷偷带回来
|
||
expect(wrapper.find('[data-test="list-filter"]').element.value).toBe('')
|
||
})
|
||
|
||
it('两张单据换着看是同一个实例换数据,不整页重挂', async () => {
|
||
const { wrapper, router } = await open('/work/A')
|
||
const vm = wrapper.findComponent(DetailPage).vm as unknown as { jobs: number }
|
||
vm.jobs = 5
|
||
await flushPromises()
|
||
await router.push('/work/B')
|
||
await flushPromises()
|
||
// 详情页自己 watch 着 :job 重拉(TaskDetailView / WorkDetailView / MobileTaskView 都是这么写的),
|
||
// 钥匙里带上路径就变成每换一张整页重挂,那些 watch 全成了死代码
|
||
const after = wrapper.findComponent(DetailPage).vm as unknown as { jobs: number }
|
||
expect(after.jobs).toBe(5)
|
||
})
|
||
|
||
it('只有一张页签时不摆这一条', async () => {
|
||
const { wrapper } = await open('/todo')
|
||
expect(wrapper.find('[data-test="tab-bar"]').exists()).toBe(false)
|
||
})
|
||
})
|