|
| 1 | +/* SPDX-FileCopyrightText: 2025 Greenbone AG |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import {describe, test, expect, testing} from '@gsa/testing'; |
| 7 | +import {changeInputValue, fireEvent, render, screen} from 'web/testing'; |
| 8 | +import SaveDialog from 'web/components/dialog/SaveDialog'; |
| 9 | +import NumberField from 'web/components/form/NumberField'; |
| 10 | +import TextField from 'web/components/form/TextField'; |
| 11 | +import Checkbox from 'web/components/form/Checkbox'; |
| 12 | +import {useState} from 'react'; |
| 13 | + |
| 14 | +interface TestDialogProps { |
| 15 | + onClose: () => void; |
| 16 | + onSave: (values: {foo?: string; bar: number}) => void; |
| 17 | + foo?: string; |
| 18 | + onFooChange?: (value: string, name?: string) => void; |
| 19 | +} |
| 20 | + |
| 21 | +const TestDialog = ({onClose, onSave, foo, onFooChange}: TestDialogProps) => { |
| 22 | + const [isEnabled, setIsEnabled] = useState<boolean>(false); |
| 23 | + const handleIsEnabledChange = (value: boolean) => { |
| 24 | + setIsEnabled(value); |
| 25 | + }; |
| 26 | + return ( |
| 27 | + <SaveDialog<{foo?: string; isEnabled: boolean}, {bar: number}> |
| 28 | + defaultValues={{bar: 42}} |
| 29 | + title="Some Dialog" |
| 30 | + values={{foo, isEnabled}} |
| 31 | + onClose={onClose} |
| 32 | + onSave={onSave} |
| 33 | + > |
| 34 | + {({values, onValueChange}) => ( |
| 35 | + <> |
| 36 | + <TextField name="foo" value={values.foo} onChange={onFooChange} /> |
| 37 | + <NumberField name="bar" value={values.bar} onChange={onValueChange} /> |
| 38 | + <Checkbox<boolean> |
| 39 | + checked={values.isEnabled} |
| 40 | + name="isEnabled" |
| 41 | + onChange={handleIsEnabledChange} |
| 42 | + /> |
| 43 | + </> |
| 44 | + )} |
| 45 | + </SaveDialog> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +describe('SaveDialog component tests', () => { |
| 50 | + test('should render a SaveDialog', () => { |
| 51 | + const handleClose = testing.fn(); |
| 52 | + const handleSave = testing.fn(); |
| 53 | + render(<TestDialog onClose={handleClose} onSave={handleSave} />); |
| 54 | + |
| 55 | + const dialog = screen.getDialog(); |
| 56 | + expect(dialog).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should call onClose when dialog is closed', () => { |
| 60 | + const handleClose = testing.fn(); |
| 61 | + const handleSave = testing.fn(); |
| 62 | + render(<TestDialog onClose={handleClose} onSave={handleSave} />); |
| 63 | + |
| 64 | + const button = screen.getDialogCloseButton(); |
| 65 | + fireEvent.click(button); |
| 66 | + |
| 67 | + expect(handleClose).toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should call onSave when save button is clicked', () => { |
| 71 | + const handleClose = testing.fn(); |
| 72 | + const handleSave = testing.fn(); |
| 73 | + render(<TestDialog onClose={handleClose} onSave={handleSave} />); |
| 74 | + |
| 75 | + const saveButton = screen.getDialogSaveButton(); |
| 76 | + fireEvent.click(saveButton); |
| 77 | + |
| 78 | + expect(handleSave).toHaveBeenCalled(); |
| 79 | + }); |
| 80 | + |
| 81 | + test('should update value in TextField', () => { |
| 82 | + const handleClose = testing.fn(); |
| 83 | + const handleSave = testing.fn(); |
| 84 | + const handleFooChange = testing.fn(); |
| 85 | + const {rerender} = render( |
| 86 | + <TestDialog |
| 87 | + foo="something" |
| 88 | + onClose={handleClose} |
| 89 | + onFooChange={handleFooChange} |
| 90 | + onSave={handleSave} |
| 91 | + />, |
| 92 | + ); |
| 93 | + |
| 94 | + const saveButton = screen.getDialogSaveButton(); |
| 95 | + fireEvent.click(saveButton); |
| 96 | + |
| 97 | + expect(handleSave).toHaveBeenCalledWith({ |
| 98 | + foo: 'something', |
| 99 | + bar: 42, |
| 100 | + }); |
| 101 | + |
| 102 | + const textField = screen.getByName('foo'); |
| 103 | + changeInputValue(textField, 'test value'); |
| 104 | + expect(handleFooChange).toHaveBeenCalledWith('test value', 'foo'); |
| 105 | + |
| 106 | + handleSave.mockClear(); |
| 107 | + |
| 108 | + rerender( |
| 109 | + <TestDialog |
| 110 | + foo="test value" |
| 111 | + onClose={handleClose} |
| 112 | + onFooChange={handleFooChange} |
| 113 | + onSave={handleSave} |
| 114 | + />, |
| 115 | + ); |
| 116 | + |
| 117 | + fireEvent.click(saveButton); |
| 118 | + |
| 119 | + expect(handleSave).toHaveBeenCalledWith({ |
| 120 | + foo: 'test value', |
| 121 | + bar: 42, |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments