Skip to content

Commit 3d71f45

Browse files
author
Emmanouil Konstantinidis
committed
Mock react router
1 parent 970ab26 commit 3d71f45

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// mock of react router used in the tests
2+
var React = require('react');
3+
4+
function Router (params) {
5+
this.name = 'router';
6+
this.params = params;
7+
}
8+
9+
Router.prototype.makeHref = function () {};
10+
Router.prototype.transitionTo = function () {};
11+
Router.prototype.isActive = function () {
12+
return false;
13+
};
14+
15+
Router.prototype.getCurrentParams = function () {
16+
return this.params;
17+
};
18+
19+
Router.Link = React.createClass({
20+
render: function () {
21+
return (<a/>);
22+
}
23+
});
24+
25+
module.exports = Router;

src/js/__tests__/components/navigation.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global jest, describe, beforeEach, it, expect, spyOn */
1+
/* global jest, describe, beforeEach, it, expect */
22

33
jest.dontMock('reflux');
44
jest.dontMock('../../actions/actions.js');
@@ -11,7 +11,7 @@ var TestUtils = React.addons.TestUtils;
1111

1212
describe('Test for Navigation', function () {
1313

14-
var apiRequests, Actions, Navigation, AuthStore;
14+
var apiRequests, Actions, Navigation, AuthStore, Router;
1515

1616
beforeEach(function () {
1717
// Mock Electron's window.require
@@ -36,6 +36,7 @@ describe('Test for Navigation', function () {
3636
Actions = require('../../actions/actions.js');
3737
AuthStore = require('../../stores/auth.js');
3838
Navigation = require('../../components/navigation.js');
39+
Router = require('react-router');
3940
});
4041

4142
it('Should load the navigation component for logged out users', function () {
@@ -56,20 +57,32 @@ describe('Test for Navigation', function () {
5657

5758
});
5859

59-
6060
it('Should load the navigation component for logged in users', function () {
6161

6262
AuthStore.authStatus = function () {
6363
return true;
6464
};
6565

66-
var instance = TestUtils.renderIntoDocument(<Navigation />);
66+
var instance;
67+
React.withContext({router: new Router()}, function () {
68+
instance = TestUtils.renderIntoDocument(<Navigation />);
69+
});
70+
6771
expect(instance.state.loading).toBeFalsy();
6872
expect(instance.refreshNotifications).toBeDefined();
6973
expect(instance.refreshDone).toBeDefined();
7074
expect(instance.logOut).toBeDefined();
7175
expect(instance.appQuit).toBeDefined();
7276

77+
var logoutIcon = TestUtils.scryRenderedDOMComponentsWithClass(instance, 'fa-sign-out');
78+
expect(logoutIcon.length).toBe(1);
79+
80+
// Now Logout
81+
instance.logOut();
82+
AuthStore.trigger();
83+
logoutIcon = TestUtils.scryRenderedDOMComponentsWithClass(instance, 'fa-sign-out');
84+
expect(logoutIcon.length).toBe(0);
85+
7386
});
7487

7588
});

0 commit comments

Comments
 (0)