Skip to content

Commit

Permalink
fix(frontend): keep query param when changing routes (#674)
Browse files Browse the repository at this point in the history
* add tests for changing routes and maintaining query params

* fix(frontend): rollback to Router to maintain queryParams sync with store
  • Loading branch information
eh-am committed Jan 3, 2022
1 parent 923fde6 commit 389019b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
33 changes: 33 additions & 0 deletions cypress/integration/webapp/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe('QueryParams', () => {
// on smaller screens component will be collapsed by default
beforeEach(() => {
cy.viewport(1440, 900);
});

// type a tag so that it's synced to the URL
// IMPORTANT! don't access the url directly since it will render this test useless
// since we are testing populating the queryParams and maintaining between routes
it('maintains queryParams when changing route', () => {
const myTag = 'myrandomtag{}';
const validate = () => {
cy.location().then((loc) => {
const urlParams = new URLSearchParams(loc.search);
expect(urlParams.get('query')).to.eq(myTag);
});
};

cy.visit('/');
cy.get('.tags-input').clear().type(myTag);
cy.get('.tags-query button').click();
validate();

cy.findByTestId('sidebar-continuous-comparison').click();
validate();

cy.findByTestId('sidebar-continuous-diff').click();
validate();

cy.findByTestId('sidebar-continuous-single').click();
validate();
});
});
7 changes: 3 additions & 4 deletions webapp/javascript/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ReactDOM from 'react-dom';
import React from 'react';

import { Provider } from 'react-redux';
import { BrowserRouter, Switch, Route } from 'react-router-dom';
import { Router, BrowserRouter, Switch, Route } from 'react-router-dom';
import FPSStats from 'react-fps-stats';
import { isExperimentalAdhocUIEnabled } from '@utils/features';
import Notifications from '@ui/Notifications';
Expand All @@ -17,7 +17,6 @@ import AdhocComparison from './components/AdhocComparison';
import ServerNotifications from './components/ServerNotifications';

import history from './util/history';
import basename from './util/baseurl';

let showFps = false;
try {
Expand All @@ -30,7 +29,7 @@ try {

ReactDOM.render(
<Provider store={store}>
<BrowserRouter history={history} basename={basename()}>
<Router history={history}>
<ServerNotifications />
<Notifications />
<div className="app">
Expand All @@ -57,7 +56,7 @@ ReactDOM.render(
)}
</Switch>
</div>
</BrowserRouter>
</Router>
{showFps ? <FPSStats left="auto" top="auto" bottom={2} right={2} /> : ''}
</Provider>,
document.getElementById('root')
Expand Down
6 changes: 5 additions & 1 deletion webapp/javascript/util/history.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// src/myHistory.js
import { createBrowserHistory } from 'history';
const history = createBrowserHistory();
import basename from './baseurl';

const history = createBrowserHistory({
basename: basename(),
});
export default history;

0 comments on commit 389019b

Please sign in to comment.