Skip to content

Commit

Permalink
fix tests and types. set index to sync
Browse files Browse the repository at this point in the history
  • Loading branch information
mbondyra committed Jun 26, 2020
1 parent f1c6ec1 commit 3b71699
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
32 changes: 23 additions & 9 deletions x-pack/plugins/lens/public/indexpattern_datasource/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { SavedObjectsClientContract } from 'kibana/public';
import { IStorageWrapper } from 'src/plugins/kibana_utils/public';
import _ from 'lodash';
import {
loadInitialState,
Expand All @@ -19,12 +18,10 @@ import { documentField } from './document_field';

jest.mock('./operations');

const createMockStorage = (lastData: Record<string, string>) => {
const createMockStorage = (lastData?: Record<string, string>) => {
return {
get: jest.fn().mockImplementation(() => lastData),
set: jest.fn().mockImplementation((key, value) => {
lastData[key] = value;
}),
set: jest.fn(),
remove: jest.fn(),
clear: jest.fn(),
};
Expand Down Expand Up @@ -281,9 +278,10 @@ describe('loader', () => {

describe('loadInitialState', () => {
it('should load a default state', async () => {
const storage = createMockStorage();
const state = await loadInitialState({
savedObjectsClient: mockClient(),
storage: createMockStorage(),
storage,
});

expect(state).toMatchObject({
Expand All @@ -298,12 +296,16 @@ describe('loader', () => {
layers: {},
showEmptyFields: false,
});
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
indexPatternId: 'a',
});
});

it('should load a default state when lastUsedIndexPatternId is not found in indexPatternRefs', async () => {
const storage = createMockStorage({ indexPatternId: 'c' });
const state = await loadInitialState({
savedObjectsClient: mockClient(),
storage: createMockStorage({ indexPatternId: 'c' }),
storage,
});

expect(state).toMatchObject({
Expand All @@ -318,6 +320,9 @@ describe('loader', () => {
layers: {},
showEmptyFields: false,
});
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
indexPatternId: 'a',
});
});

it('should load lastUsedIndexPatternId if in localStorage', async () => {
Expand All @@ -341,10 +346,11 @@ describe('loader', () => {
});

it('should use the default index pattern id, if provided', async () => {
const storage = createMockStorage();
const state = await loadInitialState({
defaultIndexPatternId: 'b',
savedObjectsClient: mockClient(),
storage: createMockStorage(),
storage,
});

expect(state).toMatchObject({
Expand All @@ -359,6 +365,9 @@ describe('loader', () => {
layers: {},
showEmptyFields: false,
});
expect(storage.set).toHaveBeenCalledWith('lens-settings', {
indexPatternId: 'b',
});
});

it('should initialize from saved state', async () => {
Expand Down Expand Up @@ -390,10 +399,11 @@ describe('loader', () => {
},
},
};
const storage = createMockStorage({ indexPatternId: 'a' });
const state = await loadInitialState({
state: savedState,
savedObjectsClient: mockClient(),
storage: createMockStorage({ indexPatternId: 'a' }),
storage,
});

expect(state).toMatchObject({
Expand All @@ -408,6 +418,10 @@ describe('loader', () => {
layers: savedState.layers,
showEmptyFields: false,
});

expect(storage.set).toHaveBeenCalledWith('lens-settings', {
indexPatternId: 'b',
});
});
});

Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/lens/public/indexpattern_datasource/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ export async function loadInitialState({
);

const currentIndexPatternId = requiredPatterns[0];
setLastUsedIndexPatternId(storage, currentIndexPatternId);

const indexPatterns = await loadIndexPatterns({
savedObjectsClient,
cache: {},
patterns: requiredPatterns,
});

if (state) {
return {
...state,
Expand Down

0 comments on commit 3b71699

Please sign in to comment.