Skip to content

Commit 31ec1b4

Browse files
committed
./test/ is renamed to ./tests/
1 parent bf08aef commit 31ec1b4

File tree

6 files changed

+578
-0
lines changed

6 files changed

+578
-0
lines changed

tests/extendReactClass.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* eslint-disable max-nested-callbacks */
2+
3+
import {
4+
expect
5+
} from 'chai';
6+
7+
import React from 'react';
8+
import TestUtils from 'react-addons-test-utils';
9+
import jsdom from 'jsdom';
10+
import extendReactClass from './../src/extendReactClass';
11+
12+
describe('extendReactClass', () => {
13+
beforeEach(() => {
14+
global.document = jsdom.jsdom(`
15+
<!DOCTYPE html>
16+
<html>
17+
<head>
18+
</head>
19+
<body>
20+
</body>
21+
</html>
22+
`);
23+
24+
global.window = document.defaultView;
25+
});
26+
27+
context('using default styles', () => {
28+
it('exposes styles through styles property', (done) => {
29+
let Component,
30+
styles;
31+
32+
Component = class extends React.Component {
33+
render () {
34+
expect(this.props.styles).to.equal(styles);
35+
done();
36+
}
37+
};
38+
39+
styles = {
40+
foo: 'foo-1'
41+
};
42+
43+
Component = extendReactClass(Component, styles);
44+
45+
TestUtils.renderIntoDocument(<Component />);
46+
});
47+
it('does not affect the other instance properties', (done) => {
48+
let Component,
49+
styles;
50+
51+
Component = class extends React.Component {
52+
render () {
53+
expect(this.props.bar).to.equal('baz');
54+
done();
55+
}
56+
};
57+
58+
styles = {
59+
foo: 'foo-1'
60+
};
61+
62+
Component = extendReactClass(Component, styles);
63+
64+
TestUtils.renderIntoDocument(<Component bar='baz' />);
65+
});
66+
});
67+
68+
context('using explicit styles', () => {
69+
it('exposes styles through styles property', (done) => {
70+
let Component,
71+
styles;
72+
73+
Component = class extends React.Component {
74+
render () {
75+
expect(this.props.styles).to.equal(styles);
76+
done();
77+
}
78+
};
79+
80+
styles = {
81+
foo: 'foo-1'
82+
};
83+
84+
Component = extendReactClass(Component);
85+
86+
TestUtils.renderIntoDocument(<Component styles={styles} />);
87+
});
88+
});
89+
});

tests/linkClass.js

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
/* eslint-disable max-nested-callbacks */
2+
3+
import {
4+
expect
5+
} from 'chai';
6+
7+
import React from 'react';
8+
import TestUtils from 'react-addons-test-utils';
9+
import jsdom from 'jsdom';
10+
import linkClass from './../src/linkClass';
11+
12+
describe('linkClass', () => {
13+
context('ReactElement does not define styleName', () => {
14+
it('does not affect element properties', () => {
15+
expect(linkClass(<div></div>)).to.deep.equal(<div></div>);
16+
});
17+
18+
it('does not affect element properties with a single element child', () => {
19+
expect(linkClass(<div><p></p></div>)).to.deep.equal(<div><p></p></div>);
20+
});
21+
22+
it('does not affect element properties with a single text child', () => {
23+
expect(linkClass(<div>test</div>)).to.deep.equal(<div>test</div>);
24+
});
25+
26+
it('does not affect the className', () => {
27+
expect(linkClass(<div className='foo'></div>)).to.deep.equal(<div className='foo'></div>);
28+
});
29+
30+
xit('does not affect element with a single children when that children is contained in an array', () => {
31+
let outcome,
32+
subject;
33+
34+
subject = React.createElement('div', null, [
35+
React.createElement('p')
36+
]);
37+
outcome = React.createElement('div', null, [
38+
React.createElement('p')
39+
]);
40+
41+
expect(linkClass(subject)).to.deep.equal(outcome);
42+
});
43+
44+
xit('does not affect element with multiple children', () => {
45+
// Using array instead of object causes the following error:
46+
// Warning: Each child in an array or iterator should have a unique "key" prop.
47+
// Check the render method of _class. See https://fb.me/react-warning-keys for more information.
48+
// @see https://github.com/facebook/react/issues/4723#issuecomment-135555277
49+
// expect(linkClass(<div><p></p><p></p></div>)).to.deep.equal(<div><p></p><p></p></div>);
50+
51+
let outcome,
52+
subject;
53+
54+
subject = React.createElement('div', null, [
55+
React.createElement('p'),
56+
React.createElement('p')
57+
]);
58+
outcome = React.createElement('div', null, [
59+
React.createElement('p'),
60+
React.createElement('p')
61+
]);
62+
63+
expect(linkClass(subject)).to.deep.equal(outcome);
64+
});
65+
});
66+
67+
context('called with null instead of ReactElement', () => {
68+
it('returns null', () => {
69+
let subject;
70+
71+
subject = linkClass(null);
72+
73+
expect(subject).to.equal(null);
74+
});
75+
});
76+
77+
context('styleName matches an existing CSS module', () => {
78+
context('when a descendant element has styleName', () => {
79+
it('assigns a generated className', () => {
80+
let subject;
81+
82+
subject = <div>
83+
<p styleName='foo'></p>
84+
</div>;
85+
86+
subject = linkClass(subject, {
87+
foo: 'foo-1'
88+
});
89+
90+
expect(subject.props.children.props.className).to.equal('foo-1');
91+
});
92+
});
93+
context('when multiple descendant elements have styleName', () => {
94+
it('assigns a generated className', () => {
95+
let subject;
96+
97+
subject = <div>
98+
<p styleName='foo'></p>
99+
<p styleName='bar'></p>
100+
</div>;
101+
102+
subject = linkClass(subject, {
103+
foo: 'foo-1',
104+
bar: 'bar-1'
105+
});
106+
107+
expect(subject.props.children[0].props.className).to.equal('foo-1');
108+
expect(subject.props.children[1].props.className).to.equal('bar-1');
109+
});
110+
});
111+
context('when ReactElement does not have an existing className', () => {
112+
it('uses the generated class name to set the className property', () => {
113+
let subject;
114+
115+
subject = <div styleName='foo'></div>;
116+
117+
subject = linkClass(subject, {
118+
foo: 'foo-1'
119+
});
120+
121+
expect(subject.props.className).to.deep.equal('foo-1');
122+
});
123+
});
124+
context('when ReactElement has an existing className', () => {
125+
it('appends the generated class name to the className property', () => {
126+
let subject;
127+
128+
subject = <div className='foo' styleName='bar'></div>;
129+
130+
subject = linkClass(subject, {
131+
bar: 'bar-1'
132+
});
133+
134+
expect(subject.props.className).to.deep.equal('foo bar-1');
135+
});
136+
});
137+
});
138+
139+
context('styleName includes multiple whitespace characters', () => {
140+
it('resolves CSS modules', () => {
141+
let subject;
142+
143+
subject = <div>
144+
<p styleName=' foo bar '></p>
145+
</div>;
146+
147+
subject = linkClass(subject, {
148+
foo: 'foo-1',
149+
bar: 'bar-1'
150+
}, {
151+
allowMultiple: true
152+
});
153+
154+
expect(subject.props.children.props.className).to.equal('foo-1 bar-1');
155+
});
156+
});
157+
158+
describe('options.allowMultiple', () => {
159+
context('when multiple module names are used', () => {
160+
context('when false', () => {
161+
it('throws an error', () => {
162+
expect(() => {
163+
linkClass(<div styleName='foo bar'></div>, {}, {allowMultiple: false});
164+
}).to.throw(Error, 'ReactElement styleName property defines multiple module names ("foo bar").');
165+
});
166+
});
167+
context('when true', () => {
168+
it('appends a generated class name for every referenced CSS module', () => {
169+
let subject;
170+
171+
subject = <div styleName='foo bar'></div>;
172+
173+
subject = linkClass(subject, {
174+
foo: 'foo-1',
175+
bar: 'bar-1'
176+
}, {allowMultiple: true});
177+
178+
expect(subject.props.className).to.deep.equal('foo-1 bar-1');
179+
});
180+
});
181+
});
182+
});
183+
184+
describe('options.errorWhenNotFound', () => {
185+
context('when styleName does not match an existing CSS module', () => {
186+
context('when false', () => {
187+
it('ignores the missing CSS module', () => {
188+
let subject;
189+
190+
subject = <div styleName='foo'></div>;
191+
192+
subject = linkClass(subject, {}, {errorWhenNotFound: false});
193+
194+
expect(subject.props.className).to.be.an('undefined');
195+
});
196+
});
197+
context('when is true', () => {
198+
it('throws an error', () => {
199+
expect(() => {
200+
linkClass(<div styleName='foo'></div>, {}, {errorWhenNotFound: true});
201+
}).to.throw(Error, '"foo" CSS module is undefined.');
202+
});
203+
});
204+
});
205+
});
206+
207+
context('when ReactElement includes ReactComponent', () => {
208+
let Foo,
209+
nodeList;
210+
211+
beforeEach(() => {
212+
global.document = jsdom.jsdom(`
213+
<!DOCTYPE html>
214+
<html>
215+
<head>
216+
</head>
217+
<body>
218+
</body>
219+
</html>
220+
`);
221+
222+
global.window = document.defaultView;
223+
224+
Foo = class extends React.Component {
225+
render () {
226+
return <div styleName='foo'>Hello</div>;
227+
}
228+
};
229+
230+
nodeList = TestUtils.renderIntoDocument(linkClass(<div styleName='foo'><Foo /></div>, {foo: 'foo-1'}));
231+
});
232+
it('processes ReactElement nodes', () => {
233+
expect(nodeList.className).to.equal('foo-1');
234+
});
235+
it('does not process ReactComponent nodes', () => {
236+
expect(nodeList.firstChild.className).to.equal('');
237+
});
238+
});
239+
});

tests/makeConfiguration.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-disable max-nested-callbacks */
2+
3+
import {
4+
expect
5+
} from 'chai';
6+
7+
import makeConfiguration from './../src/makeConfiguration';
8+
9+
describe('makeConfiguration', () => {
10+
describe('when using default configuration', () => {
11+
let configuration;
12+
13+
beforeEach(() => {
14+
configuration = makeConfiguration();
15+
});
16+
describe('allowMultiple property', () => {
17+
it('defaults to false', () => {
18+
expect(configuration.allowMultiple).to.equal(false);
19+
});
20+
});
21+
describe('errorWhenNotFound property', () => {
22+
it('defaults to true', () => {
23+
expect(configuration.errorWhenNotFound).to.equal(true);
24+
});
25+
});
26+
});
27+
describe('when unknown property is provided', () => {
28+
it('throws an error', () => {
29+
expect(() => {
30+
makeConfiguration({
31+
unknownProperty: true
32+
});
33+
}).to.throw(Error, 'Unknown configuration property "unknownProperty".');
34+
});
35+
});
36+
it('does not mutate user configuration', () => {
37+
let userConfiguration;
38+
39+
userConfiguration = {};
40+
41+
makeConfiguration(userConfiguration);
42+
43+
expect(userConfiguration).to.deep.equal({});
44+
});
45+
});

tests/mocha.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--compilers js:babel/register

0 commit comments

Comments
 (0)