-
Notifications
You must be signed in to change notification settings - Fork 2k
/
EnterLeaveCounter.spec.ts
48 lines (43 loc) · 1.48 KB
/
EnterLeaveCounter.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { EnterLeaveCounter } from '../EnterLeaveCounter.js'
describe('EnterLeaveCounter', () => {
const parentDiv = document.createElement('div')
parentDiv.setAttribute('id', 'parent')
const childDiv = document.createElement('div')
childDiv.setAttribute('id', 'child')
parentDiv.appendChild(childDiv)
const isNodeInDocument = () => {
return true
}
it('should check if the element is entered', () => {
const enterLeaveCounter: EnterLeaveCounter = new EnterLeaveCounter(
isNodeInDocument,
)
const hasEntered = enterLeaveCounter.enter(parentDiv)
expect(hasEntered).toEqual(true)
})
it('should not falsely check child elements as new element entered', () => {
const enterLeaveCounter: EnterLeaveCounter = new EnterLeaveCounter(
isNodeInDocument,
)
enterLeaveCounter.enter(parentDiv)
const hasEntered = enterLeaveCounter.enter(childDiv)
expect(hasEntered).toEqual(false)
})
it('should check if leave element was entered', () => {
const enterLeaveCounter: EnterLeaveCounter = new EnterLeaveCounter(
isNodeInDocument,
)
enterLeaveCounter.enter(parentDiv)
const testElem = document.createElement('p')
const hasLeft = enterLeaveCounter.leave(testElem)
expect(hasLeft).toEqual(false)
})
it('should leave element if it is entered', () => {
const enterLeaveCounter: EnterLeaveCounter = new EnterLeaveCounter(
isNodeInDocument,
)
enterLeaveCounter.enter(parentDiv)
const hasLeft = enterLeaveCounter.leave(parentDiv)
expect(hasLeft).toEqual(true)
})
})