|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import { act } from 'react-dom/test-utils'; |
| 9 | +import { setup, SetupResult, getProcessorValue } from './processor.helpers'; |
| 10 | + |
| 11 | +const CIRCLE_TYPE = 'circle'; |
| 12 | + |
| 13 | +describe('Processor: Circle', () => { |
| 14 | + let onUpdate: jest.Mock; |
| 15 | + let testBed: SetupResult; |
| 16 | + |
| 17 | + beforeAll(() => { |
| 18 | + jest.useFakeTimers(); |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(() => { |
| 22 | + jest.useRealTimers(); |
| 23 | + }); |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + onUpdate = jest.fn(); |
| 27 | + |
| 28 | + await act(async () => { |
| 29 | + testBed = await setup({ |
| 30 | + value: { |
| 31 | + processors: [], |
| 32 | + }, |
| 33 | + onFlyoutOpen: jest.fn(), |
| 34 | + onUpdate, |
| 35 | + }); |
| 36 | + }); |
| 37 | + testBed.component.update(); |
| 38 | + const { |
| 39 | + actions: { addProcessor, addProcessorType }, |
| 40 | + } = testBed; |
| 41 | + // Open the processor flyout |
| 42 | + addProcessor(); |
| 43 | + |
| 44 | + // Add type (the other fields are not visible until a type is selected) |
| 45 | + await addProcessorType(CIRCLE_TYPE); |
| 46 | + }); |
| 47 | + |
| 48 | + test('prevents form submission if required fields are not provided', async () => { |
| 49 | + const { |
| 50 | + actions: { saveNewProcessor }, |
| 51 | + form, |
| 52 | + } = testBed; |
| 53 | + |
| 54 | + // Click submit button with only the type defined |
| 55 | + await saveNewProcessor(); |
| 56 | + |
| 57 | + // Expect form error as "field" and "shape_type" are required parameters |
| 58 | + expect(form.getErrorsMessages()).toEqual([ |
| 59 | + 'A field value is required.', |
| 60 | + 'A shape type value is required.', |
| 61 | + ]); |
| 62 | + }); |
| 63 | + |
| 64 | + test('saves with required parameter values', async () => { |
| 65 | + const { |
| 66 | + actions: { saveNewProcessor }, |
| 67 | + form, |
| 68 | + } = testBed; |
| 69 | + |
| 70 | + // Add "field" value (required) |
| 71 | + form.setInputValue('fieldNameField.input', 'field_1'); |
| 72 | + // Save the field |
| 73 | + form.setSelectValue('shapeSelectorField', 'shape'); |
| 74 | + // Set the error distance |
| 75 | + form.setInputValue('errorDistanceField.input', '10'); |
| 76 | + |
| 77 | + await saveNewProcessor(); |
| 78 | + |
| 79 | + const processors = getProcessorValue(onUpdate, CIRCLE_TYPE); |
| 80 | + |
| 81 | + expect(processors[0].circle).toEqual({ |
| 82 | + field: 'field_1', |
| 83 | + error_distance: 10, |
| 84 | + shape_type: 'shape', |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + test('allows optional parameters to be set', async () => { |
| 89 | + const { |
| 90 | + actions: { saveNewProcessor }, |
| 91 | + form, |
| 92 | + } = testBed; |
| 93 | + |
| 94 | + // Add "field" value (required) |
| 95 | + form.setInputValue('fieldNameField.input', 'field_1'); |
| 96 | + // Select the shape |
| 97 | + form.setSelectValue('shapeSelectorField', 'geo_shape'); |
| 98 | + // Add "target_field" value |
| 99 | + form.setInputValue('targetField.input', 'target_field'); |
| 100 | + |
| 101 | + form.setInputValue('errorDistanceField.input', '10'); |
| 102 | + |
| 103 | + // Save the field with new changes |
| 104 | + await saveNewProcessor(); |
| 105 | + |
| 106 | + const processors = getProcessorValue(onUpdate, CIRCLE_TYPE); |
| 107 | + expect(processors[0].circle).toEqual({ |
| 108 | + field: 'field_1', |
| 109 | + error_distance: 10, |
| 110 | + shape_type: 'geo_shape', |
| 111 | + target_field: 'target_field', |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments