|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import React from 'react'; |
| 8 | +import { shallow } from 'enzyme'; |
| 9 | +import { mountWithIntl } from 'test_utils/enzyme_helpers'; |
| 10 | +import { datatable, DatatableComponent } from './expression'; |
| 11 | +import { LensMultiTable } from '../types'; |
| 12 | +import { DatatableProps } from './expression'; |
| 13 | +import { createMockExecutionContext } from '../../../../../src/plugins/expressions/common/mocks'; |
| 14 | +import { IFieldFormat } from '../../../../../src/plugins/data/public'; |
| 15 | +import { IAggType } from 'src/plugins/data/public'; |
| 16 | +const executeTriggerActions = jest.fn(); |
| 17 | + |
| 18 | +function sampleArgs() { |
| 19 | + const data: LensMultiTable = { |
| 20 | + type: 'lens_multitable', |
| 21 | + tables: { |
| 22 | + l1: { |
| 23 | + type: 'kibana_datatable', |
| 24 | + columns: [ |
| 25 | + { id: 'a', name: 'a', meta: { type: 'count' } }, |
| 26 | + { id: 'b', name: 'b', meta: { type: 'date_histogram', aggConfigParams: { field: 'b' } } }, |
| 27 | + { id: 'c', name: 'c', meta: { type: 'cardinality' } }, |
| 28 | + ], |
| 29 | + rows: [{ a: 10110, b: 1588024800000, c: 3 }], |
| 30 | + }, |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
| 34 | + const args: DatatableProps['args'] = { |
| 35 | + title: 'My fanci metric chart', |
| 36 | + columns: { |
| 37 | + columnIds: ['a', 'b', 'c'], |
| 38 | + type: 'lens_datatable_columns', |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + return { data, args }; |
| 43 | +} |
| 44 | + |
| 45 | +describe('datatable_expression', () => { |
| 46 | + describe('datatable renders', () => { |
| 47 | + test('it renders with the specified data and args', () => { |
| 48 | + const { data, args } = sampleArgs(); |
| 49 | + const result = datatable.fn(data, args, createMockExecutionContext()); |
| 50 | + |
| 51 | + expect(result).toEqual({ |
| 52 | + type: 'render', |
| 53 | + as: 'lens_datatable_renderer', |
| 54 | + value: { data, args }, |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('DatatableComponent', () => { |
| 60 | + test('it renders the title and value', () => { |
| 61 | + const { data, args } = sampleArgs(); |
| 62 | + |
| 63 | + expect( |
| 64 | + shallow( |
| 65 | + <DatatableComponent |
| 66 | + data={data} |
| 67 | + args={args} |
| 68 | + formatFactory={x => x as IFieldFormat} |
| 69 | + executeTriggerActions={executeTriggerActions} |
| 70 | + getType={jest.fn()} |
| 71 | + /> |
| 72 | + ) |
| 73 | + ).toMatchSnapshot(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('it invokes executeTriggerActions with correct context on click on top value', () => { |
| 77 | + const { args, data } = sampleArgs(); |
| 78 | + |
| 79 | + const wrapper = mountWithIntl( |
| 80 | + <DatatableComponent |
| 81 | + data={{ |
| 82 | + ...data, |
| 83 | + dateRange: { |
| 84 | + fromDate: new Date('2020-04-20T05:00:00.000Z'), |
| 85 | + toDate: new Date('2020-05-03T05:00:00.000Z'), |
| 86 | + }, |
| 87 | + }} |
| 88 | + args={args} |
| 89 | + formatFactory={x => x as IFieldFormat} |
| 90 | + executeTriggerActions={executeTriggerActions} |
| 91 | + getType={jest.fn(() => ({ type: 'buckets' } as IAggType))} |
| 92 | + /> |
| 93 | + ); |
| 94 | + |
| 95 | + wrapper |
| 96 | + .find('[data-test-subj="lensDatatableFilterOut"]') |
| 97 | + .first() |
| 98 | + .simulate('click'); |
| 99 | + |
| 100 | + expect(executeTriggerActions).toHaveBeenCalledWith('VALUE_CLICK_TRIGGER', { |
| 101 | + data: { |
| 102 | + data: [ |
| 103 | + { |
| 104 | + column: 0, |
| 105 | + row: 0, |
| 106 | + table: data.tables.l1, |
| 107 | + value: 10110, |
| 108 | + }, |
| 109 | + ], |
| 110 | + negate: true, |
| 111 | + }, |
| 112 | + timeFieldName: undefined, |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + test('it invokes executeTriggerActions with correct context on click on timefield', () => { |
| 117 | + const { args, data } = sampleArgs(); |
| 118 | + |
| 119 | + const wrapper = mountWithIntl( |
| 120 | + <DatatableComponent |
| 121 | + data={{ |
| 122 | + ...data, |
| 123 | + dateRange: { |
| 124 | + fromDate: new Date('2020-04-20T05:00:00.000Z'), |
| 125 | + toDate: new Date('2020-05-03T05:00:00.000Z'), |
| 126 | + }, |
| 127 | + }} |
| 128 | + args={args} |
| 129 | + formatFactory={x => x as IFieldFormat} |
| 130 | + executeTriggerActions={executeTriggerActions} |
| 131 | + getType={jest.fn(() => ({ type: 'buckets' } as IAggType))} |
| 132 | + /> |
| 133 | + ); |
| 134 | + |
| 135 | + wrapper |
| 136 | + .find('[data-test-subj="lensDatatableFilterFor"]') |
| 137 | + .at(3) |
| 138 | + .simulate('click'); |
| 139 | + |
| 140 | + expect(executeTriggerActions).toHaveBeenCalledWith('VALUE_CLICK_TRIGGER', { |
| 141 | + data: { |
| 142 | + data: [ |
| 143 | + { |
| 144 | + column: 1, |
| 145 | + row: 0, |
| 146 | + table: data.tables.l1, |
| 147 | + value: 1588024800000, |
| 148 | + }, |
| 149 | + ], |
| 150 | + negate: false, |
| 151 | + }, |
| 152 | + timeFieldName: 'b', |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments