|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +jest.dontMock('../../dashboard/Data/Browser/ScriptResponseModal.react'); |
| 5 | +jest.dontMock('../../components/Field/Field.react'); |
| 6 | +jest.dontMock('../../components/Label/Label.react'); |
| 7 | +jest.dontMock('../../components/Checkbox/Checkbox.react'); |
| 8 | +jest.dontMock('../../components/Toggle/Toggle.react'); |
| 9 | +jest.dontMock('../../components/TextInput/TextInput.react'); |
| 10 | +jest.dontMock('../../components/Dropdown/Dropdown.react'); |
| 11 | +jest.dontMock('../../components/Dropdown/Option.react'); |
| 12 | +jest.dontMock('../../components/Modal/Modal.react'); |
| 13 | +jest.dontMock('../../components/Button/Button.react'); |
| 14 | +jest.dontMock('../../components/Icon/Icon.react'); |
| 15 | +jest.dontMock('../Position'); |
| 16 | + |
| 17 | +// Mock Popover to avoid createPortal issues with react-test-renderer |
| 18 | +jest.mock('../../components/Popover/Popover.react', () => { |
| 19 | + const React = require('react'); |
| 20 | + return class Popover extends React.Component { |
| 21 | + render() { |
| 22 | + return <div>{this.props.children}</div>; |
| 23 | + } |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +import React from 'react'; |
| 28 | +import renderer from 'react-test-renderer'; |
| 29 | +const ScriptResponseModal = require('../../dashboard/Data/Browser/ScriptResponseModal.react').default; |
| 30 | + |
| 31 | +const defaultProps = { |
| 32 | + objectIds: ['obj1'], |
| 33 | + onCancel: jest.fn(), |
| 34 | + onConfirm: jest.fn(), |
| 35 | +}; |
| 36 | + |
| 37 | +describe('ScriptResponseModal', () => { |
| 38 | + describe('checkbox element', () => { |
| 39 | + it('initializes with default false', () => { |
| 40 | + const form = { |
| 41 | + elements: [{ element: 'checkbox', name: 'confirmed', label: 'Confirm' }], |
| 42 | + }; |
| 43 | + const component = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />); |
| 44 | + const instance = component.getInstance(); |
| 45 | + expect(instance.state.formData.confirmed).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('initializes with custom default', () => { |
| 49 | + const form = { |
| 50 | + elements: [{ element: 'checkbox', name: 'confirmed', label: 'Confirm', default: true }], |
| 51 | + }; |
| 52 | + const component = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />); |
| 53 | + const instance = component.getInstance(); |
| 54 | + expect(instance.state.formData.confirmed).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('renders with description', () => { |
| 58 | + const form = { |
| 59 | + elements: [{ |
| 60 | + element: 'checkbox', |
| 61 | + name: 'confirmed', |
| 62 | + label: 'Confirm', |
| 63 | + description: 'Please confirm', |
| 64 | + }], |
| 65 | + }; |
| 66 | + const tree = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />).toJSON(); |
| 67 | + expect(JSON.stringify(tree)).toContain('Please confirm'); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('toggle element', () => { |
| 72 | + it('initializes with default false', () => { |
| 73 | + const form = { |
| 74 | + elements: [{ element: 'toggle', name: 'enabled', label: 'Enable' }], |
| 75 | + }; |
| 76 | + const component = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />); |
| 77 | + const instance = component.getInstance(); |
| 78 | + expect(instance.state.formData.enabled).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it('initializes with custom default true', () => { |
| 82 | + const form = { |
| 83 | + elements: [{ element: 'toggle', name: 'enabled', label: 'Enable', default: true }], |
| 84 | + }; |
| 85 | + const component = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />); |
| 86 | + const instance = component.getInstance(); |
| 87 | + expect(instance.state.formData.enabled).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + it('renders with description', () => { |
| 91 | + const form = { |
| 92 | + elements: [{ |
| 93 | + element: 'toggle', |
| 94 | + name: 'enabled', |
| 95 | + label: 'Enable', |
| 96 | + description: 'Toggle this feature', |
| 97 | + }], |
| 98 | + }; |
| 99 | + const tree = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />).toJSON(); |
| 100 | + expect(JSON.stringify(tree)).toContain('Toggle this feature'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('renders with custom labels', () => { |
| 104 | + const form = { |
| 105 | + elements: [{ |
| 106 | + element: 'toggle', |
| 107 | + name: 'enabled', |
| 108 | + label: 'Enable', |
| 109 | + labelTrue: 'Enabled', |
| 110 | + labelFalse: 'Disabled', |
| 111 | + }], |
| 112 | + }; |
| 113 | + const tree = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />).toJSON(); |
| 114 | + const json = JSON.stringify(tree); |
| 115 | + expect(json).toContain('Enabled'); |
| 116 | + expect(json).toContain('Disabled'); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('textInput element', () => { |
| 121 | + it('initializes with default empty string', () => { |
| 122 | + const form = { |
| 123 | + elements: [{ element: 'textInput', name: 'reason', label: 'Reason' }], |
| 124 | + }; |
| 125 | + const component = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />); |
| 126 | + const instance = component.getInstance(); |
| 127 | + expect(instance.state.formData.reason).toBe(''); |
| 128 | + }); |
| 129 | + |
| 130 | + it('initializes with custom default', () => { |
| 131 | + const form = { |
| 132 | + elements: [{ element: 'textInput', name: 'reason', label: 'Reason', default: 'N/A' }], |
| 133 | + }; |
| 134 | + const component = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />); |
| 135 | + const instance = component.getInstance(); |
| 136 | + expect(instance.state.formData.reason).toBe('N/A'); |
| 137 | + }); |
| 138 | + |
| 139 | + it('renders with placeholder', () => { |
| 140 | + const form = { |
| 141 | + elements: [{ |
| 142 | + element: 'textInput', |
| 143 | + name: 'reason', |
| 144 | + label: 'Reason', |
| 145 | + placeholder: 'Enter reason...', |
| 146 | + }], |
| 147 | + }; |
| 148 | + const tree = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />).toJSON(); |
| 149 | + expect(JSON.stringify(tree)).toContain('Enter reason...'); |
| 150 | + }); |
| 151 | + |
| 152 | + it('renders with description', () => { |
| 153 | + const form = { |
| 154 | + elements: [{ |
| 155 | + element: 'textInput', |
| 156 | + name: 'reason', |
| 157 | + label: 'Reason', |
| 158 | + description: 'Provide a reason', |
| 159 | + }], |
| 160 | + }; |
| 161 | + const tree = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />).toJSON(); |
| 162 | + expect(JSON.stringify(tree)).toContain('Provide a reason'); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('dropDown element (existing)', () => { |
| 167 | + it('initializes with first item value', () => { |
| 168 | + const form = { |
| 169 | + elements: [{ |
| 170 | + element: 'dropDown', |
| 171 | + name: 'role', |
| 172 | + label: 'Role', |
| 173 | + items: [{ title: 'Admin', value: 'admin' }, { title: 'User', value: 'user' }], |
| 174 | + }], |
| 175 | + }; |
| 176 | + const component = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />); |
| 177 | + const instance = component.getInstance(); |
| 178 | + expect(instance.state.formData.role).toBe('admin'); |
| 179 | + }); |
| 180 | + }); |
| 181 | + |
| 182 | + describe('mixed elements', () => { |
| 183 | + it('initializes all element types correctly', () => { |
| 184 | + const form = { |
| 185 | + elements: [ |
| 186 | + { element: 'dropDown', name: 'role', items: [{ title: 'Admin', value: 'admin' }] }, |
| 187 | + { element: 'checkbox', name: 'confirmed', default: true }, |
| 188 | + { element: 'toggle', name: 'enabled' }, |
| 189 | + { element: 'textInput', name: 'reason', default: 'test' }, |
| 190 | + ], |
| 191 | + }; |
| 192 | + const component = renderer.create(<ScriptResponseModal form={form} {...defaultProps} />); |
| 193 | + const instance = component.getInstance(); |
| 194 | + expect(instance.state.formData).toEqual({ |
| 195 | + role: 'admin', |
| 196 | + confirmed: true, |
| 197 | + enabled: false, |
| 198 | + reason: 'test', |
| 199 | + }); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('form submission', () => { |
| 204 | + it('calls onConfirm with formData', () => { |
| 205 | + const onConfirm = jest.fn(); |
| 206 | + const form = { |
| 207 | + elements: [ |
| 208 | + { element: 'checkbox', name: 'confirmed', default: true }, |
| 209 | + { element: 'textInput', name: 'reason', default: 'hello' }, |
| 210 | + ], |
| 211 | + }; |
| 212 | + const component = renderer.create( |
| 213 | + <ScriptResponseModal form={form} objectIds={['obj1']} onCancel={jest.fn()} onConfirm={onConfirm} /> |
| 214 | + ); |
| 215 | + const instance = component.getInstance(); |
| 216 | + instance.handleConfirm(); |
| 217 | + expect(onConfirm).toHaveBeenCalledWith({ confirmed: true, reason: 'hello' }); |
| 218 | + }); |
| 219 | + }); |
| 220 | +}); |
0 commit comments