Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OWA-13 added testing setup and basic test example for React OWA and Redux #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 --recursive",
"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')();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please put in some docs here? What does it do? And start from the openmrs license...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is just setup for jsdom to create realistic browser environment for testing.


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;