Skip to content

Commit de867a7

Browse files
committed
Add test for style example
1 parent f1c1444 commit de867a7

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

examples/style/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class App extends Component {
1010

1111
<p>
1212
If you want to apply a style defined in external CSS file,<br />
13-
you can give <Origin name="id-tip" className="target"><code>class</code></Origin> and <Origin name="class-tip" className="target"><code>id</code></Origin> attributes to the DIV element of a tooltip.
13+
you can give <Origin name="id-tip" className="target origin-id"><code>id</code></Origin> and <Origin name="class-tip" className="target origin-class"><code>class</code></Origin> attributes to the DIV element of a tooltip.
1414
</p>
1515

1616
<Tooltip name="id-tip" id="my-id">

karma.conf.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@ module.exports = function(config) {
33
basePath: '',
44
frameworks: ['mocha', 'sinon'],
55
files: [
6-
'tests/feature/*.js'
6+
'tests/feature/*.js',
7+
'examples/*.css'
78
],
89
exclude: [],
910
plugins: [
1011
'karma-phantomjs-launcher',
1112
'karma-mocha',
1213
'karma-mocha-reporter',
1314
'karma-sinon',
14-
'karma-webpack'
15+
'karma-webpack',
16+
'karma-html2js-preprocessor'
1517
],
1618
preprocessors: {
1719
'tests/feature/*.js': ['webpack'],
20+
'examples/*.css': ['html2js']
1821
},
1922
webpack: {
2023
devtool: 'inline-source-map',

tests/feature/style.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import assert from 'power-assert';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom';
4+
import TestUtils from 'react-addons-test-utils';
5+
import { Provider } from 'react-redux';
6+
import { Tooltip, Origin, utils } from '../../src/index';
7+
import App from '../../examples/style/app';
8+
import store from '../../examples/common/store';
9+
import { firstComponent, getStyleValue, getComputedStyleValue } from '../helpers';
10+
11+
const { position } = utils;
12+
13+
describe('Style Example', () => {
14+
before(() => {
15+
document.body.innerHTML += '<div id="container" style="position:absolute;top:0;left:0;"></div>';
16+
document.body.innerHTML += `<style type="text/css">${window.__html__['examples/style.css']}</style>`;
17+
});
18+
19+
let tree;
20+
beforeEach(() => {
21+
tree = ReactDOM.render(
22+
<Provider store={store}>
23+
<App />
24+
</Provider>,
25+
document.getElementById('container'));
26+
});
27+
28+
afterEach(() => {
29+
ReactDOM.unmountComponentAtNode(document.getElementById('container'));
30+
});
31+
32+
describe('id tooltip', () => {
33+
it('should be colored', () => {
34+
const origin = firstComponent(tree, Origin.WrappedComponent, { name: 'id-tip' }).refs.wrapper;
35+
TestUtils.Simulate.mouseEnter(origin);
36+
37+
const tooltip = firstComponent(tree, Tooltip.WrappedComponent, { name: 'id-tip' }).refs.tooltip;
38+
assert(getComputedStyleValue(tooltip, 'background-color') === 'rgb(255, 205, 210)');
39+
assert(tooltip.innerText === 'This is a id tooltip.');
40+
41+
const border = firstComponent(tree, Tooltip.WrappedComponent, { name: 'id-tip' }).refs.border;
42+
assert(getComputedStyleValue(border, 'border-color') === 'rgb(255, 205, 210) rgba(0, 0, 0, 0) rgba(0, 0, 0, 0)');
43+
});
44+
});
45+
46+
describe('class tooltip', () => {
47+
it('should be colored', () => {
48+
const origin = firstComponent(tree, Origin.WrappedComponent, { name: 'class-tip' }).refs.wrapper;
49+
TestUtils.Simulate.mouseEnter(origin);
50+
51+
const tooltip = firstComponent(tree, Tooltip.WrappedComponent, { name: 'class-tip' }).refs.tooltip;
52+
assert(getComputedStyleValue(tooltip, 'background-color') === 'rgb(187, 222, 251)');
53+
assert(tooltip.innerText === 'This is a class tooltip.');
54+
55+
const border = firstComponent(tree, Tooltip.WrappedComponent, { name: 'class-tip' }).refs.border;
56+
assert(getComputedStyleValue(border, 'border-color') === 'rgb(187, 222, 251) rgba(0, 0, 0, 0) rgba(0, 0, 0, 0)');
57+
});
58+
});
59+
});

tests/helpers.js

+5
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ export function getStyleValue(element, propName) {
2828
parser.cssText = element.getAttribute('style');
2929
return parser.getPropertyValue(propName);
3030
}
31+
32+
export function getComputedStyleValue(element, propName) {
33+
const style = getComputedStyle(element);
34+
return style.getPropertyValue(propName);
35+
}

0 commit comments

Comments
 (0)