Skip to content

Commit

Permalink
iAPI: Add tests for handling arrays in deepMerge() (WordPress#66218)
Browse files Browse the repository at this point in the history
* add new tests

* updated tests

* remove one extra test

Co-authored-by: michalczaplinski <[email protected]>
Co-authored-by: DAreRodz <[email protected]>
  • Loading branch information
3 people authored and karthick-murugan committed Nov 13, 2024
1 parent 88d0c33 commit b45f286
Showing 1 changed file with 65 additions and 18 deletions.
83 changes: 65 additions & 18 deletions packages/interactivity/src/proxies/test/deep-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,24 +251,6 @@ describe( 'Interactivity API', () => {
expect( result ).toEqual( { a: 1 } );
} );

it( 'should handle arrays', () => {
const target = { a: [ 1, 2 ] };
const source = { a: [ 3, 4 ] };
const result = {};
deepMerge( result, target );
deepMerge( result, source );
expect( result ).toEqual( { a: [ 3, 4 ] } );
} );

it( 'should handle arrays when overwrite is false', () => {
const target = { a: [ 1, 2 ] };
const source = { a: [ 3, 4 ] };
const result = {};
deepMerge( result, target, false );
deepMerge( result, source, false );
expect( result ).toEqual( { a: [ 1, 2 ] } );
} );

it( 'should handle null values', () => {
const target = { a: 1, b: null };
const source = { b: 2, c: null };
Expand Down Expand Up @@ -500,5 +482,70 @@ describe( 'Interactivity API', () => {
expect( spy ).toHaveBeenCalledTimes( 3 );
expect( deepValue ).toBe( 'value 2' );
} );

describe( 'arrays', () => {
it( 'should handle arrays', () => {
const target = { a: [ 1, 2 ] };
const source = { a: [ 3, 4 ] };
const result = {};
deepMerge( result, target );
deepMerge( result, source );
expect( result ).toEqual( { a: [ 3, 4 ] } );
} );

it( 'should handle arrays when overwrite is false', () => {
const target = { a: [ 1, 2 ] };
const source = { a: [ 3, 4 ] };
const result = {};
deepMerge( result, target, false );
deepMerge( result, source, false );
expect( result ).toEqual( { a: [ 1, 2 ] } );
} );

it( 'should add new array from source if not present in target', () => {
const target = { a: 1 };
const source = { arr: [ 1, 2, 3 ] };
const result = {};
deepMerge( result, target );
deepMerge( result, source );
expect( result ).toEqual( { a: 1, arr: [ 1, 2, 3 ] } );
} );

it( 'should handle nested arrays', () => {
const target = { nested: { arr: [ 1, 2 ] } };
const source = { nested: { arr: [ 3, 4, 5 ] } };
const result = {};
deepMerge( result, target );
deepMerge( result, source );
expect( result ).toEqual( { nested: { arr: [ 3, 4, 5 ] } } );
} );

it( 'should handle object with array as target and object with object as source', () => {
const target = { arr: [ 1, 2, 3 ] };
const source = { arr: { 1: 'two', 3: 'four' } };
const result: any = {};
deepMerge( result, target );
deepMerge( result, source );
expect( result ).toEqual( { arr: { 1: 'two', 3: 'four' } } );
} );

it( 'should handle object with object as target and object with array as source', () => {
const target = { arr: { 0: 'zero', 2: 'two' } };
const source = { arr: [ 'a', 'b', 'c' ] };
const result = {};
deepMerge( result, target );
deepMerge( result, source );
expect( result ).toEqual( { arr: [ 'a', 'b', 'c' ] } );
} );

it( 'should handle objects with arrays containing object elements', () => {
const target = { arr: [ { a: 1 }, { b: 2 } ] };
const source = { arr: [ { a: 2 }, { c: 3 } ] };
const result: any = {};
deepMerge( result, target );
deepMerge( result, source );
expect( result ).toEqual( { arr: [ { a: 2 }, { c: 3 } ] } );
} );
} );
} );
} );

0 comments on commit b45f286

Please sign in to comment.