/
index.js
116 lines (88 loc) · 3.19 KB
/
index.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
import * as assert from 'power-assert';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as TestUtils from 'react-dom/test-utils';
import sinon from 'sinon';
import ViewportObserver from '../src';
describe('ViewportObserver', () => {
const div = document.createElement('div');
beforeEach(() => {
document.body.appendChild(div);
});
afterEach(() => {
ReactDOM.unmountComponentAtNode(div);
document.body.removeChild(div);
});
it('should be rendered with default values', () => {
const component = TestUtils.renderIntoDocument(<ViewportObserver />);
const node = TestUtils.findRenderedDOMComponentWithTag(component, 'div');
assert.notEqual(node, null);
assert.equal(node.className, '');
assert.equal(node.style.display, '');
});
it('should be rendered as specified `tagName`', () => {
const component = TestUtils.renderIntoDocument(<ViewportObserver tagName="span" />);
const node = TestUtils.findRenderedDOMComponentWithTag(component, 'span');
assert.notEqual(node, null);
});
it('should have expected `className`', () => {
const component = TestUtils.renderIntoDocument(<ViewportObserver className="a" />);
const node = TestUtils.findRenderedDOMComponentWithTag(component, 'div');
assert.equal(node.className, 'a');
});
it('should have expected CSS `display` property', () => {
const component = TestUtils.renderIntoDocument(<ViewportObserver display="inline-flex" />);
const node = TestUtils.findRenderedDOMComponentWithTag(component, 'div');
assert.equal(node.style.display, 'inline-flex');
});
it('should fire `onEnter` -> `onChange` -> `onLeave`', done => {
const onEnter = () => {};
const onChange = () => {};
const onLeave = () => {};
const spiedOnEnter = sinon.spy(onEnter);
const spiedOnChange = sinon.spy(onChange);
const spiedOnLeave = sinon.spy(onLeave);
const props = {
onEnter : spiedOnEnter,
onChange : spiedOnChange,
onLeave : spiedOnLeave
};
const component = ReactDOM.render(<ViewportObserver {...props} />, div);
const node = TestUtils.findRenderedDOMComponentWithTag(component, 'div');
document.body.style.height = '1000px';
node.style.marginTop = '10px';
node.style.height = '100px';
setTimeout(() => {
window.scrollTo(0, 10);
}, 100);
setTimeout(() => {
assert(spiedOnEnter.called);
assert(spiedOnChange.called);
assert(!spiedOnLeave.called);
}, 200);
setTimeout(() => {
window.scrollTo(0, 110);
}, 300);
setTimeout(() => {
assert(spiedOnEnter.called);
assert(spiedOnChange.called);
assert(!spiedOnLeave.called);
}, 400);
setTimeout(() => {
window.scrollTo(0, 111);
}, 500);
setTimeout(() => {
assert(spiedOnEnter.called);
assert(spiedOnChange.called);
assert(spiedOnLeave.called);
done();
}, 600);
});
it('should be disposed after unmount', () => {
const component = ReactDOM.render(<ViewportObserver tagName="span" />, div);
assert.notEqual(component.observer, null);
ReactDOM.unmountComponentAtNode(div);
assert.equal(component.observer, null);
});
});