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:
@ -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']);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user