Skip to content

Commit 6dbd00a

Browse files
committed
Use PathStore.setup/teardown for testing
1 parent c976d21 commit 6dbd00a

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

modules/mixins/LocationContext.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var LocationContext = {
6262
);
6363

6464
if (location)
65-
PathStore.useLocation(location);
65+
PathStore.setup(location);
6666
},
6767

6868
/**

modules/stores/PathStore.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ function notifyChange() {
99
_events.emit(CHANGE_EVENT);
1010
}
1111

12-
var _currentLocation, _currentPath, _currentActionType;
12+
var _currentLocation, _currentActionType;
13+
var _currentPath = '/';
1314

1415
function handleLocationChangeAction(action) {
1516
if (_currentPath !== action.path) {
@@ -39,7 +40,7 @@ var PathStore = {
3940
/**
4041
* Setup the PathStore to use the given location.
4142
*/
42-
useLocation: function (location) {
43+
setup: function (location) {
4344
invariant(
4445
_currentLocation == null || _currentLocation === location,
4546
'You cannot use %s and %s on the same page',
@@ -56,6 +57,19 @@ var PathStore = {
5657
_currentLocation = location;
5758
},
5859

60+
/**
61+
* Tear down the PathStore. Really only used for testing.
62+
*/
63+
teardown: function () {
64+
if (_currentLocation && _currentLocation.teardown)
65+
_currentLocation.teardown();
66+
67+
_currentLocation = _currentActionType = null;
68+
_currentPath = '/';
69+
70+
PathStore.removeAllChangeListeners();
71+
},
72+
5973
/**
6074
* Returns the current URL path.
6175
*/

modules/stores/__tests__/PathStore-test.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
var assert = require('assert');
22
var expect = require('expect');
33
var LocationActions = require('../../actions/LocationActions');
4-
var LocationDispatcher = require('../../dispatchers/LocationDispatcher');
54
var PathStore = require('../PathStore');
65

76
describe('PathStore', function () {
87

8+
var _onChange;
9+
var MockLocation = {
10+
setup: function (onChange) {
11+
_onChange = onChange;
12+
},
13+
getCurrentPath: function () {
14+
return '/';
15+
},
16+
toString: function () {
17+
return '<MockLocation>';
18+
}
19+
};
20+
921
beforeEach(function () {
10-
LocationDispatcher.handleViewAction({
11-
type: LocationActions.PUSH,
12-
path: '/'
13-
});
22+
PathStore.setup(MockLocation);
1423
});
1524

25+
afterEach(PathStore.teardown);
26+
1627
var changeWasFired;
1728
function changeListener() {
1829
changeWasFired = true;
@@ -30,7 +41,7 @@ describe('PathStore', function () {
3041
describe('when a new URL path is pushed', function () {
3142
beforeEach(setupChangeListener);
3243
beforeEach(function () {
33-
LocationDispatcher.handleViewAction({
44+
_onChange({
3445
type: LocationActions.PUSH,
3546
path: '/push'
3647
});
@@ -54,7 +65,7 @@ describe('PathStore', function () {
5465
describe('when a URL path is replaced', function () {
5566
beforeEach(setupChangeListener);
5667
beforeEach(function () {
57-
LocationDispatcher.handleViewAction({
68+
_onChange({
5869
type: LocationActions.REPLACE,
5970
path: '/replace'
6071
});
@@ -78,7 +89,7 @@ describe('PathStore', function () {
7889
describe('when a URL path is popped', function () {
7990
beforeEach(setupChangeListener);
8091
beforeEach(function () {
81-
LocationDispatcher.handleViewAction({
92+
_onChange({
8293
type: LocationActions.POP,
8394
path: '/pop'
8495
});

0 commit comments

Comments
 (0)