fix: fix field select options positions after option removal (#5350)

- Adds an util `toSpliced`. We cannot used the native Javascript
`Array.prototype.toSpliced` method as Chromatic servers don't support
it.
- Makes sure Select field options have sequential positions after
removing an option (form validation schema checks that positions are
sequential and considers options invalid otherwise).
This commit is contained in:
Thaïs
2024-05-10 18:31:22 +02:00
committed by GitHub
parent 72521d5554
commit 0fb416d17c
5 changed files with 99 additions and 16 deletions

View File

@ -0,0 +1,37 @@
import { toSpliced } from '../toSpliced';
describe('toSpliced', () => {
it('removes elements from the array starting at the given index', () => {
// Given
const array = ['a', 'b', 'c', 'd', 'e'];
// When
const result = toSpliced(array, 2, 2);
// Then
expect(result).toEqual(['a', 'b', 'e']);
});
it('replaces elements in the array starting at the given index', () => {
// Given
const array = ['a', 'b', 'c', 'd', 'e'];
// When
const result = toSpliced(array, 1, 2, 'x', 'y');
// Then
expect(result).toEqual(['a', 'x', 'y', 'd', 'e']);
});
it('returns a new array without modifying the original array', () => {
// Given
const array = ['a', 'b', 'c'];
// When
const result = toSpliced(array, 0, 1);
// Then
expect(result).toEqual(['b', 'c']);
expect(array).toEqual(['a', 'b', 'c']);
});
});