Skip to content

Commit

Permalink
Fix initial state sort handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Apr 19, 2021
1 parent 681bd64 commit 9801524
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,48 @@ describe('Test discover state', () => {
expect(state.getPreviousAppState()).toEqual(stateA);
});
});
describe('Test discover initial state sort handling', () => {
test('Non-empty sort in URL should not fallback to state defaults', async () => {
history = createBrowserHistory();
history.push('/#?_a=(sort:!(!(order_date,desc)))');

state = getState({
getStateDefaults: () => ({ sort: [['fallback', 'desc']] }),
history,
uiSettings: uiSettingsMock,
});
await state.replaceUrlAppState({});
await state.startSync();
expect(state.appStateContainer.getState().sort).toMatchInlineSnapshot(`
Array [
Array [
"order_date",
"desc",
],
]
`);
});
test('Empty sort in URL should allow fallback state defaults', async () => {
history = createBrowserHistory();
history.push('/#?_a=(sort:!())');

state = getState({
getStateDefaults: () => ({ sort: [['fallback', 'desc']] }),
history,
uiSettings: uiSettingsMock,
});
await state.replaceUrlAppState({});
await state.startSync();
expect(state.appStateContainer.getState().sort).toMatchInlineSnapshot(`
Array [
Array [
"fallback",
"desc",
],
]
`);
});
});

describe('Test discover state with legacy migration', () => {
test('migration of legacy query ', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,20 @@ export function getState({
appStateFromUrl.query = migrateLegacyQuery(appStateFromUrl.query);
}

if (appStateFromUrl && appStateFromUrl.sort && !appStateFromUrl.sort.length) {
// If there's an empty array given in the URL, the sort prop should be removed
// This allows the sort prop to be overwritten with the default sorting
delete appStateFromUrl.sort;
}

let initialAppState = handleSourceColumnState(
{
...defaultAppState,
...appStateFromUrl,
},
uiSettings
);

// todo filter source depending on fields fetching flag (if no columns remain and source fetching is enabled, use default columns)
let previousAppState: AppState;
const appStateContainer = createStateContainer<AppState>(initialAppState);
Expand Down

0 comments on commit 9801524

Please sign in to comment.