|
| 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 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import React from 'react'; |
| 10 | +import { shallow } from 'enzyme'; |
| 11 | +import { TableVisBasic } from './table_vis_basic'; |
| 12 | +import { FormattedColumn, TableVisConfig, TableVisUiState } from '../types'; |
| 13 | +import { DatatableColumn } from 'src/plugins/expressions'; |
| 14 | +import { createTableVisCell } from './table_vis_cell'; |
| 15 | +import { createGridColumns } from './table_vis_columns'; |
| 16 | + |
| 17 | +jest.mock('./table_vis_columns', () => ({ |
| 18 | + createGridColumns: jest.fn(() => []), |
| 19 | +})); |
| 20 | +jest.mock('./table_vis_cell', () => ({ |
| 21 | + createTableVisCell: jest.fn(() => () => {}), |
| 22 | +})); |
| 23 | + |
| 24 | +describe('TableVisBasic', () => { |
| 25 | + const props = { |
| 26 | + fireEvent: jest.fn(), |
| 27 | + table: { |
| 28 | + columns: [], |
| 29 | + rows: [], |
| 30 | + formattedColumns: { |
| 31 | + test: { |
| 32 | + formattedTotal: 100, |
| 33 | + } as FormattedColumn, |
| 34 | + }, |
| 35 | + }, |
| 36 | + visConfig: {} as TableVisConfig, |
| 37 | + uiStateProps: { |
| 38 | + sort: { |
| 39 | + columnIndex: null, |
| 40 | + direction: null, |
| 41 | + }, |
| 42 | + columnsWidth: [], |
| 43 | + setColumnsWidth: jest.fn(), |
| 44 | + setSort: jest.fn(), |
| 45 | + }, |
| 46 | + }; |
| 47 | + |
| 48 | + it('should init data grid', () => { |
| 49 | + const comp = shallow(<TableVisBasic {...props} />); |
| 50 | + expect(comp).toMatchSnapshot(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should init data grid with title provided - for split mode', () => { |
| 54 | + const title = 'My data table'; |
| 55 | + const comp = shallow(<TableVisBasic {...props} title={title} />); |
| 56 | + expect(comp).toMatchSnapshot(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should render the toolbar', () => { |
| 60 | + const comp = shallow( |
| 61 | + <TableVisBasic |
| 62 | + {...props} |
| 63 | + visConfig={{ ...props.visConfig, title: 'My saved vis', showToolbar: true }} |
| 64 | + /> |
| 65 | + ); |
| 66 | + expect(comp).toMatchSnapshot(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should sort rows by column and pass the sorted rows for consumers', () => { |
| 70 | + const uiStateProps = { |
| 71 | + ...props.uiStateProps, |
| 72 | + sort: { |
| 73 | + columnIndex: 1, |
| 74 | + direction: 'desc', |
| 75 | + } as TableVisUiState['sort'], |
| 76 | + }; |
| 77 | + const table = { |
| 78 | + columns: [{ id: 'first' }, { id: 'second' }] as DatatableColumn[], |
| 79 | + rows: [ |
| 80 | + { first: 1, second: 2 }, |
| 81 | + { first: 3, second: 4 }, |
| 82 | + { first: 5, second: 6 }, |
| 83 | + ], |
| 84 | + formattedColumns: {}, |
| 85 | + }; |
| 86 | + const sortedRows = [ |
| 87 | + { first: 5, second: 6 }, |
| 88 | + { first: 3, second: 4 }, |
| 89 | + { first: 1, second: 2 }, |
| 90 | + ]; |
| 91 | + const comp = shallow( |
| 92 | + <TableVisBasic |
| 93 | + {...props} |
| 94 | + table={table} |
| 95 | + uiStateProps={uiStateProps} |
| 96 | + visConfig={{ ...props.visConfig, showToolbar: true }} |
| 97 | + /> |
| 98 | + ); |
| 99 | + expect(createTableVisCell).toHaveBeenCalledWith(sortedRows, table.formattedColumns); |
| 100 | + expect(createGridColumns).toHaveBeenCalledWith( |
| 101 | + table.columns, |
| 102 | + sortedRows, |
| 103 | + table.formattedColumns, |
| 104 | + uiStateProps.columnsWidth, |
| 105 | + props.fireEvent |
| 106 | + ); |
| 107 | + |
| 108 | + const { onSort } = comp.find('EuiDataGrid').prop('sorting'); |
| 109 | + // sort the first column |
| 110 | + onSort([{ id: 'first', direction: 'asc' }]); |
| 111 | + expect(uiStateProps.setSort).toHaveBeenCalledWith({ columnIndex: 0, direction: 'asc' }); |
| 112 | + // sort the second column - should erase the first column sorting since there is only one level sorting available |
| 113 | + onSort([ |
| 114 | + { id: 'first', direction: 'asc' }, |
| 115 | + { id: 'second', direction: 'desc' }, |
| 116 | + ]); |
| 117 | + expect(uiStateProps.setSort).toHaveBeenCalledWith({ columnIndex: 1, direction: 'desc' }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should pass renderFooterCellValue for the total row', () => { |
| 121 | + const comp = shallow( |
| 122 | + <TableVisBasic {...props} visConfig={{ ...props.visConfig, showTotal: true }} /> |
| 123 | + ); |
| 124 | + const renderFooterCellValue: (props: any) => void = comp |
| 125 | + .find('EuiDataGrid') |
| 126 | + .prop('renderFooterCellValue'); |
| 127 | + expect(renderFooterCellValue).toEqual(expect.any(Function)); |
| 128 | + expect(renderFooterCellValue({ columnId: 'test' })).toEqual(100); |
| 129 | + }); |
| 130 | +}); |
0 commit comments