Skip to content
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
45 changes: 45 additions & 0 deletions packages/query-devtools/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,51 @@ describe('Utils tests', () => {
/* eslint-enable */
})

describe('empty path', () => {
it('should return the new value as-is when "updatePath" is empty', () => {
const oldData = { title: 'Hello world' }

expect(updateNestedDataByPath(oldData, [], 'replaced')).toBe('replaced')
})
})

describe('array nested path', () => {
it('should update nested data inside an array correctly', () => {
const oldData = [
{ id: 1, title: 'first' },
{ id: 2, title: 'second' },
]

const newData = updateNestedDataByPath(
oldData,
['1', 'title'],
'updated',
)

expect(newData).not.toBe(oldData)
expect(newData).toMatchInlineSnapshot(`
[
{
"id": 1,
"title": "first",
},
{
"id": 2,
"title": "updated",
},
]
`)
})
})

describe('primitive', () => {
it('should return primitive data as-is when it is not an Object/Array/Map/Set', () => {
expect(updateNestedDataByPath(42, ['x'], 'new')).toBe(42)
expect(updateNestedDataByPath('hello', ['x'], 'new')).toBe('hello')
expect(updateNestedDataByPath(null, ['x'], 'new')).toBe(null)
})
})

describe('nested data', () => {
it('should update data correctly', () => {
/* eslint-disable cspell/spellchecker */
Expand Down
Loading