Skip to content

Commit

Permalink
fix and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Oct 7, 2020
1 parent 41cbf19 commit 0f24e71
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ describe('getFormatWithAggs', () => {
const format = getFieldFormat(mapping);

expect(format.convert({ gte: 1, lt: 20, label: 'custom' })).toBe('custom');
expect(getFormat).toHaveBeenCalledTimes(1);
// underlying formatter is not called because custom label can be used directly
expect(getFormat).toHaveBeenCalledTimes(0);
});

test('creates custom format for terms', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from './constants';
import { RangePopover } from './advanced_editor';
import { DragDropBuckets } from '../shared_components';
import { EuiFieldText } from '@elastic/eui';

const dataPluginMockValue = dataPluginMock.createStartContract();
// need to overwrite the formatter field first
Expand Down Expand Up @@ -438,6 +439,63 @@ describe('ranges', () => {
});
});

it('should add a new range with custom label', () => {
const setStateSpy = jest.fn();

const instance = mount(
<InlineOptions
{...defaultOptions}
state={state}
setState={setStateSpy}
columnId="col1"
currentColumn={state.layers.first.columns.col1 as RangeIndexPatternColumn}
layerId="first"
/>
);

// This series of act clojures are made to make it work properly the update flush
act(() => {
instance.find(EuiButtonEmpty).prop('onClick')!({} as ReactMouseEvent);
});

act(() => {
// need another wrapping for this in order to work
instance.update();

expect(instance.find(RangePopover)).toHaveLength(2);

// edit the label and check
instance.find(RangePopover).find(EuiFieldText).first().prop('onChange')!({
target: {
value: 'customlabel',
},
} as React.ChangeEvent<HTMLInputElement>);
jest.advanceTimersByTime(TYPING_DEBOUNCE_TIME * 4);

expect(setStateSpy).toHaveBeenCalledWith({
...state,
layers: {
first: {
...state.layers.first,
columns: {
...state.layers.first.columns,
col1: {
...state.layers.first.columns.col1,
params: {
...state.layers.first.columns.col1.params,
ranges: [
{ from: 0, to: DEFAULT_INTERVAL, label: '' },
{ from: DEFAULT_INTERVAL, to: Infinity, label: 'customlabel' },
],
},
},
},
},
},
});
});
});

it('should open a popover to edit an existing range', () => {
const setStateSpy = jest.fn();

Expand Down

0 comments on commit 0f24e71

Please sign in to comment.