forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-button.js
More file actions
153 lines (117 loc) · 4.09 KB
/
test-button.js
File metadata and controls
153 lines (117 loc) · 4.09 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* MUI test react appbar library
* @module test/react-tests/test-appbar
*/
import assert from 'assert';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactUtils from 'react-dom/test-utils';
import { requestAnimationFrame } from '../../src/js/lib/util';
import jqLite from '../../src/js/lib/jqLite';
import Button from '../../src/react/button';
import { getShallowRendererOutput } from '../lib/react-helpers';
describe('react/button', function () {
it('renders properly', function () {
let result = getShallowRendererOutput(<Button>test</Button>);
assert.equal(result.type, 'button');
assert.equal(/\bmui-btn\b/.test(result.props.className), true);
assert.equal(result.props.children[0], 'test');
});
it('renders properly with addtional classNames', function () {
let result = getShallowRendererOutput(
<Button className="additional">
test
</Button>
);
assert.equal(/\badditional\b/.test(result.props.className), true);
});
it('renders properly with additional styles', function () {
let result = getShallowRendererOutput(
<Button style={{ additonal: 'style' }}>
test
</Button>
);
assert.equal(result.props.style.additonal, 'style');
});
it('supports colors', function () {
let result = getShallowRendererOutput(
<Button color="primary">test</Button>
);
assert.equal(/mui-btn--primary/.test(result.props.className), true);
});
it('supports variants', function () {
let result = getShallowRendererOutput(
<Button variant="raised">test</Button>
);
assert.equal(/mui-btn--raised/.test(result.props.className), true);
});
it('supports sizes', function () {
let result = getShallowRendererOutput(<Button size="large">test</Button>);
assert.equal(/mui-btn--large/.test(result.props.className), true);
});
it('supports type attribute', function () {
// check default
let result = getShallowRendererOutput(<Button>test</Button>);
assert.equal(result.props.type, null);
// check 'button' type
result = getShallowRendererOutput(
<Button type="button">
test
</Button>
);
assert.equal(result.props.type, 'button');
});
it('supports click, mouse and touch events', function () {
let executedEvents = [],
triggeredEvents = [];
// define callback
const fn = function (ev) { executedEvents.push(ev.type); };
// define props
const props = {
onClick: fn,
onMouseDown: fn,
onMouseUp: fn,
onMouseLeave: fn,
onTouchStart: fn,
onTouchEnd: fn
};
const node = ReactUtils.renderIntoDocument(
<Button { ...props }>test</Button>
);
// trigger events
let k, eventName, eventType;
for (k in props) {
// remove 'on' and lowercase first letter
eventName = k.charAt(2).toLowerCase() + k.substr(3, k.length);
eventType = eventName.toLowerCase();
// trigger event
ReactUtils.Simulate[eventName](node.buttonElRef, { type: eventType });
triggeredEvents.push(eventType);
}
// check that events were executed
assert.deepEqual(triggeredEvents, executedEvents);
});
it('renders ripples on click', function (done) {
let node = ReactUtils.renderIntoDocument(<Button>test</Button>),
buttonEl = node.buttonElRef,
rippleEl = node.rippleElRef;
// check state before click
assert.equal(node.state.ripple, null);
assert.equal(jqLite.hasClass(rippleEl, 'mui--is-visible'), false);
// trigger ripple
ReactUtils.Simulate.mouseDown(buttonEl);
// check state after click
assert.equal(jqLite.hasClass(rippleEl, 'mui--is-visible'), true);
assert.equal(jqLite.hasClass(rippleEl, 'mui--is-animating'), false);
// check animation
requestAnimationFrame(function () {
assert.equal(jqLite.hasClass(rippleEl, 'mui--is-animating'), true);
// remove ripple
ReactUtils.Simulate.mouseUp(node.buttonElRef);
requestAnimationFrame(function () {
assert.equal(jqLite.hasClass(rippleEl, 'mui--is-visible'), false);
done();
});
});
});
});