|
1 | | -import { mount } from '@src/helpers/test' |
| 1 | +import { |
| 2 | + checkElementExistenceOnMount, |
| 3 | + checkOnMountAndUnmountEvents, |
| 4 | + checkOnStartEvents, |
| 5 | + checkOnStopEvents, |
| 6 | + mount |
| 7 | +} from '@src/helpers/test' |
2 | 8 | import { useLocation } from '@src/vue-use-kit' |
3 | 9 |
|
4 | 10 | afterEach(() => { |
@@ -35,58 +41,27 @@ describe('useLocation', () => { |
35 | 41 | 'protocol', |
36 | 42 | 'search' |
37 | 43 | ] |
38 | | - const events = ['popstate', 'pushstate', 'replacestate'] |
39 | 44 |
|
40 | | - it('should call popstate, pushstate and replacestate onMounted', async () => { |
41 | | - const addEventListenerSpy = jest.spyOn(window, 'addEventListener') |
42 | | - const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener') |
43 | | - const wrapper = mount(testComponent()) |
44 | | - await wrapper.vm.$nextTick() |
45 | | - expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length) |
46 | | - events.forEach(event => { |
47 | | - expect(addEventListenerSpy).toBeCalledWith(event, expect.any(Function)) |
48 | | - }) |
| 45 | + const events = ['popstate', 'pushstate', 'replacestate'] |
49 | 46 |
|
50 | | - // Destroy instance to check if the remove event listener is being called |
51 | | - wrapper.destroy() |
52 | | - expect(removeEventListenerSpy).toHaveBeenCalledTimes(events.length) |
53 | | - events.forEach(event => { |
54 | | - expect(removeEventListenerSpy).toBeCalledWith(event, expect.any(Function)) |
55 | | - }) |
| 47 | + it('should add events on mounted and remove them on unmounted', async () => { |
| 48 | + await checkOnMountAndUnmountEvents(window, events, testComponent) |
56 | 49 | }) |
57 | 50 |
|
58 | | - it('should call addEventListener again when start is called', async () => { |
59 | | - const addEventListenerSpy = jest.spyOn(window, 'addEventListener') |
60 | | - const wrapper = mount(testComponent()) |
61 | | - expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length) |
62 | | - wrapper.find('#stop').trigger('click') |
63 | | - |
64 | | - // Wait for Vue to append #start in the DOM |
65 | | - await wrapper.vm.$nextTick() |
66 | | - wrapper.find('#start').trigger('click') |
67 | | - expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length * 2) |
| 51 | + it('should add events again when start is called', async () => { |
| 52 | + await checkOnStartEvents(window, events, testComponent) |
68 | 53 | }) |
69 | 54 |
|
70 | | - it('should call removeEventListener when stop is called', async () => { |
71 | | - const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener') |
72 | | - const wrapper = mount(testComponent()) |
73 | | - wrapper.find('#stop').trigger('click') |
74 | | - |
75 | | - // Wait for Vue to append #start in the DOM |
76 | | - await wrapper.vm.$nextTick() |
77 | | - expect(removeEventListenerSpy).toHaveBeenCalledTimes(events.length) |
| 55 | + it('should remove events when stop is called', async () => { |
| 56 | + await checkOnStopEvents(window, events, testComponent) |
78 | 57 | }) |
79 | 58 |
|
80 | | - it('should show #isTracking when onMount is true', async () => { |
81 | | - const wrapper = mount(testComponent(true)) |
82 | | - await wrapper.vm.$nextTick() |
83 | | - expect(wrapper.find('#isTracking').exists()).toBe(true) |
| 59 | + it('should show #isTracking when runOnMount is true', async () => { |
| 60 | + await checkElementExistenceOnMount(true, testComponent) |
84 | 61 | }) |
85 | 62 |
|
86 | | - it('should not show #isTracking when onMount is false', async () => { |
87 | | - const wrapper = mount(testComponent(false)) |
88 | | - await wrapper.vm.$nextTick() |
89 | | - expect(wrapper.find('#isTracking').exists()).toBe(false) |
| 63 | + it('should not show #isTracking when runOnMount is false', async () => { |
| 64 | + await checkElementExistenceOnMount(false, testComponent) |
90 | 65 | }) |
91 | 66 |
|
92 | 67 | it('should display the locationState object', async () => { |
|
0 commit comments