|
| 1 | +import { |
| 2 | + checkElementExistenceOnMount, |
| 3 | + checkOnMountAndUnmountEvents, |
| 4 | + checkOnStartEvents, |
| 5 | + checkOnStopEvents |
| 6 | +} from '@src/helpers/test' |
| 7 | +import { ref } from '@vue/composition-api' |
| 8 | +import { useBeforeUnload } from '@src/vue-use-kit' |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + jest.clearAllMocks() |
| 12 | +}) |
| 13 | + |
| 14 | +const testComponent = (onMount = true) => ({ |
| 15 | + template: ` |
| 16 | + <div> |
| 17 | + <div id="isTracking" v-if="isTracking"></div> |
| 18 | + <button id="start" @click="start"></button> |
| 19 | + <button id="stop" @click="stop"></button> |
| 20 | + </div> |
| 21 | + `, |
| 22 | + setup() { |
| 23 | + const isDirty = ref(false) |
| 24 | + const { isTracking, start, stop } = useBeforeUnload(isDirty, onMount) |
| 25 | + return { isTracking, start, stop } |
| 26 | + } |
| 27 | +}) |
| 28 | + |
| 29 | +describe('useBeforeUnload', () => { |
| 30 | + const events = ['beforeunload'] |
| 31 | + |
| 32 | + it('should add events on mounted and remove them on unmounted', async () => { |
| 33 | + await checkOnMountAndUnmountEvents(window, events, testComponent) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should add events again when start is called', async () => { |
| 37 | + await checkOnStartEvents(window, events, testComponent) |
| 38 | + }) |
| 39 | + |
| 40 | + it('should remove events when stop is called', async () => { |
| 41 | + await checkOnStopEvents(window, events, testComponent) |
| 42 | + }) |
| 43 | + |
| 44 | + it('should show #isTracking when runOnMount is true', async () => { |
| 45 | + await checkElementExistenceOnMount(true, testComponent) |
| 46 | + }) |
| 47 | + |
| 48 | + it('should not show #isTracking when runOnMount is false', async () => { |
| 49 | + await checkElementExistenceOnMount(false, testComponent) |
| 50 | + }) |
| 51 | +}) |
0 commit comments