Skip to content

Commit

Permalink
Merge pull request #413 from yahoo/missing-query
Browse files Browse the repository at this point in the history
Add query in some missing places
  • Loading branch information
mridgway committed Apr 1, 2016
2 parents b0af00d + 92897b7 commit 3863005
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
8 changes: 6 additions & 2 deletions packages/fluxible-router/lib/handleHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ function createComponent(Component, opts) {
var confirmResult = onBeforeUnloadText ? window.confirm(onBeforeUnloadText) : true;

var navParams = nav.params || {};
var navQuery = nav.query || {};
var historyState = {
query: navQuery,
params: navParams,
scroll: {
x: window.scrollX,
Expand All @@ -193,7 +195,8 @@ function createComponent(Component, opts) {
this.context.executeAction(navigateAction, {
type: TYPE_POPSTATE,
url: url,
params: (e.state && e.state.params)
params: (e.state && e.state.params),
query: (e.state && e.state.query),
});
}
}
Expand All @@ -218,6 +221,7 @@ function createComponent(Component, opts) {
var nav = this.props.currentNavigate || {};
var navType = nav.type || TYPE_DEFAULT;
var navParams = nav.params || {};
var navQuery = nav.query || {};
var historyState;

switch (navType) {
Expand All @@ -227,7 +231,7 @@ function createComponent(Component, opts) {
if (nav.url === this._history.getUrl()) {
return;
}
historyState = {params: navParams};
historyState = {params: navParams, query: navQuery};
if (nav.preserveScrollPosition) {
historyState.scroll = {x: window.scrollX, y: window.scrollY};
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/fluxible-router/lib/navigateAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function navigateAction (context, payload, done) {
transactionId: context.rootId
}, payload);
if (!payload.url && payload.routeName) {
navigate.url = routeStore.makePath(payload.routeName, payload.params);
navigate.url = routeStore.makePath(payload.routeName, payload.params, payload.query);
navigate.routeName = null;
}

Expand Down
24 changes: 12 additions & 12 deletions packages/fluxible-router/tests/unit/lib/handleHistory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET'});
expect(testResult.pushState).to.eql({state: {params: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.pushState).to.eql({state: {params: {}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.scrollTo).to.eql({x: 0, y: 0});
});
it('update with unicode route, navigate.type=click, reset scroll position', function () {
Expand All @@ -465,7 +465,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/föö', method: 'GET'});
expect(testResult.pushState).to.eql({state: {params: {}, scroll: {x: 0, y: 0}}, title: null, url: '/föö'});
expect(testResult.pushState).to.eql({state: {params: {}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/föö'});
expect(testResult.scrollTo).to.eql({x: 0, y: 0});
});
it('update with different route, navigate.type=click, enableScroll=false, do not reset scroll position', function () {
Expand All @@ -481,7 +481,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET'});
expect(testResult.pushState).to.eql({state: {params: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.pushState).to.eql({state: {params: {}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.scrollTo).to.equal(undefined);
});
it('update with different route, navigate.type=replacestate, enableScroll=false, do not reset scroll position', function () {
Expand All @@ -497,7 +497,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET', type: 'replacestate'});
expect(testResult.replaceState).to.eql({state: {params: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.replaceState).to.eql({state: {params: {}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.scrollTo).to.equal(undefined);
});
it('update with different route, navigate.type=default, reset scroll position', function () {
Expand All @@ -512,7 +512,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET'});
expect(testResult.pushState).to.eql({state: {params: {}, scroll: {x: 0, y: 0} }, title: null, url: '/bar'});
expect(testResult.pushState).to.eql({state: {params: {}, query: {}, scroll: {x: 0, y: 0} }, title: null, url: '/bar'});
expect(testResult.scrollTo).to.eql({x: 0, y: 0});
});
it('update with different route, navigate.type=default, enableScroll=false, do not reset scroll position', function () {
Expand All @@ -528,7 +528,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET'});
expect(testResult.pushState).to.eql({state: {params: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.pushState).to.eql({state: {params: {}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.scrollTo).to.equal(undefined);
});
it('do not pushState, navigate.type=popstate, restore scroll position', function () {
Expand Down Expand Up @@ -574,7 +574,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', type: 'click', params: {foo: 'bar'}});
expect(testResult.pushState).to.eql({state: {params: {foo: 'bar'}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.pushState).to.eql({state: {params: {foo: 'bar'}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
});
it('update with same path and different hash, navigate.type=click, with params', function () {
var routeStore = mockContext.getStore('RouteStore');
Expand All @@ -588,7 +588,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/foo#hash2', type: 'click', params: {foo: 'bar'}});
expect(testResult.pushState).to.eql({state: {params: {foo: 'bar'}, scroll: {x: 0, y: 0}}, title: null, url: '/foo#hash2'});
expect(testResult.pushState).to.eql({state: {params: {foo: 'bar'}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/foo#hash2'});
});
it('update with different route, navigate.type=replacestate, with params', function () {
var routeStore = mockContext.getStore('RouteStore');
Expand All @@ -602,7 +602,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET', type: 'replacestate', params: {foo: 'bar'}});
expect(testResult.replaceState).to.eql({state: {params: {foo: 'bar'}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.replaceState).to.eql({state: {params: {foo: 'bar'}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
});
it('update with different route, navigate.type=replacestate', function () {
var routeStore = mockContext.getStore('RouteStore');
Expand All @@ -616,7 +616,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET', type: 'replacestate'});
expect(testResult.replaceState).to.eql({state: {params: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
expect(testResult.replaceState).to.eql({state: {params: {}, query: {}, scroll: {x: 0, y: 0}}, title: null, url: '/bar'});
});
it('update with different route, navigate.type=pushstate, preserve scroll state', function () {
global.window.scrollX = 42;
Expand All @@ -632,7 +632,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET', type: 'click', preserveScrollPosition: true});
expect(testResult.pushState).to.eql({state: {params: {}, scroll: {x: 42, y: 3}}, title: null, url: '/bar'});
expect(testResult.pushState).to.eql({state: {params: {}, query: {}, scroll: {x: 42, y: 3}}, title: null, url: '/bar'});
});
it('update with different route, navigate.type=replacestate, preserve scroll state', function () {
global.window.scrollX = 42;
Expand All @@ -648,7 +648,7 @@ describe('handleHistory', function () {
<MockAppComponent context={mockContext} />
);
routeStore._handleNavigateStart({url: '/bar', method: 'GET', type: 'replacestate', preserveScrollPosition: true});
expect(testResult.replaceState).to.eql({state: {params: {}, scroll: {x: 42, y: 3}}, title: null, url: '/bar'});
expect(testResult.replaceState).to.eql({state: {params: {}, query: {}, scroll: {x: 42, y: 3}}, title: null, url: '/bar'});
});
});
});
Expand Down
16 changes: 16 additions & 0 deletions packages/fluxible-router/tests/unit/lib/navigateAction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ describe('navigateAction', function () {
});
});

it('should dispatch with query params', function (done) {
navigateAction(mockContext, {
routeName: 'withParams',
params: { id: 5 },
query: { foo: 'bar' }
}, function (err) {
expect(err).to.equal(undefined);
expect(mockContext.dispatchCalls.length).to.equal(2);
expect(mockContext.dispatchCalls[0].name).to.equal('NAVIGATE_START');
expect(mockContext.dispatchCalls[0].payload.url).to.equal('/withParams/5?foo=bar');
expect(mockContext.dispatchCalls[1].name).to.equal('NAVIGATE_SUCCESS');
expect(mockContext.dispatchCalls[1].payload.url).to.equal('/withParams/5?foo=bar');
done();
});
});

it('should error if routeStore does not exist', function (done) {
function BadRouteStore(){}
BadRouteStore.storeName = 'RouteStore';
Expand Down

0 comments on commit 3863005

Please sign in to comment.