Skip to content

Commit 7a93ff1

Browse files
committed
improvement(useIntersection): Adding tests
1 parent 1d410bc commit 7a93ff1

File tree

5 files changed

+63
-20
lines changed

5 files changed

+63
-20
lines changed

src/components/useIntersection/stories/UseIntersectionAdvancedDemo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<use-intersection-element-demo
55
class="intersection__el"
66
:options="intersectionOpts"
7-
@change="handleIntersectionChange"
7+
@changed="handleIntersectionChange"
88
>
99
<video controls loop>
1010
<source :src="getAlternateVideoUrl(index)" type="video/mp4" />

src/components/useIntersection/stories/UseIntersectionDemo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<use-intersection-element-demo
55
class="intersection__el"
66
:options="intersectionOpts"
7-
@change="handleIntersectionChange"
7+
@changed="handleIntersectionChange"
88
@paused="handleIntersectionPaused"
99
/>
1010
</div>

src/components/useIntersection/stories/UseIntersectionElementDemo.vue

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
<slot></slot>
44
<button
55
class="button is-primary"
6-
@click="handleObserverState"
6+
@click="handleStart"
77
v-if="!isObserving"
88
title="Start observing"
99
>
1010
<span class="icon"><i class="fas fa-play"></i></span>
1111
</button>
1212
<button
1313
class="button is-warning"
14-
@click="handleObserverState"
14+
@click="handleStop"
1515
v-else
1616
title="Stop observing"
1717
>
@@ -45,24 +45,25 @@ export default Vue.extend({
4545
4646
watch(() => {
4747
if (!observedEntry.value) return
48-
emit('change', observedEntry.value)
48+
emit('changed', observedEntry.value)
4949
})
5050
5151
let isObserving = ref(true)
52-
const handleObserverState = () => {
52+
const handleStart = () => {
5353
if (!observedEntry.value) return
54-
if (isObserving.value) {
55-
stop()
56-
emit('paused', observedEntry.value.target, true)
57-
isObserving.value = false
58-
} else {
59-
start()
60-
emit('paused', observedEntry.value.target, false)
61-
isObserving.value = true
62-
}
54+
start()
55+
emit('paused', observedEntry.value.target, false)
56+
isObserving.value = true
57+
}
58+
59+
const handleStop = () => {
60+
if (!observedEntry.value) return
61+
stop()
62+
emit('paused', observedEntry.value.target, true)
63+
isObserving.value = false
6364
}
6465
65-
return { handleObserverState, isObserving, elRef }
66+
return { handleStart, handleStop, isObserving, elRef }
6667
}
6768
})
6869
</script>
Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,49 @@
1-
// import { mount } from '@src/helpers/test'
2-
// import { useIntersection } from '@src/vue-use-kit'
1+
import { mount } from '@src/helpers/test'
2+
import { ref } from '@src/api'
3+
import { useIntersection } from '@src/vue-use-kit'
4+
5+
let observe: any
6+
let unobserve: any
7+
let disconnect: any
8+
beforeEach(() => {
9+
observe = jest.fn()
10+
unobserve = jest.fn()
11+
disconnect = jest.fn()
12+
;(window as any).IntersectionObserver = jest.fn(() => ({
13+
observe,
14+
unobserve,
15+
disconnect
16+
}))
17+
})
18+
19+
const testComponent = (onMount = true) => ({
20+
template: `
21+
<div ref="elRef">
22+
<button id="stop" @click="stop"></button>
23+
<button id="start" @click="start"></button>
24+
</div>
25+
`,
26+
setup() {
27+
const elRef = ref(null)
28+
const { start, stop } = useIntersection(elRef, {}, onMount)
29+
return { start, stop, elRef }
30+
}
31+
})
332

433
describe('useIntersection', () => {
5-
it('should do something', () => {
6-
// Add test here
34+
it('should call IntersectionObserver when initialized', () => {
35+
expect(observe).toHaveBeenCalledTimes(0)
36+
mount(testComponent())
37+
expect(observe).toHaveBeenCalledTimes(1)
38+
})
39+
40+
it('should call IntersectionObserver again when start is called', async () => {
41+
expect(observe).toHaveBeenCalledTimes(0)
42+
const wrapper = mount(testComponent())
43+
expect(observe).toHaveBeenCalledTimes(1)
44+
wrapper.find('#stop').trigger('click')
45+
wrapper.find('#start').trigger('click')
46+
await wrapper.vm.$nextTick()
47+
expect(observe).toHaveBeenCalledTimes(2)
748
})
849
})

src/components/useIntersection/useIntersection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function useIntersection(
2020
if (!('IntersectionObserver' in window)) throw new Error(errorMsg)
2121

2222
// Do not start if the observer is already initialized
23+
// or the elRef does not exist
2324
if (observer || !elRef.value) return
2425
observer = new IntersectionObserver(handleObserver, options)
2526
observer.observe(elRef.value)

0 commit comments

Comments
 (0)