-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checklist: Untangle server and client list merging (#25956)
Note that both `onboardingChecklist.js` and `jetpack-checklist.js` (yes, we should harmonize filenames, too) had a function duplicated (named `onboardingTasks` and `jetpackTasks()`, respectively). At closer look, that function was essentially merging two objects, and sorting them by key according to an array. I've reshuffled that functionality so that it makes more sense now IMO.
- Loading branch information
Showing
6 changed files
with
184 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** @format */ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { mergeObjectIntoArrayById } from '../util'; | ||
|
||
describe( 'mergeObjectIntoArrayById', () => { | ||
test( 'should produce a new array', () => { | ||
const arr = [ { id: 'a', prop: 'prop' } ]; | ||
const obj = { a: { newProp: 'newProp' } }; | ||
expect( mergeObjectIntoArrayById( arr, obj ) ).not.toBe( arr ); | ||
} ); | ||
|
||
test( 'should produce a new object when merging', () => { | ||
const arr = [ { id: 'a', prop: 'prop' } ]; | ||
const obj = { a: { newProp: 'newProp' } }; | ||
|
||
const result = mergeObjectIntoArrayById( arr, obj ); | ||
expect( result[ 0 ] ).not.toBe( arr[ 0 ] ); | ||
expect( result[ 0 ] ).not.toBe( obj ); | ||
} ); | ||
|
||
test( 'should not replace unchanged objects', () => { | ||
const arr = [ { id: 'a', prop: 'prop' }, { id: 'b' } ]; | ||
const obj = { a: { newProp: 'newProp' } }; | ||
|
||
expect( mergeObjectIntoArrayById( arr, obj )[ 1 ] ).toBe( arr[ 1 ] ); | ||
} ); | ||
|
||
test( 'should overwrite existing props', () => { | ||
const arr = [ { id: 'a', prop: 'prop' } ]; | ||
const obj = { a: { prop: 'updated' } }; | ||
|
||
expect( mergeObjectIntoArrayById( arr, obj ) ).toEqual( [ | ||
{ | ||
id: 'a', | ||
prop: 'updated', | ||
}, | ||
] ); | ||
} ); | ||
|
||
test( 'should keep existing props', () => { | ||
const arr = [ { id: 'a', prop: 'prop', untouched: 'stay-the-same' } ]; | ||
const obj = { a: { prop: 'updated' } }; | ||
|
||
expect( mergeObjectIntoArrayById( arr, obj ) ).toEqual( [ | ||
{ | ||
id: 'a', | ||
prop: 'updated', | ||
untouched: 'stay-the-same', | ||
}, | ||
] ); | ||
} ); | ||
|
||
test( 'should add new props', () => { | ||
const arr = [ { id: 'a', prop: 'prop' } ]; | ||
const obj = { a: { newProp: 'add me' } }; | ||
|
||
expect( mergeObjectIntoArrayById( arr, obj ) ).toEqual( [ | ||
{ | ||
id: 'a', | ||
prop: 'prop', | ||
newProp: 'add me', | ||
}, | ||
] ); | ||
} ); | ||
|
||
test( 'should ignore object keys not present in the array', () => { | ||
const arr = [ { id: 'a', prop: 'prop' } ]; | ||
const obj = { c: { ignore: 'me' } }; | ||
|
||
expect( mergeObjectIntoArrayById( arr, obj ) ).toEqual( [ | ||
{ | ||
id: 'a', | ||
prop: 'prop', | ||
}, | ||
] ); | ||
} ); | ||
} ); |
Oops, something went wrong.