Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Mar 14, 2024
1 parent a5ee7d9 commit 42e9201
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/plugins/discover/public/__mocks__/saved_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const savedSearchMock = {
id: 'the-saved-search-id',
title: 'A saved search',
searchSource: createSearchSourceMock({ index: dataViewMock }),
columns: ['default_column'],
sort: [],
} as unknown as SavedSearch;

export const savedSearchMockWithTimeField = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
createSearchSessionRestorationDataProvider,
} from './discover_state';
import { createBrowserHistory, createMemoryHistory, History } from 'history';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import { createSearchSourceMock, dataPluginMock } from '@kbn/data-plugin/public/mocks';
import type { SavedSearch, SortOrder } from '@kbn/saved-search-plugin/public';
import {
savedSearchAdHoc,
Expand Down Expand Up @@ -524,6 +524,43 @@ describe('Test discover state actions', () => {
unsubscribe();
});

test('loadSavedSearch given a URL with different time range than the stored one showing as changed', async () => {
const url = '/#_g=(time:(from:now-24h%2Fh,to:now))';
const savedSearch = {
...savedSearchMock,
searchSource: createSearchSourceMock({ index: dataViewMock, filter: [] }),
timeRestore: true,
timeRange: { from: 'now-666m', to: 'now' },
};
const { state } = await getState(url, {
savedSearch,
isEmptyUrl: false,
});
await state.actions.loadSavedSearch({ savedSearchId: savedSearchMock.id });
const unsubscribe = state.actions.initializeAndSync();
await new Promise(process.nextTick);
expect(state.savedSearchState.getHasChanged$().getValue()).toBe(true);
unsubscribe();
});

test('loadSavedSearch given a URL with different time range than without timeRestore not showing as changed', async () => {
const url = '/#?_g=(time:(from:now-24h%2Fh,to:now))';
const savedSearch = {
...savedSearchMock,
searchSource: createSearchSourceMock({ index: dataViewMock, filter: [] }),
timeRestore: false,
};
const { state } = await getState(url, {
savedSearch,
isEmptyUrl: false,
});
await state.actions.loadSavedSearch({ savedSearchId: savedSearchMock.id });
const unsubscribe = state.actions.initializeAndSync();
await new Promise(process.nextTick);
expect(state.savedSearchState.getHasChanged$().getValue()).toBe(false);
unsubscribe();
});

test('loadSavedSearch ignoring hideChart in URL', async () => {
const url = '/#?_a=(hideChart:true,columns:!(message))&_g=()';
const { state } = await getState(url, { savedSearch: savedSearchMock });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { savedSearchMock } from '../../../__mocks__/saved_search';
import { discoverServiceMock } from '../../../__mocks__/services';
import { Filter, FilterStateStore, Query } from '@kbn/es-query';
import { updateSavedSearch } from './update_saved_search';
import { SavedSearch } from '@kbn/saved-search-plugin/public';

describe('updateSavedSearch', () => {
const query: Query = {
Expand Down Expand Up @@ -73,6 +74,56 @@ describe('updateSavedSearch', () => {
expect(savedSearch.searchSource.getField('filter')).toEqual([globalFilter, appFilter]);
});

it('should set time range is timeRestore is enabled', async () => {
const savedSearch: SavedSearch = {
...savedSearchMock,
searchSource: savedSearchMock.searchSource.createCopy(),
timeRestore: true,
};
(discoverServiceMock.timefilter.getTime as jest.Mock).mockReturnValue({
from: 'now-666m',
to: 'now',
});
updateSavedSearch({
savedSearch,
globalStateContainer: createGlobalStateContainer(),
services: discoverServiceMock,
state: {
query,
filters: [appFilter],
},
});
expect(savedSearch.timeRange).toEqual({
from: 'now-666m',
to: 'now',
});
});

it('should not set time range is timeRestore is enabled', async () => {
const savedSearch: SavedSearch = {
...savedSearchMock,
searchSource: savedSearchMock.searchSource.createCopy(),
timeRestore: false,
};
(discoverServiceMock.timefilter.getTime as jest.Mock).mockReturnValue({
from: 'now-666m',
to: 'now',
});
updateSavedSearch({
savedSearch,
globalStateContainer: createGlobalStateContainer(),
services: discoverServiceMock,
state: {
query,
filters: [appFilter],
},
});
expect(savedSearch.timeRange).not.toEqual({
from: 'now-666m',
to: 'now',
});
});

it('should set query and filters from services', async () => {
const savedSearch = {
...savedSearchMock,
Expand Down

0 comments on commit 42e9201

Please sign in to comment.