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
117
118
119
120
121
122
123
124
125
126
127
128
129
|
// @flow
import { expect } from 'chai';
import * as React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils, { Simulate } from 'react-dom/test-utils';
import Switch from '../../app/components/Switch';
describe('components/Switch', () => {
let container: ?HTMLElement;
function renderIntoDocument(instance: React.Element<*>): React.Component<*, *> {
if (container) {
throw new Error('Unmount previously rendered component first.');
}
container = document.createElement('div');
if (!document.documentElement) {
throw new Error('document.documentElement cannot be null.');
}
document.documentElement.appendChild(container);
return ReactDOM.render(instance, container);
}
// unmount container and clean up DOM
afterEach(() => {
if (container) {
ReactDOM.unmountComponentAtNode(container);
container = null;
}
});
it('should switch on', (done) => {
const onChange = (isOn) => {
expect(isOn).to.be.true;
done();
};
const component = renderIntoDocument(<Switch isOn={false} onChange={onChange} />);
const domNode = ReactTestUtils.findRenderedDOMComponentWithTag(component, 'input');
// See: https://github.com/facebook/flow/pull/5841
if (domNode) {
Simulate.mouseDown(domNode, { clientX: 100, clientY: 0 });
Simulate.mouseUp(domNode, { clientX: 100, clientY: 0 });
Simulate.change(domNode, { target: { checked: true } });
}
});
it('should switch off', (done) => {
const onChange = (isOn) => {
expect(isOn).to.be.false;
done();
};
const component = renderIntoDocument(<Switch isOn={true} onChange={onChange} />);
const domNode = ReactTestUtils.findRenderedDOMComponentWithTag(component, 'input');
// See: https://github.com/facebook/flow/pull/5841
if (domNode) {
Simulate.mouseDown(domNode, { clientX: 100, clientY: 0 });
Simulate.mouseUp(domNode, { clientX: 100, clientY: 0 });
Simulate.change(domNode, { target: { checked: false } });
}
});
it('should handle left to right swipe', (done) => {
const onChange = (isOn) => {
expect(isOn).to.be.true;
done();
};
const component = renderIntoDocument(<Switch isOn={false} onChange={onChange} />);
const domNode = ReactTestUtils.findRenderedDOMComponentWithTag(component, 'input');
// See: https://github.com/facebook/flow/pull/5841
if (domNode) {
Simulate.mouseDown(domNode, { clientX: 100, clientY: 0 });
}
// Switch listens to events on document
document.dispatchEvent(new MouseEvent('mousemove', { clientX: 150, clientY: 0 }));
document.dispatchEvent(new MouseEvent('mouseup', { clientX: 150, clientY: 0 }));
});
it('should handle right to left swipe', (done) => {
const onChange = (isOn) => {
expect(isOn).to.be.false;
done();
};
const component = renderIntoDocument(<Switch isOn={true} onChange={onChange} />);
const domNode = ReactTestUtils.findRenderedDOMComponentWithTag(component, 'input');
// See: https://github.com/facebook/flow/pull/5841
if (domNode) {
Simulate.mouseDown(domNode, { clientX: 150, clientY: 0 });
}
// Switch listens to events on document
document.dispatchEvent(new MouseEvent('mousemove', { clientX: 100, clientY: 0 }));
document.dispatchEvent(new MouseEvent('mouseup', { clientX: 100, clientY: 0 }));
});
it('should timeout when user holds knob for too long without moving', (done) => {
const onChange = () => {
throw new Error('onChange should not be called on timeout.');
};
const component = renderIntoDocument(<Switch isOn={false} onChange={onChange} />);
const domNode = ReactTestUtils.findRenderedDOMComponentWithTag(component, 'input');
// See: https://github.com/facebook/flow/pull/5841
if (domNode) {
Simulate.mouseDown(domNode, { clientX: 100, clientY: 0 });
}
setTimeout(() => {
// Switch listens to events on document
document.dispatchEvent(new MouseEvent('mouseup', { clientX: 100, clientY: 0 }));
try {
// See: https://github.com/facebook/flow/pull/5841
if (domNode) {
// should not trigger onChange()
Simulate.change(domNode);
}
done();
} catch (e) {
done(e);
}
}, 1000);
});
});
|