|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { fireEvent, screen } from '@testing-library/svelte'; |
| 3 | +import { renderSchemaWithSingleProperty } from './property_test_utils'; |
| 4 | + |
| 5 | +describe('Reset properties to their default values', async () => { |
| 6 | + it('Reset object with default value on top-level property', async () => { |
| 7 | + const { component, onChange } = renderSchemaWithSingleProperty( |
| 8 | + { |
| 9 | + type: 'object', |
| 10 | + default: { key1: { key2: 'foo' } }, |
| 11 | + properties: { key1: { type: 'object', properties: { key2: { type: 'string' } } } }, |
| 12 | + required: ['key1'] |
| 13 | + }, |
| 14 | + true |
| 15 | + ); |
| 16 | + expect(component.getArguments()).deep.eq({ testProp: { key1: { key2: 'foo' } } }); |
| 17 | + await fireEvent.input(screen.getByRole('textbox'), { target: { value: 'bar' } }); |
| 18 | + expect(onChange).toHaveBeenCalledWith({ testProp: { key1: { key2: 'bar' } } }); |
| 19 | + await fireEvent.click(screen.getByRole('button', { name: 'Reset' })); |
| 20 | + expect(onChange).toHaveBeenCalledWith({ testProp: { key1: { key2: 'foo' } } }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('Object with default on nested object does not have the Reset button', async () => { |
| 24 | + const { component, onChange } = renderSchemaWithSingleProperty( |
| 25 | + { |
| 26 | + type: 'object', |
| 27 | + properties: { |
| 28 | + key1: { |
| 29 | + type: 'object', |
| 30 | + default: { key2: 'foo' }, |
| 31 | + properties: { key2: { type: 'string' } } |
| 32 | + } |
| 33 | + }, |
| 34 | + required: ['key1'] |
| 35 | + }, |
| 36 | + true |
| 37 | + ); |
| 38 | + expect(component.getArguments()).deep.eq({ testProp: { key1: { key2: 'foo' } } }); |
| 39 | + await fireEvent.input(screen.getByRole('textbox'), { target: { value: 'bar' } }); |
| 40 | + expect(onChange).toHaveBeenCalledWith({ testProp: { key1: { key2: 'bar' } } }); |
| 41 | + expect(screen.queryAllByRole('button', { name: 'Reset' })).toHaveLength(0); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Reset tuple with default value on top-level property', async () => { |
| 45 | + const { component, onChange } = renderSchemaWithSingleProperty( |
| 46 | + { |
| 47 | + type: 'array', |
| 48 | + minItems: 2, |
| 49 | + maxItems: 2, |
| 50 | + items: [{ type: 'integer' }, { type: 'integer' }], |
| 51 | + default: [1, 2] |
| 52 | + }, |
| 53 | + true |
| 54 | + ); |
| 55 | + expect(component.getArguments()).deep.eq({ testProp: [1, 2] }); |
| 56 | + const [input1, input2] = screen.getAllByRole('spinbutton'); |
| 57 | + expect(input1).toHaveValue(1); |
| 58 | + expect(input2).toHaveValue(2); |
| 59 | + await fireEvent.input(input1, { target: { value: '3' } }); |
| 60 | + expect(onChange).toHaveBeenCalledWith({ testProp: [3, 2] }); |
| 61 | + await fireEvent.click(screen.getByRole('button', { name: 'Reset' })); |
| 62 | + expect(onChange).toHaveBeenCalledWith({ testProp: [1, 2] }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('Tuple with default on nested items does not have the Reset button', async () => { |
| 66 | + const { component, onChange } = renderSchemaWithSingleProperty( |
| 67 | + { |
| 68 | + type: 'array', |
| 69 | + minItems: 2, |
| 70 | + maxItems: 2, |
| 71 | + items: [ |
| 72 | + { type: 'integer', default: 1 }, |
| 73 | + { type: 'integer', default: 2 } |
| 74 | + ] |
| 75 | + }, |
| 76 | + true |
| 77 | + ); |
| 78 | + expect(component.getArguments()).deep.eq({ testProp: [1, 2] }); |
| 79 | + const [input1, input2] = screen.getAllByRole('spinbutton'); |
| 80 | + expect(input1).toHaveValue(1); |
| 81 | + expect(input2).toHaveValue(2); |
| 82 | + await fireEvent.input(input1, { target: { value: '3' } }); |
| 83 | + expect(onChange).toHaveBeenCalledWith({ testProp: [3, 2] }); |
| 84 | + expect(screen.queryAllByRole('button', { name: 'Reset' })).toHaveLength(0); |
| 85 | + }); |
| 86 | + |
| 87 | + it('Reset array with default value on top-level property', async () => { |
| 88 | + const { component, onChange } = renderSchemaWithSingleProperty( |
| 89 | + { |
| 90 | + type: 'array', |
| 91 | + items: { type: 'string' }, |
| 92 | + default: ['a', 'b'] |
| 93 | + }, |
| 94 | + true |
| 95 | + ); |
| 96 | + expect(component.getArguments()).deep.eq({ testProp: ['a', 'b'] }); |
| 97 | + const [input1, input2] = screen.getAllByRole('textbox'); |
| 98 | + expect(input1).toHaveValue('a'); |
| 99 | + expect(input2).toHaveValue('b'); |
| 100 | + await fireEvent.input(input1, { target: { value: 'x' } }); |
| 101 | + expect(onChange).toHaveBeenCalledWith({ testProp: ['x', 'b'] }); |
| 102 | + await fireEvent.click(screen.getByRole('button', { name: 'Reset' })); |
| 103 | + expect(onChange).toHaveBeenCalledWith({ testProp: ['a', 'b'] }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('Array with default on nested items does not have the Reset button', async () => { |
| 107 | + const { component, onChange } = renderSchemaWithSingleProperty( |
| 108 | + { |
| 109 | + type: 'array', |
| 110 | + items: { type: 'string', default: 'a' }, |
| 111 | + minItems: 2 |
| 112 | + }, |
| 113 | + true |
| 114 | + ); |
| 115 | + expect(component.getArguments()).deep.eq({ testProp: ['a', 'a'] }); |
| 116 | + const [input1, input2] = screen.getAllByRole('textbox'); |
| 117 | + expect(input1).toHaveValue('a'); |
| 118 | + expect(input2).toHaveValue('a'); |
| 119 | + await fireEvent.input(input1, { target: { value: 'x' } }); |
| 120 | + expect(onChange).toHaveBeenCalledWith({ testProp: ['x', 'a'] }); |
| 121 | + expect(screen.queryAllByRole('button', { name: 'Reset' })).toHaveLength(0); |
| 122 | + }); |
| 123 | +}); |
0 commit comments