-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/tests/composables/useVueExitIntent/inactiveSeconds.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { test, describe, expect, afterEach, vi } from 'vitest'; | ||
import { useVueExitIntent } from '@/composables/useVueExitIntent'; | ||
import { shallowMount, mount } from '@vue/test-utils'; | ||
import * as utils from '@/utils'; | ||
|
||
const { defaultOptions } = utils; | ||
|
||
describe('Respects option inactiveSeconds', () => { | ||
afterEach(() => { | ||
localStorage.clear(); | ||
vi.clearAllTimers(); | ||
}); | ||
|
||
test('Triggers immediately if triggerOnPageLoad is true', async () => { | ||
const userOptions = { | ||
...defaultOptions, | ||
triggerOnPageLoad: true, | ||
inactiveSeconds: 5 | ||
}; | ||
|
||
const App = { | ||
template: `<div></div>`, | ||
setup() { | ||
const { isShowing, close } = useVueExitIntent(userOptions); | ||
|
||
return { | ||
isShowing, | ||
close | ||
}; | ||
} | ||
}; | ||
|
||
const wrapper = shallowMount(App); | ||
await wrapper.vm.$nextTick(); | ||
expect(wrapper.vm.isShowing).toBe(true); | ||
|
||
wrapper.vm.close(); | ||
expect(wrapper.vm.isShowing).toBe(false); | ||
}); | ||
|
||
test('Triggers on mouseleave after inactiveSeconds', async () => { | ||
vi.useFakeTimers(); | ||
vi.spyOn(utils, 'isTouchDevice').mockReturnValue(false); | ||
|
||
const userOptions = { | ||
...defaultOptions, | ||
inactiveSeconds: 2 | ||
}; | ||
|
||
const App = { | ||
template: `<div></div>`, | ||
setup() { | ||
const { isShowing, close } = useVueExitIntent(userOptions); | ||
|
||
return { | ||
isShowing, | ||
close | ||
}; | ||
} | ||
}; | ||
|
||
const wrapper = mount(App); | ||
await wrapper.vm.$nextTick(); | ||
expect(wrapper.vm.isShowing).toBe(false); | ||
|
||
vi.advanceTimersByTime(1000); | ||
expect(wrapper.vm.isShowing).toBe(false); | ||
|
||
vi.advanceTimersByTime(1000); | ||
document.documentElement.dispatchEvent(new MouseEvent('mouseleave')); | ||
expect(wrapper.vm.isShowing).toBe(true); | ||
|
||
vi.useRealTimers(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters