Skip to content

Commit

Permalink
fix / add jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkime committed Dec 20, 2022
1 parent 231ba16 commit 0dc8292
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 37 deletions.
8 changes: 8 additions & 0 deletions src/plugins/data_views/common/data_views/data_views.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ describe('IndexPatterns', () => {
SOClientGetDelay = 0;
});

test('force field refresh', async () => {
const id = '1';
await indexPatterns.get(id);
expect(apiClient.getFieldsForWildcard).toBeCalledTimes(1);
await indexPatterns.get(id, undefined, true);
expect(apiClient.getFieldsForWildcard).toBeCalledTimes(2);
});

test('does cache ad-hoc data views', async () => {
const id = '1';

Expand Down
1 change: 1 addition & 0 deletions src/plugins/discover/public/__mocks__/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function createDiscoverDataViewsMock() {
return Promise.reject('Invald');
}
},
getDefaultDataView: jest.fn(() => dataViewMock),
updateSavedObject: jest.fn(),
getIdsWithTitle: jest.fn(() => {
return Promise.resolve([dataViewMock, dataViewComplexMock, dataViewWithTimefieldMock]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,6 @@ interface DataViewData {
stateValFound: boolean;
}

export function findDataViewById(
dataViews: DataViewListItem[],
id: string
): DataViewListItem | undefined {
return dataViews.find((o) => o.id === id);
}

/**
* Checks if the given defaultIndex exists and returns
* the first available data view id if not
*/
export function getFallbackDataViewId(
dataViews: DataViewListItem[],
defaultIndex: string = ''
): string {
if (defaultIndex && findDataViewById(dataViews, defaultIndex)) {
return defaultIndex;
}
return dataViews && dataViews[0]?.id ? dataViews[0].id : '';
}

/**
* A given data view id is checked for existence and a fallback is provided if it doesn't exist
* The provided defaultIndex is usually configured in Advanced Settings, if it's also invalid
* the first entry of the given list of dataViews is used
*/
export function getDataViewId(
id: string = '',
dataViews: DataViewListItem[] = [],
defaultIndex: string = ''
): string {
if (!id || !findDataViewById(dataViews, id)) {
return getFallbackDataViewId(dataViews, defaultIndex);
}
return id;
}

/**
* Function to load the given data view by id, providing a fallback if it doesn't exist
*/
Expand Down

0 comments on commit 0dc8292

Please sign in to comment.