From f81060bd21017ee5cdb1cf541aa0cedf56bc1137 Mon Sep 17 00:00:00 2001 From: Pablo Neves Machado Date: Thu, 10 Jun 2021 10:25:07 +0200 Subject: [PATCH] Add unit test --- .../step_about_rule/field_validators.test.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 x-pack/plugins/security_solution/public/detections/components/rules/step_about_rule/field_validators.test.ts diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/step_about_rule/field_validators.test.ts b/x-pack/plugins/security_solution/public/detections/components/rules/step_about_rule/field_validators.test.ts new file mode 100644 index 00000000000000..72a7a090a0d858 --- /dev/null +++ b/x-pack/plugins/security_solution/public/detections/components/rules/step_about_rule/field_validators.test.ts @@ -0,0 +1,37 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { ValidationFuncArg } from 'src/plugins/es_ui_shared/static/forms/hook_form_lib'; + +import { emptyArrayItem } from './field_validators'; + +describe('emptyArrayItem', () => { + const MESSAGE = 'error message'; + const defaultArgs: ValidationFuncArg = { + path: '', + form: { + getFormData: jest.fn(), + getFields: jest.fn(), + }, + formData: {} as FormData, + errors: [], + value: undefined, + }; + + test('should return an error when value is an empty string', () => { + expect(emptyArrayItem(MESSAGE)({ ...defaultArgs, value: ' ' })).toEqual({ + code: 'ERR_INVALID_CHARS', + message: MESSAGE, + }); + }); + test('should return undefined when value is not a string', () => { + expect(emptyArrayItem(MESSAGE)({ ...defaultArgs, value: 9999 })).toBe(undefined); + }); + test('should return undefined when value is not an empty string', () => { + expect(emptyArrayItem(MESSAGE)({ ...defaultArgs, value: 'non-empty string' })).toBe(undefined); + }); +});