Skip to content

Commit

Permalink
OWA-13 added testing setup and basic test example for React and React…
Browse files Browse the repository at this point in the history
…+Redux
  • Loading branch information
AdamGrzybkowski committed Jan 23, 2017
1 parent 932db18 commit ed96758
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ module.exports = generators.Base.extend({
);
},

babelrc: function() {
if (this.includeReact) {
this.fs.copyTpl(
this.templatePath('babelrc'),
this.destinationPath('.babelrc')
);
}
},

scripts: function() {
// AngularJS
if (this.includeAngular) {
Expand Down Expand Up @@ -296,6 +305,10 @@ module.exports = generators.Base.extend({
this.templatePath('scripts/react/components/App.jsx'),
this.destinationPath('app/js/components/App.jsx')
);
this.fs.copyTpl(
this.templatePath('scripts/react/test'),
this.destinationPath('app/test')
);

if (this.includeRedux) {
this.fs.copyTpl(
Expand Down
14 changes: 14 additions & 0 deletions app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
"babel-preset-es2015": "^6.1.18",
<% if(includeReact === true) { %>
"babel-preset-react": "^6.16.0",
"sinon": "^1.17.7",
"chai": "^3.5.0",
"enzyme": "^2.7.0",
"jsdom": "^9.9.1",
"react-addons-test-utils": "^15.4.2",
"mocha": "^3.2.0",
<% if(includeRedux === true) { %>
"redux-mock-store": "^1.2.1",
<% } %>
<% } %>
"browser-sync": "^2.11.1",
"browser-sync-webpack-plugin": "^1.0.1",
Expand All @@ -61,7 +70,12 @@
"build:prod": "npm run test && npm run build",
"build:deploy": "webpack --progress --colors --mode=deploy --target=web",
"watch": "webpack --progress --colors --watch --mode=deploy --target=web",
<% if(includeReact === true) { %>
"test": "mocha app/test/helpers/browser.js app/test/*.spec.js",
"test:watch": "npm run test -- -w"
<% } else { %>
"test": "echo \"Error: no test specified\""
<% } %>
},
"keywords": [
"OpenMRS",
Expand Down
3 changes: 3 additions & 0 deletions app/templates/babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["react", "es2015"]
}
23 changes: 23 additions & 0 deletions app/templates/scripts/react/test/components/App.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http:https://license.openmrs.org
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
import React from 'react';
import { mount, shallow } from 'enzyme';
import {expect} from 'chai';

import App from '../../js/components/App';

describe('<App/>', function () {
it('should say Hello!', function () {
const wrapper = shallow(<App/>);
expect(wrapper.find('h1')).to.have.length(1);
});
});
31 changes: 31 additions & 0 deletions app/templates/scripts/react/test/helpers/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http:https://license.openmrs.org
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
require('babel-register')();

var jsdom = require('jsdom').jsdom;

var exposedProperties = ['window', 'navigator', 'document'];

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});

global.navigator = {
userAgent: 'node.js'
};

documentRef = document;

0 comments on commit ed96758

Please sign in to comment.