Skip to content

Commit 6233018

Browse files
committed
Add location revival for easier record/replay (#757)
1 parent 7d18e9c commit 6233018

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

modules/Location.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ var PathUtils = require('./PathUtils');
99
*/
1010
class Location {
1111

12+
/**
13+
* Revives the location from its serialized form.
14+
* Use `Location.revive` as a second argument to `JSON.parse`.
15+
*
16+
* var serialized = JSON.stringify(location);
17+
* var deserialized = JSON.parse(serialized, Location.revive);
18+
*
19+
*/
20+
static revive(key, value) {
21+
if (key === '') {
22+
return new Location(value.path, value.navigationType);
23+
} else {
24+
return value;
25+
}
26+
}
27+
1228
constructor(path, navigationType) {
1329
this.path = path;
1430
this.navigationType = navigationType || NavigationTypes.POP;
@@ -26,6 +42,13 @@ class Location {
2642
return PathUtils.getQuery(this.path, options);
2743
}
2844

45+
toJSON() {
46+
return {
47+
path: this.path,
48+
navigationType: this.navigationType
49+
};
50+
}
51+
2952
}
3053

3154
module.exports = Location;

modules/__tests__/Location-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
var expect = require('expect');
22
var Location = require('../Location');
3+
var NavigationTypes = require('../NavigationTypes');
34

45
describe('Location', function () {
56
var location;
67

8+
it('can be revived', function () {
9+
location = new Location('/the/path', NavigationTypes.POP);
10+
11+
var serialized = JSON.stringify(location);
12+
var revived = JSON.parse(serialized, Location.revive);
13+
14+
expect(revived instanceof Location).toEqual(true);
15+
expect(revived.path).toEqual(location.path);
16+
expect(revived.navigationType).toEqual(location.navigationType);
17+
});
18+
719
describe('with a query string', function () {
820
beforeEach(function () {
921
location = new Location('/the/path?the=query');

0 commit comments

Comments
 (0)