|
| 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-test-renderer'; |
| 9 | + |
| 10 | +import { createFleetTestRendererMock } from '../../../../../../mock'; |
| 11 | + |
| 12 | +import { useFleetServerHostsForm } from './use_fleet_server_host_form'; |
| 13 | + |
| 14 | +jest.mock('../../services/agent_and_policies_count', () => ({ |
| 15 | + ...jest.requireActual('../../services/agent_and_policies_count'), |
| 16 | + getAgentAndPolicyCount: () => ({ agentCount: 0, agentPolicyCount: 0 }), |
| 17 | +})); |
| 18 | +jest.mock('../../hooks/use_confirm_modal', () => ({ |
| 19 | + ...jest.requireActual('../../hooks/use_confirm_modal'), |
| 20 | + useConfirmModal: () => ({ confirm: () => true }), |
| 21 | +})); |
| 22 | + |
| 23 | +describe('useFleetServerHostsForm', () => { |
| 24 | + it('should not allow to submit an invalid form', async () => { |
| 25 | + const testRenderer = createFleetTestRendererMock(); |
| 26 | + const onSucess = jest.fn(); |
| 27 | + const { result } = testRenderer.renderHook(() => useFleetServerHostsForm([], onSucess)); |
| 28 | + |
| 29 | + act(() => |
| 30 | + result.current.fleetServerHostsInput.props.onChange(['http://test.fr', 'http://test.fr']) |
| 31 | + ); |
| 32 | + |
| 33 | + await act(() => result.current.submit()); |
| 34 | + |
| 35 | + expect(result.current.fleetServerHostsInput.props.errors).toMatchInlineSnapshot(` |
| 36 | + Array [ |
| 37 | + Object { |
| 38 | + "index": 0, |
| 39 | + "message": "Duplicate URL", |
| 40 | + }, |
| 41 | + Object { |
| 42 | + "index": 1, |
| 43 | + "message": "Duplicate URL", |
| 44 | + }, |
| 45 | + ] |
| 46 | + `); |
| 47 | + expect(onSucess).not.toBeCalled(); |
| 48 | + expect(result.current.isDisabled).toBeTruthy(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should submit a valid form', async () => { |
| 52 | + const testRenderer = createFleetTestRendererMock(); |
| 53 | + const onSucess = jest.fn(); |
| 54 | + testRenderer.startServices.http.post.mockResolvedValue({}); |
| 55 | + const { result } = testRenderer.renderHook(() => useFleetServerHostsForm([], onSucess)); |
| 56 | + |
| 57 | + act(() => result.current.fleetServerHostsInput.props.onChange(['http://test.fr'])); |
| 58 | + |
| 59 | + await act(() => result.current.submit()); |
| 60 | + expect(onSucess).toBeCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should allow the user to correct and submit a invalid form', async () => { |
| 64 | + const testRenderer = createFleetTestRendererMock(); |
| 65 | + const onSucess = jest.fn(); |
| 66 | + testRenderer.startServices.http.post.mockResolvedValue({}); |
| 67 | + const { result } = testRenderer.renderHook(() => useFleetServerHostsForm([], onSucess)); |
| 68 | + |
| 69 | + act(() => |
| 70 | + result.current.fleetServerHostsInput.props.onChange(['http://test.fr', 'http://test.fr']) |
| 71 | + ); |
| 72 | + |
| 73 | + await act(() => result.current.submit()); |
| 74 | + expect(onSucess).not.toBeCalled(); |
| 75 | + expect(result.current.isDisabled).toBeTruthy(); |
| 76 | + |
| 77 | + act(() => result.current.fleetServerHostsInput.props.onChange(['http://test.fr'])); |
| 78 | + expect(result.current.isDisabled).toBeFalsy(); |
| 79 | + |
| 80 | + await act(() => result.current.submit()); |
| 81 | + expect(onSucess).toBeCalled(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments