Skip to content

Commit 9f22073

Browse files
authored
fix(use-dataloader): fix cache array key with false value (#1258)
* fix(use-dataloader): fix case where cache array key with false value was ignored * test(use-dataloader): add test for null value in cache key
1 parent 8686046 commit 9f22073

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@scaleway/use-dataloader': patch
3+
---
4+
5+
Fix case where cache array key with false value was ignored.

packages/use-dataloader/src/__tests__/helpers.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,23 @@ describe('marshalQueryKey', () => {
1212

1313
test('should accept primitive array', () => {
1414
const date = new Date('2021')
15-
expect(marshalQueryKey(['defaultKey', 3, null, date, true])).toStrictEqual(
16-
'defaultKey.3.2021-01-01T00:00:00.000Z.true',
17-
)
15+
expect(
16+
marshalQueryKey(['defaultKey', 3, null, false, date, true]),
17+
).toStrictEqual('defaultKey.3.false.2021-01-01T00:00:00.000Z.true')
1818

1919
expect(
2020
marshalQueryKey(
2121
[
2222
'default key',
2323
['number', 3],
2424
['null', null],
25+
['false', false],
2526
['date', date],
2627
['boolean', true],
2728
].flat(),
2829
),
2930
).toStrictEqual(
30-
'default key.number.3.null.date.2021-01-01T00:00:00.000Z.boolean.true',
31+
'default key.number.3.null.false.false.date.2021-01-01T00:00:00.000Z.boolean.true',
3132
)
3233
})
3334

packages/use-dataloader/src/__tests__/useDataLoader.test.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,18 +675,23 @@ describe('useDataLoader', () => {
675675

676676
test('should use cached data', async () => {
677677
const fakePromise = jest.fn(initialProps.method)
678+
const testDate = new Date()
678679
const { result } = renderHook(
679680
props => [
680681
useDataLoader(props.key, props.method, props.config),
681682
useDataLoader(props.key, props.method, {
682683
...props.config,
683684
enabled: false,
684685
}),
686+
// eslint-disable-next-line no-sparse-arrays
687+
useDataLoader(['test-13', , testDate], props.method, props.config),
688+
useDataLoader(['test-13', null, testDate], props.method, props.config),
685689
],
686690
{
687691
initialProps: {
688692
...initialProps,
689-
key: 'test-13',
693+
// eslint-disable-next-line no-sparse-arrays
694+
key: ['test-13', false, testDate],
690695
method: fakePromise,
691696
},
692697
wrapper,
@@ -710,7 +715,10 @@ describe('useDataLoader', () => {
710715

711716
await waitFor(() => expect(result.current[1]?.isSuccess).toBe(true))
712717
expect(result.current[1]?.isSuccess).toBe(true)
713-
expect(fakePromise).toBeCalledTimes(2)
718+
719+
await waitFor(() => expect(result.current[2]?.isSuccess).toBe(true))
720+
expect(result.current[2]?.data).toBe(true)
721+
expect(fakePromise).toBeCalledTimes(3)
714722
})
715723

716724
test('should be reloaded from dataloader context', async () => {

packages/use-dataloader/src/helpers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import type { KeyType } from './types'
88
export const marshalQueryKey = (queryKey: KeyType) =>
99
Array.isArray(queryKey)
1010
? queryKey
11-
.filter(Boolean)
11+
// For now, we treat null values as if they were undefined
12+
// because we can't stringified them and JSON.stringify is not performant
13+
.filter(value => value !== undefined && value !== null)
1214
.map(subKey => {
1315
if (subKey instanceof Date) {
1416
return subKey.toISOString()

0 commit comments

Comments
 (0)