diff --git a/package.json b/package.json index bc98acd..be72cd4 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "url": "https://github.com/blitz-js/superjson" }, "devDependencies": { + "@tanstack/query-core": "^4.2.2", "@types/debug": "^4.1.5", "@types/mongodb": "^3.6.12", "benchmark": "^2.1.4", diff --git a/src/index.test.ts b/src/index.test.ts index 174ed8c..1865d0d 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -14,6 +14,7 @@ import { import { ObjectID } from 'mongodb'; import { Decimal } from 'decimal.js'; +import { dehydrate, QueryClient } from '@tanstack/query-core'; const isNode10 = process.version.indexOf('v10') === 0; @@ -1001,6 +1002,64 @@ test('regression: `Object.create(null)` / object without prototype', () => { expect(parsed.date).toBeInstanceOf(Date); }); +describe("ReactQuery. Issue #196: TypeError: Cannot read property 'queries' of undefined", () => { + it('works with an empty dehydrated QueryClient', () => { + const queryClient = new QueryClient(); + const input = { props: { dehydratedState: dehydrate(queryClient) } }; + + const stringified = SuperJSON.stringify(input); + const parsed: any = SuperJSON.parse(stringified); + + expect(parsed).toEqual(input); + }); + + const template = { + id: 'cl6mlkijv00000glajl1xljzl', + label: 'foo', + ownerId: 'cl6cdyozp0007jbul3o4oqoox', + createdAt: new Date('2022-08-09T19:50:25.003Z'), + updatedAt: new Date('2022-08-09T19:50:25.019Z'), + fields: [ + { + id: 'cl6mlkik800010glagppvr24i', + label: 'Property', + type: 'NUMBER', + templateId: 'cl6mlkijv00000glajl1xljzl', + createdAt: new Date('2022-08-09T19:50:25.003Z'), + updatedAt: new Date('2022-08-09T19:50:25.019Z'), + }, + ], + }; + + it('works with simple-key dehydrated QueryClient', () => { + const queryClient = new QueryClient(); + queryClient.setQueryData(['templates'], [template]); + const input = { props: { dehydratedState: dehydrate(queryClient) } }; + + const stringified = SuperJSON.stringify(input); + const parsed: any = SuperJSON.parse(stringified); + + expect(parsed).toEqual(input); + }); + + it('works with double-key dehydrated QueryClient', () => { + const queryClient = new QueryClient(); + queryClient.setQueryData(['templates'], [template]); + + queryClient.setQueryData(['templates', template.id], template); + const input = { props: { dehydratedState: dehydrate(queryClient) } }; + + // INFO: The following code produces the same kind of log described + // on https://github.com/blitz-js/superjson/issues/196 + // console.log(JSON.stringify(input, null, 2)); + + const stringified = SuperJSON.stringify(input); + const parsed: any = SuperJSON.parse(stringified); + + expect(parsed).toEqual(input); + }); +}); + test('prototype pollution - __proto__', () => { expect(() => { SuperJSON.parse( diff --git a/yarn.lock b/yarn.lock index d4c8dde..e8028d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1412,6 +1412,11 @@ dependencies: "@sinonjs/commons" "^1.7.0" +"@tanstack/query-core@^4.2.2": + version "4.2.2" + resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-4.2.2.tgz#134a45887ff3fc8667e81936028c746564c90882" + integrity sha512-cA+hTvYtZU6cTpY1gmfokLWK76mciEkTW1vN2CJ4CZf7xhpO9HmvmzQo9mTygV96a9EGQUuYPt37fYFrsnzmww== + "@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7": version "7.1.12" resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.12.tgz#4d8e9e51eb265552a7e4f1ff2219ab6133bdfb2d"