This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.jsx
99 lines (78 loc) · 2.83 KB
/
test.jsx
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
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import FastClick from './index.jsx';
import ReactTestUtils from 'react-dom/test-utils';
import assert from 'assert';
const { Simulate } = ReactTestUtils;
class ChangeOnClick extends Component {
state = {
clicked: false
};
clicked() {
this.setState({ clicked: true });
}
render() {
const { clicked } = this.state;
return (
<FastClick>
<div id="showclicked" onClick={() => this.clicked()}>{clicked ? 'clicked' : 'notclicked'}</div>
</FastClick>
);
}
}
describe('react-fastclick-alt', function () {
var test, showClicked, sc;
beforeEach(function () {
document.body.innerHTML = '<div id="test"></div>';
test = document.getElementById('test');
ReactDOM.render(<ChangeOnClick/>, test);
showClicked = document.getElementById('showclicked');
sc = showClicked;
});
var wasClicked = function () {
return showClicked.innerText === 'clicked';
};
it('base case, nothing happens and the element is not clicked', function () {
assert(!wasClicked());
});
it('shows clicked when actually clicked', function () {
assert(!wasClicked());
showClicked.click();
assert(wasClicked());
});
it('shows clicked when tapped', function () {
assert(!wasClicked());
var ti = { identifier: 0, screenX: 0, screenY: 0, target: sc };
Simulate.touchStart(sc, { touches: [ ti ], targetTouches: [ ti ] });
Simulate.touchEnd(sc, { touches: [], targetTouches: [], changedTouches: [ ti ] });
assert(wasClicked());
});
it('shows clicked when tapped for 100ms', function (done) {
assert(!wasClicked());
var ti = { identifier: 0, screenX: 0, screenY: 0, target: sc };
Simulate.touchStart(sc, { touches: [ ti ], targetTouches: [ ti ] });
setTimeout(function () {
Simulate.touchEnd(sc, { touches: [], targetTouches: [], changedTouches: [ ti ] });
if (!wasClicked()) {
throw Error('was not clicked');
}
done();
}, 100);
});
it('clicked when finger moves for <15px', function () {
assert(!wasClicked());
var ti = { identifier: 0, screenX: 0, screenY: 0, target: sc };
var te = { identifier: 0, screenX: 10, screenY: 0, target: sc };
Simulate.touchStart(sc, { touches: [ ti ], targetTouches: [ ti ] });
Simulate.touchEnd(sc, { touches: [], targetTouches: [], changedTouches: [ te ] });
assert(wasClicked());
});
it('not clicked when finger moves for >15px', function () {
assert(!wasClicked());
var ti = { identifier: 0, screenX: 0, screenY: 0, target: sc };
var te = { identifier: 0, screenX: 12, screenY: 12, target: sc };
Simulate.touchStart(sc, { touches: [ ti ], targetTouches: [ ti ] });
Simulate.touchEnd(sc, { touches: [], targetTouches: [], changedTouches: [ te ] });
assert(!wasClicked());
});
});