Skip to content

Commit 1da16d6

Browse files
committed
add testing
1 parent b7536a4 commit 1da16d6

File tree

6 files changed

+972
-62
lines changed

6 files changed

+972
-62
lines changed

karma.conf.js

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* eslint-disable max-len */
2+
3+
import webpackConfig from './webpack.config.babel';
4+
5+
export default (config) => {
6+
const configuration = {
7+
8+
// base path that will be used to resolve all patterns (eg. files, exclude)
9+
basePath: 'test',
10+
11+
12+
// frameworks to use
13+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
14+
frameworks: ['mocha'],
15+
16+
17+
// list of files / patterns to load in the browser
18+
files: [
19+
'index.html',
20+
'index.js',
21+
],
22+
23+
24+
// list of files to exclude
25+
exclude: [
26+
],
27+
28+
29+
// preprocess matching files before serving them to the browser
30+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
31+
preprocessors: {
32+
'index.js': ['webpack'],
33+
'index.html': ['html2js'],
34+
},
35+
36+
37+
webpack: {
38+
...webpackConfig,
39+
entry: {
40+
index: './src/index.js',
41+
},
42+
},
43+
44+
webpackMiddleware: { stats: 'errors-only' },
45+
46+
47+
// test results reporter to use
48+
// possible values: 'dots', 'progress'
49+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
50+
reporters: ['mocha'],
51+
52+
53+
// web server port
54+
port: 9876,
55+
56+
57+
// enable / disable colors in the output (reporters and logs)
58+
colors: true,
59+
60+
61+
// level of logging
62+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
63+
logLevel: config.LOG_ERROR,
64+
65+
66+
// enable / disable watching file and executing tests whenever any file changes
67+
autoWatch: true,
68+
69+
70+
// start these browsers
71+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
72+
browsers: ['Chrome'],
73+
74+
customLaunchers: {
75+
Chrome_travis_ci: {
76+
base: 'Chrome',
77+
flags: ['--no-sandbox']
78+
}
79+
},
80+
81+
82+
// Continuous Integration mode
83+
// if true, Karma captures browsers, runs the tests and exits
84+
// singleRun: true,
85+
86+
// Concurrency level
87+
// how many browser should be started simultaneous
88+
concurrency: Infinity
89+
};
90+
91+
if (process.env.TRAVIS) {
92+
configuration.browsers = ['Chrome_travis_ci'];
93+
}
94+
95+
config.set(configuration);
96+
};

package.json

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "0.0.0-alpha.7",
44
"description": "Bring react-native Animated to web",
55
"scripts": {
6+
"start": "npm run karma -- start",
7+
"test": "npm run karma -- start --single-run",
8+
"karma": "cross-env ANIMATED_TEST=true babel-node -- ./node_modules/.bin/karma",
69
"prebuild": "rimraf ./dist && mkdirp ./dist && rimraf ./lib && mkdirp ./lib",
710
"build:cjs": "babel src -d lib",
811
"build:umd": "webpack src dist/react-web-animated.js",
@@ -31,7 +34,15 @@
3134
"babel-preset-react": "^6.16.0",
3235
"babel-preset-stage-0": "^6.16.0",
3336
"babel-register": "^6.16.3",
37+
"cross-env": "^3.1.4",
38+
"karma": "^1.5.0",
39+
"karma-chrome-launcher": "^2.0.0",
40+
"karma-html2js-preprocessor": "^1.1.0",
41+
"karma-mocha": "^1.3.0",
42+
"karma-mocha-reporter": "^2.2.2",
43+
"karma-webpack": "^2.0.2",
3444
"mkdirp": "^0.5.1",
45+
"mocha": "^3.2.0",
3546
"react": "^15.3.2",
3647
"react-dom": "^15.3.2",
3748
"rimraf": "^2.5.4",

test/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div id="app"></div>

test/index.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
import assert from 'assert';
3+
import React, { Component } from 'react';
4+
import { render, findDOMNode } from 'react-dom';
5+
import Animated from '../src';
6+
7+
describe('library', function () {
8+
let rootNode;
9+
10+
beforeEach(function () {
11+
document.body.innerHTML = window.__html__['index.html'];
12+
rootNode = document.getElementById('app');
13+
});
14+
15+
it('<Animated.Div>', function () {
16+
const value = 'it works';
17+
18+
const App = () => (<Animated.Div>{value}</Animated.Div>);
19+
20+
render(<App />, rootNode);
21+
assert.equal(rootNode.textContent, value);
22+
});
23+
24+
it('Animated.Value()', function (done) {
25+
const defaultValue = 0;
26+
const updatedValue = 1;
27+
28+
class App extends Component {
29+
state = {
30+
anim: new Animated.Value(defaultValue),
31+
};
32+
33+
componentDidMount() {
34+
const { style } = findDOMNode(this);
35+
36+
assert.equal(parseInt(domNode.style.width, 10), defaultValue);
37+
this.state.anim.setValue(updatedValue);
38+
assert.equal(parseInt(domNode.style.width, 10), updatedValue);
39+
done();
40+
}
41+
42+
render() {
43+
return (
44+
<Animated.Div
45+
style={{
46+
width: this.state.anim,
47+
}}
48+
/>
49+
);
50+
}
51+
}
52+
53+
render(<App />, rootNode);
54+
});
55+
56+
});

webpack.config.babel.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import { resolve } from 'path';
33
import webpack from 'webpack';
44

5-
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
5+
const { env } = process;
66

7+
process.env.NODE_ENV = env.NODE_ENV || 'development';
8+
9+
const isTesting = !!env.ANIMATED_TEST;
710
const PROJECT_PATH = __dirname;
811
const inProject = (...args) => resolve(PROJECT_PATH, ...args);
912
const inSrc = inProject.bind(null, 'src');
@@ -18,7 +21,7 @@ export default {
1821
rules: [
1922
{
2023
test: /\.jsx?$/,
21-
include: srcDir,
24+
include: [srcDir, inProject('test')],
2225
loader: 'babel',
2326
query: {
2427
presets: [
@@ -47,7 +50,7 @@ export default {
4750
alias: { React: 'react' },
4851
},
4952
devtool: 'source-map',
50-
externals: {
53+
externals: isTesting ? {} : {
5154
react: 'React',
5255
'react-dom': 'ReactDOM',
5356
},

0 commit comments

Comments
 (0)