serverIdentityPanel.test.ts 2.1 KB

12345678910111213141516171819202122232425262728293031
  1. import { createPinia, setActivePinia } from 'pinia'
  2. import { flushPromises, mount } from '@vue/test-utils'
  3. import { afterEach, expect, it, vi } from 'vitest'
  4. import { useAuthStore } from '@/stores/auth'
  5. import ServerIdentityPanel from '@/components/app/ServerIdentityPanel.vue'
  6. const api=vi.hoisted(()=>({fetch:vi.fn(),save:vi.fn()}))
  7. vi.mock('@/api/serverIdentity',async original=>({...await original<typeof import('@/api/serverIdentity')>(),fetchServerIdentity:api.fetch,saveServerIdentity:api.save}))
  8. const initial={serverId:'server-a',name:'家里',displayName:'家里',revision:1}
  9. let auth:ReturnType<typeof useAuthStore>
  10. function setup(role='admin'){
  11. setActivePinia(createPinia());auth=useAuthStore();auth.status='authenticated';auth.user={id:'user',username:'fixture',role}
  12. auth.acceptServerIdentity(initial);api.fetch.mockResolvedValue(initial)
  13. return mount(ServerIdentityPanel)
  14. }
  15. afterEach(()=>{auth?.$dispose();vi.clearAllMocks()})
  16. it('saves the server name with the loaded revision',async()=>{
  17. const wrapper=setup();await flushPromises()
  18. api.save.mockResolvedValue({...initial,name:'办公室',displayName:'办公室',revision:2})
  19. await wrapper.get('input').setValue('办公室');await wrapper.get('form').trigger('submit');await flushPromises()
  20. expect(api.save).toHaveBeenCalledWith('办公室',1,'server-a');expect(auth.serverIdentity?.displayName).toBe('办公室')
  21. wrapper.unmount()
  22. })
  23. it('shows the same name read-only for ordinary accounts',async()=>{
  24. const wrapper=setup('user');await flushPromises();expect(wrapper.text()).toContain('家里');expect(wrapper.find('input').exists()).toBe(false);wrapper.unmount()
  25. })
  26. it('keeps an unsaved draft when another client changes the name and ignores older revisions',async()=>{
  27. const wrapper=setup();await flushPromises();await wrapper.get('input').setValue('我的草稿')
  28. auth.acceptServerIdentity({...initial,name:'其他端',displayName:'其他端',revision:2});await flushPromises()
  29. expect(wrapper.get<HTMLInputElement>('input').element.value).toBe('我的草稿')
  30. auth.acceptServerIdentity(initial);expect(auth.serverIdentity?.name).toBe('其他端');wrapper.unmount()
  31. })