Skip to content

Commit

Permalink
Add tests for new by value functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed Jul 21, 2023
1 parent adf14a0 commit b871cc8
Show file tree
Hide file tree
Showing 6 changed files with 492 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { extract, inject } from './search_inject_extract';

describe('search inject extract', () => {
describe('inject', () => {
it('should not inject references if state does not have attributes', () => {
const state = { type: 'type', id: 'id' };
const injectedReferences = [{ name: 'name', type: 'type', id: 'id' }];
expect(inject(state, injectedReferences)).toEqual(state);
});

it('should inject references if state has references with the same name', () => {
const state = {
type: 'type',
id: 'id',
attributes: {
references: [{ name: 'name', type: 'type', id: '1' }],
},
};
const injectedReferences = [{ name: 'name', type: 'type', id: '2' }];
expect(inject(state, injectedReferences)).toEqual({
...state,
attributes: {
...state.attributes,
references: injectedReferences,
},
});
});

it('should clear references if state has no references with the same name', () => {
const state = {
type: 'type',
id: 'id',
attributes: {
references: [{ name: 'name', type: 'type', id: '1' }],
},
};
const injectedReferences = [{ name: 'other', type: 'type', id: '2' }];
expect(inject(state, injectedReferences)).toEqual({
...state,
attributes: {
...state.attributes,
references: [],
},
});
});
});

describe('extract', () => {
it('should not extract references if state does not have attributes', () => {
const state = { type: 'type', id: 'id' };
expect(extract(state)).toEqual({ state, references: [] });
});

it('should extract references if state has references', () => {
const state = {
type: 'type',
id: 'id',
attributes: {
references: [{ name: 'name', type: 'type', id: '1' }],
},
};
expect(extract(state)).toEqual({
state,
references: [{ name: 'name', type: 'type', id: '1' }],
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { savedSearchMock } from '../__mocks__/saved_search';
import { getDiscoverLocatorParams } from './get_discover_locator_params';
import type { SearchInput } from './types';

describe('getDiscoverLocatorParams', () => {
it('should return saved search id if input has savedObjectId', () => {
const input = { savedObjectId: 'savedObjectId' } as SearchInput;
expect(getDiscoverLocatorParams({ input, savedSearch: savedSearchMock })).toEqual({
savedSearchId: 'savedObjectId',
});
});

it('should return Discover params if input has no savedObjectId', () => {
const input = {} as SearchInput;
expect(getDiscoverLocatorParams({ input, savedSearch: savedSearchMock })).toEqual({
dataViewId: savedSearchMock.searchSource.getField('index')?.id,
dataViewSpec: savedSearchMock.searchSource.getField('index')?.toSpec(),
timeRange: savedSearchMock.timeRange,
refreshInterval: savedSearchMock.refreshInterval,
filters: savedSearchMock.searchSource.getField('filter'),
query: savedSearchMock.searchSource.getField('query'),
columns: savedSearchMock.columns,
sort: savedSearchMock.sort,
viewMode: savedSearchMock.viewMode,
hideAggregatedPreview: savedSearchMock.hideAggregatedPreview,
breakdownField: savedSearchMock.breakdownField,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { discoverServiceMock } from '../__mocks__/services';
import { SearchEmbeddableFactory, type StartServices } from './search_embeddable_factory';
import { ErrorEmbeddable } from '@kbn/embeddable-plugin/public';
import type { SearchByValueInput } from '@kbn/saved-search-plugin/public';

jest.mock('@kbn/embeddable-plugin/public', () => {
return {
Expand All @@ -29,7 +30,7 @@ const input = {
const ErrorEmbeddableMock = ErrorEmbeddable as unknown as jest.Mock;

describe('SearchEmbeddableFactory', () => {
it('should create factory correctly', async () => {
it('should create factory correctly from saved object', async () => {
const mockUnwrap = jest
.spyOn(discoverServiceMock.savedSearch.byValue.attributeService, 'unwrapAttributes')
.mockClear();
Expand All @@ -46,6 +47,24 @@ describe('SearchEmbeddableFactory', () => {
expect(embeddable).toBeDefined();
});

it('should create factory correctly from by value input', async () => {
const mockUnwrap = jest
.spyOn(discoverServiceMock.savedSearch.byValue.attributeService, 'unwrapAttributes')
.mockClear();

const factory = new SearchEmbeddableFactory(
() => Promise.resolve({ executeTriggerActions: jest.fn() } as unknown as StartServices),
() => Promise.resolve(discoverServiceMock)
);

const { savedObjectId, ...byValueInput } = input;
const embeddable = await factory.create(byValueInput as SearchByValueInput);

expect(mockUnwrap).toHaveBeenCalledTimes(1);
expect(mockUnwrap).toHaveBeenLastCalledWith(byValueInput);
expect(embeddable).toBeDefined();
});

it('should show error embeddable when create throws an error', async () => {
const error = new Error('Failed to create embeddable');
const factory = new SearchEmbeddableFactory(
Expand Down
Loading

0 comments on commit b871cc8

Please sign in to comment.