@@ -135,6 +135,51 @@ describe('Utils tests', () => {
135135 /* eslint-enable */
136136 } )
137137
138+ describe ( 'empty path' , ( ) => {
139+ it ( 'should return the new value as-is when "updatePath" is empty' , ( ) => {
140+ const oldData = { title : 'Hello world' }
141+
142+ expect ( updateNestedDataByPath ( oldData , [ ] , 'replaced' ) ) . toBe ( 'replaced' )
143+ } )
144+ } )
145+
146+ describe ( 'array nested path' , ( ) => {
147+ it ( 'should update nested data inside an array correctly' , ( ) => {
148+ const oldData = [
149+ { id : 1 , title : 'first' } ,
150+ { id : 2 , title : 'second' } ,
151+ ]
152+
153+ const newData = updateNestedDataByPath (
154+ oldData ,
155+ [ '1' , 'title' ] ,
156+ 'updated' ,
157+ )
158+
159+ expect ( newData ) . not . toBe ( oldData )
160+ expect ( newData ) . toMatchInlineSnapshot ( `
161+ [
162+ {
163+ "id": 1,
164+ "title": "first",
165+ },
166+ {
167+ "id": 2,
168+ "title": "updated",
169+ },
170+ ]
171+ ` )
172+ } )
173+ } )
174+
175+ describe ( 'primitive' , ( ) => {
176+ it ( 'should return primitive data as-is when it is not an Object/Array/Map/Set' , ( ) => {
177+ expect ( updateNestedDataByPath ( 42 , [ 'x' ] , 'new' ) ) . toBe ( 42 )
178+ expect ( updateNestedDataByPath ( 'hello' , [ 'x' ] , 'new' ) ) . toBe ( 'hello' )
179+ expect ( updateNestedDataByPath ( null , [ 'x' ] , 'new' ) ) . toBe ( null )
180+ } )
181+ } )
182+
138183 describe ( 'nested data' , ( ) => {
139184 it ( 'should update data correctly' , ( ) => {
140185 /* eslint-disable cspell/spellchecker */
0 commit comments