forked from jamespierce-rg/radium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-helpers.js
59 lines (49 loc) · 1.6 KB
/
test-helpers.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
import Color from 'color';
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import ShallowRenderer from 'react-test-renderer/shallow';
import TestUtils from 'react-dom/test-utils';
export function getRenderOutput(element) {
const renderer = new ShallowRenderer();
renderer.render(element);
return renderer.getRenderOutput();
}
class Wrapper extends Component {
render() {
return this.props.children;
}
}
export function renderFcIntoDocument(element) {
return TestUtils.renderIntoDocument(<Wrapper>{element}</Wrapper>);
}
export function getElement(output, tagName) {
return ReactDOM.findDOMNode(
TestUtils.findRenderedDOMComponentWithTag(output, tagName)
);
}
export function getElements(output, tagName) {
return TestUtils.scryRenderedDOMComponentsWithTag(output, tagName).map(
component => ReactDOM.findDOMNode(component)
);
}
function cleanCSS(css) {
return css.replace(/\s*\n\s*/g, '').replace(/\s*([{};:,])\s*/g, '$1');
}
export function expectCSS(styleElement, css) {
// strip newlines and excess whitespace from both to normalize browsers.
// IE9, for instance, does not include new lines in innerText.
// Also allows us to write our expected CSS cleanly, without worring about the
// format of the actual output.
expect(cleanCSS(styleElement.innerText)).to.equal(cleanCSS(css));
}
export function expectColor(actual, expected) {
expect(Color(actual).hex()).to.equal(Color(expected).hex());
}
export function createEsClass(renderFn) {
class Composed extends Component {
render() {
return renderFn() || <div />;
}
}
return Composed;
}