Skip to content

Commit

Permalink
Add unit test for the custom state storage
Browse files Browse the repository at this point in the history
  • Loading branch information
weltenwort committed Dec 11, 2023
1 parent aeee2d4 commit 82e9b6e
Showing 1 changed file with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
DiscoverStateContainer,
createSearchSessionRestorationDataProvider,
} from './discover_state';
import { createBrowserHistory, History } from 'history';
import { createBrowserHistory, createMemoryHistory, History } from 'history';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import type { SavedSearch, SortOrder } from '@kbn/saved-search-plugin/public';
import {
Expand All @@ -27,6 +27,7 @@ import { waitFor } from '@testing-library/react';
import { DiscoverCustomizationContext, FetchStatus } from '../../types';
import { dataViewAdHoc, dataViewComplexMock } from '../../../__mocks__/data_view_complex';
import { copySavedSearch } from './discover_saved_search_container';
import { createKbnUrlStateStorage, IKbnUrlStateStorage } from '@kbn/kibana-utils-plugin/public';

const startSync = (appState: DiscoverAppStateContainer) => {
const { start, stop } = appState.syncState();
Expand Down Expand Up @@ -151,6 +152,68 @@ describe('Test discover state', () => {
expect(getCurrentUrl()).toBe('/#?_g=(refreshInterval:(pause:!t,value:5000))');
});
});

describe('Test discover state with overridden state storage', () => {
let stopSync = () => {};
let history: History;
let stateStorage: IKbnUrlStateStorage;
let state: DiscoverStateContainer;

beforeEach(async () => {
jest.useFakeTimers();
history = createMemoryHistory({
initialEntries: [
{
pathname: '/',
hash: `?_a=()`,
},
],
});
stateStorage = createKbnUrlStateStorage({
history,
useHash: false,
useHashQuery: true,
});
state = getDiscoverStateContainer({
services: discoverServiceMock,
history,
customizationContext,
stateStorageContainer: stateStorage,
});
state.savedSearchState.set(savedSearchMock);
state.appState.update({}, true);
stopSync = startSync(state.appState);
});

afterEach(() => {
stopSync();
stopSync = () => {};
jest.useRealTimers();
});

test('setting app state and syncing to URL', async () => {
state.appState.update({ index: 'modified' });

await jest.runAllTimersAsync();

expect(history.createHref(history.location)).toMatchInlineSnapshot(
`"/#?_a=(columns:!(default_column),index:modified,interval:auto,sort:!())"`
);
});

test('changing URL to be propagated to appState', async () => {
history.push('/#?_a=(index:modified)');

await jest.runAllTimersAsync();

expect(state.appState.getState()).toMatchInlineSnapshot(`
Object {
"index": "modified",
}
`);
});
});

describe('Test discover initial state sort handling', () => {
test('Non-empty sort in URL should not be overwritten by saved search sort', async () => {
const savedSearch = {
Expand Down

0 comments on commit 82e9b6e

Please sign in to comment.