-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
/
Badge.test.js
184 lines (156 loc) · 7.11 KB
/
Badge.test.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React from 'react';
import { assert } from 'chai';
import { createMount, getClasses } from '@material-ui/core/test-utils';
import describeConformance from '../test-utils/describeConformance';
import Badge from './Badge';
import { act } from 'react-dom/test-utils';
function findBadge(wrapper) {
return wrapper.find('span').at(1);
}
describe('<Badge />', () => {
let mount;
let classes;
const defaultProps = {
children: <div className="unique">Hello World</div>,
badgeContent: 10,
};
before(() => {
mount = createMount({ strict: true });
classes = getClasses(<Badge {...defaultProps} />);
});
after(() => {
mount.cleanUp();
});
describeConformance(
<Badge>
<div />
</Badge>,
() => ({
classes,
inheritComponent: 'span',
mount,
refInstanceof: window.HTMLSpanElement,
testComponentPropWith: 'div',
}),
);
it('renders children and badgeContent', () => {
const children = <div id="child" />;
const badge = <div id="badge" />;
const wrapper = mount(<Badge badgeContent={badge}>{children}</Badge>);
assert.strictEqual(wrapper.contains(children), true);
assert.strictEqual(wrapper.contains(badge), true);
});
it('renders children and overwrite badge class', () => {
const badgeClassName = 'testBadgeClassName';
const wrapper = mount(<Badge {...defaultProps} classes={{ badge: badgeClassName }} />);
assert.strictEqual(findBadge(wrapper).hasClass(badgeClassName), true);
});
it('renders children', () => {
const wrapper = mount(<Badge className="testClassName" {...defaultProps} />);
assert.strictEqual(wrapper.contains(defaultProps.children), true);
});
it('retains text, color and variant while invisible for disappearing transition', () => {
const wrapper = mount(<Badge {...defaultProps} color="primary" variant="dot" />);
act(() => {
wrapper.setProps({ badgeContent: 0, color: 'secondary', variant: 'standard' });
});
assert.strictEqual(findBadge(wrapper).text(), '');
assert.strictEqual(findBadge(wrapper).hasClass(classes.colorPrimary), true);
assert.strictEqual(findBadge(wrapper).hasClass(classes.dot), true);
act(() => {
wrapper.setProps({ showZero: true });
});
assert.strictEqual(findBadge(wrapper).text(), '0');
});
describe('prop: color', () => {
it('should have the colorPrimary class when color="primary"', () => {
const wrapper = mount(<Badge {...defaultProps} color="primary" />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.colorPrimary), true);
});
it('should have the colorSecondary class when color="secondary"', () => {
const wrapper = mount(<Badge {...defaultProps} color="secondary" />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.colorSecondary), true);
});
it('should have the colorError class when color="error"', () => {
const wrapper = mount(<Badge {...defaultProps} color="error" />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.colorError), true);
});
});
describe('prop: invisible', () => {
it('should default to false', () => {
const wrapper = mount(<Badge {...defaultProps} />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), false);
});
it('should render without the invisible class when set to false', () => {
const wrapper = mount(<Badge {...defaultProps} invisible={false} />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), false);
});
it('should render with the invisible class when set to true', () => {
const wrapper = mount(<Badge {...defaultProps} invisible />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), true);
});
it('should render with the invisible class when empty and not dot', () => {
let wrapper;
wrapper = mount(<Badge {...defaultProps} badgeContent={null} />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), true);
wrapper = mount(<Badge {...defaultProps} badgeContent={undefined} />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), true);
wrapper = mount(<Badge {...defaultProps} badgeContent={undefined} variant="dot" />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), false);
});
});
describe('prop: showZero', () => {
it('should default to false', () => {
const wrapper = mount(<Badge {...defaultProps} badgeContent={0} />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), true);
});
it('should render without the invisible class when false and badgeContent is not 0', () => {
const wrapper = mount(<Badge {...defaultProps} showZero />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), false);
});
it('should render without the invisible class when true and badgeContent is 0', () => {
const wrapper = mount(<Badge {...defaultProps} badgeContent={0} showZero />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), false);
});
it('should render with the invisible class when false and badgeContent is 0', () => {
const wrapper = mount(<Badge {...defaultProps} badgeContent={0} showZero={false} />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.invisible), true);
});
});
describe('prop: variant', () => {
it('should default to standard', () => {
const wrapper = mount(<Badge {...defaultProps} />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.badge), true);
assert.strictEqual(findBadge(wrapper).hasClass(classes.dot), false);
});
it('should render with the standard class when variant="standard"', () => {
const wrapper = mount(<Badge {...defaultProps} variant="standard" />);
assert.strictEqual(findBadge(wrapper).hasClass(classes.badge), true);
assert.strictEqual(findBadge(wrapper).hasClass(classes.dot), false);
});
it('should not render badgeContent when variant="dot"', () => {
const wrapper = mount(<Badge {...defaultProps} variant="dot" />);
assert.strictEqual(findBadge(wrapper).text(), '');
assert.strictEqual(findBadge(wrapper).hasClass(classes.badge), true);
assert.strictEqual(findBadge(wrapper).hasClass(classes.dot), true);
});
});
describe('prop: max', () => {
it('should default to 99', () => {
const wrapper = mount(<Badge {...defaultProps} badgeContent={100} />);
assert.strictEqual(findBadge(wrapper).text(), '99+');
});
it('should cap badgeContent', () => {
const wrapper = mount(<Badge {...defaultProps} badgeContent={1000} max={999} />);
assert.strictEqual(findBadge(wrapper).text(), '999+');
});
it('should not cap if badgeContent and max are equal', () => {
const wrapper = mount(<Badge {...defaultProps} badgeContent={1000} max={1000} />);
assert.strictEqual(findBadge(wrapper).text(), '1000');
});
it('should not cap if badgeContent is lower than max', () => {
const wrapper = mount(<Badge {...defaultProps} badgeContent={50} max={1000} />);
assert.strictEqual(findBadge(wrapper).text(), '50');
});
});
});