Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for issue #196 #197

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
59 changes: 59 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down