import { flushPromises, mount } from '@vue/test-utils'; import { VAlert, VBtn, VSnackbar } from 'vuetify/components'; import UpdateSnackbar from '../UpdateSnackbar.vue'; const registration: any = { addEventListener: vi.fn().mockImplementation((e: any, callback: any) => { callback(); }), installing: { addEventListener: vi.fn().mockImplementation((e: any, callback: any) => { callback(); }), }, waiting: { postMessage: vi.fn(), }, update: vi.fn(), }; const navigator: any = { serviceWorker: { getRegistration() { return registration; }, }, }; const navigatorWithoutInstalling: any = { serviceWorker: { getRegistration() { return { addEventListener: vi .fn() .mockImplementation((e: any, callback: any) => { callback(); }), }; }, }, }; const navigatorWithoutWaiting: any = { serviceWorker: { getRegistration() { return { addEventListener: vi .fn() .mockImplementation((e: any, callback: any) => { callback(); }), installing: { addEventListener: vi .fn() .mockImplementation((e: any, callback: any) => { callback(); }), }, }; }, }, }; describe('UpdateSnackbar tests', () => { it('should mount', () => { const wrapper = mount(UpdateSnackbar); expect(wrapper).toBeTruthy(); }); it('should set v-alert state', async () => { vi.spyOn(globalThis, 'navigator', 'get').mockReturnValue(navigator); const wrapper = mount(UpdateSnackbar); await flushPromises(); await wrapper.findComponent(VAlert).setValue(false); expect(wrapper.findComponent(VAlert).text()).toBe(''); }); it('should set v-snackbar state', async () => { vi.spyOn(globalThis, 'navigator', 'get').mockReturnValue(navigator); const wrapper = mount(UpdateSnackbar); await flushPromises(); await wrapper.findComponent(VSnackbar).setValue(false); expect(wrapper.findComponent(VSnackbar).text()).toBe(''); }); it('should check for updates if navigator present', () => { vi.spyOn(globalThis, 'navigator', 'get').mockReturnValue(navigator); const wrapper = mount(UpdateSnackbar); expect(wrapper).toBeTruthy(); }); it('should check for updates on interval', async () => { vi.useFakeTimers(); vi.spyOn(globalThis, 'navigator', 'get').mockReturnValue(navigator); mount(UpdateSnackbar); await flushPromises(); vi.advanceTimersByTime(1000); expect(navigator.serviceWorker.getRegistration().update).toHaveBeenCalled(); vi.useRealTimers(); }); it('should show installing when update found', async () => { vi.spyOn(globalThis, 'navigator', 'get').mockReturnValue( navigatorWithoutInstalling, ); const wrapper = mount(UpdateSnackbar); await flushPromises(); expect(wrapper.findComponent(VBtn).text()).toBe('labels.installing'); }); it('should ignore update if waiting not found', async () => { vi.spyOn(globalThis, 'navigator', 'get').mockReturnValue( navigatorWithoutWaiting, ); const wrapper = mount(UpdateSnackbar); await flushPromises(); expect(wrapper.findComponent(VBtn).text()).toBe('labels.installing'); }); it('should apply update on user confirmation', async () => { vi.spyOn(globalThis, 'navigator', 'get').mockReturnValue(navigator); const wrapper = mount(UpdateSnackbar); await flushPromises(); await wrapper.findComponentByTestId('update').trigger('click'); expect( navigator.serviceWorker.getRegistration().waiting.postMessage, ).toHaveBeenCalledWith('SKIP_WAITING'); }); });