Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactivity API: Add tests for handling arrays in deepMerge() #66218

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 } ] } );
} );
} );
} );
} );
Loading