|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import { dataPluginMock } from '../../../data/public/mocks'; |
| 21 | +import { extractIndexPatternsFromSpec } from './extract_index_pattern'; |
| 22 | +import { setData } from '../services'; |
| 23 | + |
| 24 | +import type { VegaSpec } from '../data_model/types'; |
| 25 | + |
| 26 | +const getMockedSpec = (mockedObj: any) => (mockedObj as unknown) as VegaSpec; |
| 27 | + |
| 28 | +describe('extractIndexPatternsFromSpec', () => { |
| 29 | + const dataStart = dataPluginMock.createStartContract(); |
| 30 | + |
| 31 | + beforeAll(() => { |
| 32 | + setData(dataStart); |
| 33 | + }); |
| 34 | + |
| 35 | + test('should not throw errors if no index is specified', async () => { |
| 36 | + const spec = getMockedSpec({ |
| 37 | + data: {}, |
| 38 | + }); |
| 39 | + |
| 40 | + const indexes = await extractIndexPatternsFromSpec(spec); |
| 41 | + |
| 42 | + expect(indexes).toMatchInlineSnapshot(`Array []`); |
| 43 | + }); |
| 44 | + |
| 45 | + test('should extract single index pattern', async () => { |
| 46 | + const spec = getMockedSpec({ |
| 47 | + data: { |
| 48 | + url: { |
| 49 | + index: 'test', |
| 50 | + }, |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + const indexes = await extractIndexPatternsFromSpec(spec); |
| 55 | + |
| 56 | + expect(indexes).toMatchInlineSnapshot(` |
| 57 | + Array [ |
| 58 | + Object { |
| 59 | + "id": "test", |
| 60 | + "title": "test", |
| 61 | + }, |
| 62 | + ] |
| 63 | + `); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should extract multiple index patterns', async () => { |
| 67 | + const spec = getMockedSpec({ |
| 68 | + data: [ |
| 69 | + { |
| 70 | + url: { |
| 71 | + index: 'test1', |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + url: { |
| 76 | + index: 'test2', |
| 77 | + }, |
| 78 | + }, |
| 79 | + ], |
| 80 | + }); |
| 81 | + |
| 82 | + const indexes = await extractIndexPatternsFromSpec(spec); |
| 83 | + |
| 84 | + expect(indexes).toMatchInlineSnapshot(` |
| 85 | + Array [ |
| 86 | + Object { |
| 87 | + "id": "test1", |
| 88 | + "title": "test1", |
| 89 | + }, |
| 90 | + Object { |
| 91 | + "id": "test2", |
| 92 | + "title": "test2", |
| 93 | + }, |
| 94 | + ] |
| 95 | + `); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should filter empty values', async () => { |
| 99 | + const spec = getMockedSpec({ |
| 100 | + data: [ |
| 101 | + { |
| 102 | + url: { |
| 103 | + wrong: 'wrong', |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + url: { |
| 108 | + index: 'ok', |
| 109 | + }, |
| 110 | + }, |
| 111 | + ], |
| 112 | + }); |
| 113 | + |
| 114 | + const indexes = await extractIndexPatternsFromSpec(spec); |
| 115 | + |
| 116 | + expect(indexes).toMatchInlineSnapshot(` |
| 117 | + Array [ |
| 118 | + Object { |
| 119 | + "id": "ok", |
| 120 | + "title": "ok", |
| 121 | + }, |
| 122 | + ] |
| 123 | + `); |
| 124 | + }); |
| 125 | +}); |
0 commit comments