2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
* 2.0.
|
2019-09-17 20:57:53 +02:00
|
|
|
*/
|
|
|
|
|
2020-08-10 15:06:58 +02:00
|
|
|
import { SavedObjectsClientContract, SavedObjectsBulkUpdateObject } from 'kibana/public';
|
2019-09-17 20:57:53 +02:00
|
|
|
import { SavedObjectIndexStore } from './saved_object_store';
|
|
|
|
|
|
|
|
describe('LensStore', () => {
|
|
|
|
function testStore(testId?: string) {
|
|
|
|
const client = {
|
|
|
|
create: jest.fn(() => Promise.resolve({ id: testId || 'testid' })),
|
2020-08-10 15:06:58 +02:00
|
|
|
bulkUpdate: jest.fn(([{ id }]: SavedObjectsBulkUpdateObject[]) =>
|
|
|
|
Promise.resolve({ savedObjects: [{ id }, { id }] })
|
|
|
|
),
|
2019-09-17 20:57:53 +02:00
|
|
|
get: jest.fn(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
client,
|
2020-08-10 15:06:58 +02:00
|
|
|
store: new SavedObjectIndexStore((client as unknown) as SavedObjectsClientContract),
|
2019-09-17 20:57:53 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('save', () => {
|
|
|
|
test('creates and returns a visualization document', async () => {
|
|
|
|
const { client, store } = testStore('FOO');
|
|
|
|
const doc = await store.save({
|
|
|
|
title: 'Hello',
|
2020-06-10 18:28:00 +02:00
|
|
|
description: 'My doc',
|
2019-09-17 20:57:53 +02:00
|
|
|
visualizationType: 'bar',
|
2020-08-21 18:08:25 +02:00
|
|
|
references: [],
|
2019-09-17 20:57:53 +02:00
|
|
|
state: {
|
|
|
|
datasourceStates: {
|
|
|
|
indexpattern: { type: 'index_pattern', indexPattern: '.kibana_test' },
|
|
|
|
},
|
|
|
|
visualization: { x: 'foo', y: 'baz' },
|
|
|
|
query: { query: '', language: 'lucene' },
|
|
|
|
filters: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(doc).toEqual({
|
2020-09-23 23:40:52 +02:00
|
|
|
savedObjectId: 'FOO',
|
2019-09-17 20:57:53 +02:00
|
|
|
title: 'Hello',
|
2020-06-10 18:28:00 +02:00
|
|
|
description: 'My doc',
|
2019-09-17 20:57:53 +02:00
|
|
|
visualizationType: 'bar',
|
2020-08-21 18:08:25 +02:00
|
|
|
references: [],
|
2019-09-17 20:57:53 +02:00
|
|
|
state: {
|
|
|
|
datasourceStates: {
|
|
|
|
indexpattern: { type: 'index_pattern', indexPattern: '.kibana_test' },
|
|
|
|
},
|
|
|
|
visualization: { x: 'foo', y: 'baz' },
|
|
|
|
query: { query: '', language: 'lucene' },
|
|
|
|
filters: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(client.create).toHaveBeenCalledTimes(1);
|
2020-08-21 18:08:25 +02:00
|
|
|
expect(client.create).toHaveBeenCalledWith(
|
|
|
|
'lens',
|
|
|
|
{
|
|
|
|
title: 'Hello',
|
|
|
|
description: 'My doc',
|
|
|
|
visualizationType: 'bar',
|
|
|
|
state: {
|
|
|
|
datasourceStates: {
|
|
|
|
indexpattern: { type: 'index_pattern', indexPattern: '.kibana_test' },
|
|
|
|
},
|
|
|
|
visualization: { x: 'foo', y: 'baz' },
|
|
|
|
query: { query: '', language: 'lucene' },
|
|
|
|
filters: [],
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
|
|
|
},
|
2020-08-21 18:08:25 +02:00
|
|
|
{
|
|
|
|
references: [],
|
|
|
|
}
|
|
|
|
);
|
2019-09-17 20:57:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('updates and returns a visualization document', async () => {
|
|
|
|
const { client, store } = testStore();
|
|
|
|
const doc = await store.save({
|
2020-09-23 23:40:52 +02:00
|
|
|
savedObjectId: 'Gandalf',
|
2019-09-17 20:57:53 +02:00
|
|
|
title: 'Even the very wise cannot see all ends.',
|
|
|
|
visualizationType: 'line',
|
2020-08-21 18:08:25 +02:00
|
|
|
references: [],
|
2019-09-17 20:57:53 +02:00
|
|
|
state: {
|
|
|
|
datasourceStates: { indexpattern: { type: 'index_pattern', indexPattern: 'lotr' } },
|
|
|
|
visualization: { gear: ['staff', 'pointy hat'] },
|
|
|
|
query: { query: '', language: 'lucene' },
|
|
|
|
filters: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(doc).toEqual({
|
2020-09-23 23:40:52 +02:00
|
|
|
savedObjectId: 'Gandalf',
|
2019-09-17 20:57:53 +02:00
|
|
|
title: 'Even the very wise cannot see all ends.',
|
|
|
|
visualizationType: 'line',
|
2020-08-21 18:08:25 +02:00
|
|
|
references: [],
|
2019-09-17 20:57:53 +02:00
|
|
|
state: {
|
|
|
|
datasourceStates: { indexpattern: { type: 'index_pattern', indexPattern: 'lotr' } },
|
|
|
|
visualization: { gear: ['staff', 'pointy hat'] },
|
|
|
|
query: { query: '', language: 'lucene' },
|
|
|
|
filters: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-08-10 15:06:58 +02:00
|
|
|
expect(client.bulkUpdate).toHaveBeenCalledTimes(1);
|
|
|
|
expect(client.bulkUpdate).toHaveBeenCalledWith([
|
|
|
|
{
|
|
|
|
type: 'lens',
|
|
|
|
id: 'Gandalf',
|
2020-08-21 18:08:25 +02:00
|
|
|
references: [],
|
2020-08-10 15:06:58 +02:00
|
|
|
attributes: {
|
|
|
|
title: null,
|
|
|
|
visualizationType: null,
|
|
|
|
state: null,
|
|
|
|
},
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
2020-08-10 15:06:58 +02:00
|
|
|
{
|
|
|
|
type: 'lens',
|
|
|
|
id: 'Gandalf',
|
2020-08-21 18:08:25 +02:00
|
|
|
references: [],
|
2020-08-10 15:06:58 +02:00
|
|
|
attributes: {
|
|
|
|
title: 'Even the very wise cannot see all ends.',
|
|
|
|
visualizationType: 'line',
|
|
|
|
state: {
|
|
|
|
datasourceStates: { indexpattern: { type: 'index_pattern', indexPattern: 'lotr' } },
|
|
|
|
visualization: { gear: ['staff', 'pointy hat'] },
|
|
|
|
query: { query: '', language: 'lucene' },
|
|
|
|
filters: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
2019-09-17 20:57:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('load', () => {
|
|
|
|
test('throws if an error is returned', async () => {
|
|
|
|
const { client, store } = testStore();
|
|
|
|
client.get = jest.fn(async () => ({
|
|
|
|
id: 'Paul',
|
|
|
|
type: 'lens',
|
|
|
|
attributes: {
|
|
|
|
title: 'Hope clouds observation.',
|
|
|
|
visualizationType: 'dune',
|
|
|
|
state: '{ "datasource": { "giantWorms": true } }',
|
|
|
|
},
|
|
|
|
error: new Error('shoot dang!'),
|
|
|
|
}));
|
|
|
|
|
|
|
|
await expect(store.load('Paul')).rejects.toThrow('shoot dang!');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|