|
| 1 | +import { describe, it, vi, expect, afterEach } from 'vitest'; |
| 2 | +import { MountingOptions, VueWrapper, mount } from '@vue/test-utils'; |
| 3 | +import AlertUpdateToLatest from './AlertUpdateToLatest.vue'; |
| 4 | +type ComponentProps = InstanceType<typeof AlertUpdateToLatest>['$props']; |
| 5 | + |
| 6 | +describe('AlertUpdateToLatest component tests', () => { |
| 7 | + // @ts-expect-error SILENCE JSDOM ERRORS ABOUT LOCATION RELOAD |
| 8 | + window.location = vi.fn(); |
| 9 | + global.fetch = vi.fn(); |
| 10 | + let wrapper: VueWrapper<any>; |
| 11 | + const findAlertButton = () => wrapper.find('.puik-button'); |
| 12 | + |
| 13 | + const factory = (props: ComponentProps, options: MountingOptions<any> = {}) => { |
| 14 | + wrapper = mount(AlertUpdateToLatest, { |
| 15 | + props, |
| 16 | + ...options |
| 17 | + }); |
| 18 | + }; |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + vi.clearAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should call the update route on click', async () => { |
| 25 | + vi.mocked<any>(global.fetch).mockImplementationOnce(async () => ({ |
| 26 | + status: 200, |
| 27 | + ok: true, |
| 28 | + json: async () => ({ |
| 29 | + ps_accounts: { |
| 30 | + status: true |
| 31 | + } |
| 32 | + }) |
| 33 | + })); |
| 34 | + factory({ |
| 35 | + enableLink: '/enable' |
| 36 | + }); |
| 37 | + expect(findAlertButton()); |
| 38 | + await findAlertButton().trigger('click'); |
| 39 | + expect(fetch).toHaveBeenCalledWith('/upgrade', { |
| 40 | + method: 'POST' |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should not call the update route on click if there is no link passed', async () => { |
| 45 | + vi.mocked<any>(global.fetch).mockImplementationOnce(async () => ({ |
| 46 | + status: 200, |
| 47 | + ok: true, |
| 48 | + json: async () => ({ |
| 49 | + ps_accounts: { |
| 50 | + status: true |
| 51 | + } |
| 52 | + }) |
| 53 | + })); |
| 54 | + factory({ |
| 55 | + enableLink: null |
| 56 | + }); |
| 57 | + expect(findAlertButton()); |
| 58 | + await findAlertButton().trigger('click'); |
| 59 | + expect(fetch).not.toBeCalled(); |
| 60 | + }); |
| 61 | +}); |
0 commit comments