Permalink
Browse files

unit test for ReportAsTableView.jsx

  • Loading branch information...
JudeNiroshan committed Aug 13, 2017
1 parent 5fc3c95 commit 6fc2fd1d2cc32247b93ee0f3d8dbd1a76e392709
@@ -0,0 +1,11 @@
import Response from 'http-response-object';
export const fakeRequestLibrary = (requestUrl, requestOptions, shouldPass = true, responseData = null) => {
return new Promise((resolve, reject) => {
if (shouldPass) {
resolve(new Response(200, {}, responseData || { message: `You called ${requestUrl}` }, requestUrl));
} else {
reject(new Response(404, {}, responseData || { message: `The page at ${requestUrl} was not found` }, requestUrl));
}
});
};
@@ -0,0 +1,66 @@
import React from 'react';
import renderer from 'react-test-renderer';
import sinon from 'sinon';
import { fakeRequestLibrary } from '../../../__mocks__/fakeRequestLibrary';
import ReportAsTableView from '../../../components/reports/common/ReportAsTableView';
const SERVER_URL = "http://localhost:8080/openmrs/ws/rest/v1/reportingrest/reportdata/e451ae04-4881-11e7-a919-92ebcb67fe33";
const FAKE_RESPONSE = {
dataSets: [
{
metadata: {
columns: [
{
name: "username",
label: "username"
},
{
name: "date_created",
label: "date_created"
}
]
},
rows: [
{
date_created: "2010-04-26T13:25:00.000+0530",
username: "daemon"
},
{
date_created: "2017-06-08T21:37:18.000+0530",
username: "clerk"
},
{
date_created: "2017-07-08T21:37:18.000+0530",
username: "nurse"
},
{
date_created: "2017-07-08T21:37:19.000+0530",
username: "doctor"
},
{
date_created: "2017-07-08T21:37:18.000+0530",
username: "sysadmin"
}
]
}
]
};
describe('<ReportAsTableView /> ', () => {
it('renders correctly with success data received from server', () => {
const params = {
"startDate": "2017-05-05",
"endDate": "2017-10-05"
};
var rendered = renderer.create(
<ReportAsTableView reportUUID="e451ae04-4881-11e7-a919-92ebcb67fe33"
reportParameters={params}
fetchData={fakeRequestLibrary('www.openmrs-fake-server.org', {}, true, FAKE_RESPONSE)} />
);
expect(rendered.toJSON()).toMatchSnapshot();
});
});
@@ -1,21 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ReportAsTableView renders correctly renders correctly 1`] = `
<div>
<h1>
</h1>
exports[`<ReportAsTableView /> renders correctly with success data received from server 1`] = `
<div
style={
Object {
"border": "1px solid black",
}
}
>
<div
className="table-container"
className="NotFoundWrapper"
>
<table
className="reportTable"
<div
className="attentionSign"
>
<thead>
<tr />
</thead>
<tbody />
</table>
<img
src="./warning.png"
width="300"
/>
</div>
<div>
No Data found
<span>
for
Report Table
</span>
</div>
</div>
</div>
`;
@@ -4,12 +4,15 @@ import GroupByDateChart from '../common/GroupByDateChart';
import BasicXYChart from '../common/BasicXYChart';
import ReportTitle from '../common/ReportTitle';
import ListOfUsersInputBox from './ListOfUsersInputBox';
import { fakeRequestLibrary } from '../../../__mocks__/fakeRequestLibrary';
/**
* Display the result of List of Users report
*/
class ListOfUsers extends Component {
constructor() {
super();
this.state = {
@@ -20,6 +23,51 @@ class ListOfUsers extends Component {
this.getReportUUID = this.getReportUUID.bind(this);
this.eventListenerForParameter = this.eventListenerForParameter.bind(this);
this.FAKE_RESPONSE = this.FAKE_RESPONSE.bind(this);
}
FAKE_RESPONSE() {
return {
dataSets: [
{
metadata: {
columns: [
{
name: "username",
label: "username"
},
{
name: "date_created",
label: "date_created"
}
]
},
rows: [
{
date_created: "2010-04-26T13:25:00.000+0530",
username: "daemon"
},
{
date_created: "2017-06-08T21:37:18.000+0530",
username: "clerk"
},
{
date_created: "2017-07-08T21:37:18.000+0530",
username: "nurse"
},
{
date_created: "2017-07-08T21:37:19.000+0530",
username: "doctor"
},
{
date_created: "2017-07-08T21:37:18.000+0530",
username: "sysadmin"
}
]
}
]
}
}
getReportUUID() {
@@ -39,9 +87,11 @@ class ListOfUsers extends Component {
return (
<div>
<ReportTitle heading="List of Users" />
<ListOfUsersInputBox listener={this.eventListenerForParameter}/>
<ListOfUsersInputBox listener={this.eventListenerForParameter} />
<ReportAsTableView reportUUID={this.getReportUUID()}
reportParameters={this.state.parameters} />
reportParameters={this.state.parameters}
fetchData={fakeRequestLibrary('www.example.com', {}, true, this.FAKE_RESPONSE())} />
<GroupByDateChart reportUUID={this.getReportUUID()}
reportParameters={this.state.parameters} groupBy='month' />
@@ -36,10 +36,20 @@ class ReportAsTableView extends Component {
}
init(params) {
new ApiHelper().post(ReportConstants.REPORT_REQUEST + this.props.reportUUID, params)
if(this.props.fetchData != null){
//Test Path
this.props.fetchData
.then((response) => {
console.log('>>>>>'+JSON.stringify(response.body));
this.resolveResponse(response.body);
});
}else{
new ApiHelper().post(ReportConstants.REPORT_REQUEST + this.props.reportUUID, params)
.then((response) => {
this.resolveResponse(response);
});
}
}
resolveResponse(data) {
@@ -62,6 +72,8 @@ class ReportAsTableView extends Component {
Object.keys(row).forEach(function (key, index) {
if (row[key] != null && row[key] != 'undefined' && isNaN(row[key]) && moment(row[key]).isValid()) {
row[key] = moment(row[key]).format("YYYY-MM-DD HH:mm:ss");
}else if(row[key] == null) {
row[key] = "";
}
});
return row;
@@ -72,7 +84,9 @@ class ReportAsTableView extends Component {
return (
<div style={{ border: '1px solid black' }}>
{console.log('rendering the table component>>>>>>>>>>>>>>>')}
{this.getColumns().length > 0 ? (
<ReactDataGrid
columns={this.getColumns()}
rowGetter={this.rowGetter}
View
@@ -32,6 +32,7 @@
"file-loader": "^0.8.5",
"html-loader": "^0.4.3",
"html-webpack-plugin": "^2.24.1",
"http-response-object": "^2.0.3",
"identity-obj-proxy": "^3.0.0",
"jest": "^20.0.4",
"on-build-webpack": "^0.1.0",

0 comments on commit 6fc2fd1

Please sign in to comment.