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

Implement compareResultsUsingQuery helper function #10237

Merged
merged 3 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
348 changes: 348 additions & 0 deletions src/react/hooks/__tests__/compareResults.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,348 @@
import { gql } from "../../../core";
import { compareResultsUsingQuery } from "../compareResults";

describe("compareResultsUsingQuery", () => {
it("is importable and a function", () => {
expect(typeof compareResultsUsingQuery).toBe("function");
});

it("works with a basic single-field query", () => {
const query = gql`
query {
hello
}
`;

expect(compareResultsUsingQuery(
query,
{ hello: "hi" },
{ hello: "hi" },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ hello: "hi", unrelated: 1 },
{ hello: "hi", unrelated: 100 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ hello: "hi" },
{ hello: "hey" },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{},
{ hello: "hi" },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ hello: "hi" },
{},
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ hello: "hi" },
null,
)).toBe(false);

expect(compareResultsUsingQuery(
query,
null,
{ hello: "hi" },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
null,
null,
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{},
{},
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ unrelated: "whatever" },
{ unrelated: "no matter" },
)).toBe(true);
});

it("is not confused by properties in different orders", () => {
const query = gql`
query {
a
b
c
}
`;

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ b: 2, c: 3, a: 1 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ d: "bogus", a: 1, b: 2, c: 3 },
{ b: 2, c: 3, a: 1, d: "also bogus" },
)).toBe(true);
});

it("respects the @nonreactive directive on fields", () => {
const query = gql`
query {
a
b
c @nonreactive
}
`;

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 2, c: "different" },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: "different", b: 2, c: 4 },
)).toBe(false);
});

it("respects the @nonreactive directive on inline fragments", () => {
const query = gql`
query {
a
... @nonreactive {
b
c
}
}
`;

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 20, c: 30 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 10, b: 20, c: 30 },
)).toBe(false);
});

it("respects the @nonreactive directive on named fragment ...spreads", () => {
const query = gql`
query {
a
...BCFragment @nonreactive
}

fragment BCFragment on Query {
b
c
}
`;

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 2, c: 30 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 20, c: 3 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 20, c: 30 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 10, b: 20, c: 30 },
)).toBe(false);
});

it("respects the @nonreactive directive on named fragment definitions", () => {
const query = gql`
query {
a
...BCFragment
}

fragment BCFragment on Query @nonreactive {
b
c
}
`;

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 2, c: 30 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 20, c: 3 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 20, c: 30 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 10, b: 20, c: 30 },
)).toBe(false);
});

it("traverses fragments without @nonreactive", () => {
const query = gql`
query {
a
...BCFragment
}

fragment BCFragment on Query {
b
c
}
`;

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 2, c: 3 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ c: 3, a: 1, b: 2 },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 2, c: 30 },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 20, c: 3 },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 20, c: 30 },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ a: 1, b: 2, c: 3 },
{ a: 10, b: 20, c: 30 },
)).toBe(false);
});

it("iterates over array-valued result fields", () => {
const query = gql`
query {
things {
__typename
id
...ThingDetails
}
}

fragment ThingDetails on Thing {
stable
volatile @nonreactive
}
`;

const makeThing = (id: string, stable = 1234) => ({
__typename: "Thing",
id,
stable,
volatile: Math.random(),
});

expect(compareResultsUsingQuery(
query,
{ things: "abc".split("").map(id => makeThing(id)) },
{ things: [makeThing("a"), makeThing("b"), makeThing("c")] },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ things: "abc".split("").map(id => makeThing(id)) },
{ things: "not an array" },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ things: {} },
{ things: [] },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ things: [] },
{ things: {} },
)).toBe(false);

benjamn marked this conversation as resolved.
Show resolved Hide resolved
expect(compareResultsUsingQuery(
query,
{ things: "ab".split("").map(id => makeThing(id)) },
{ things: [makeThing("a"), makeThing("b")] },
)).toBe(true);

expect(compareResultsUsingQuery(
query,
{ things: "ab".split("").map(id => makeThing(id)) },
{ things: [makeThing("b"), makeThing("a")] },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ things: "ab".split("").map(id => makeThing(id)) },
{ things: [makeThing("a"), makeThing("b", 2345)] },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ things: "ab".split("").map(id => makeThing(id)) },
{ things: [makeThing("a", 3456), makeThing("b")] },
)).toBe(false);

expect(compareResultsUsingQuery(
query,
{ things: "ab".split("").map(id => makeThing(id)) },
{ things: [makeThing("b"), makeThing("a")] },
)).toBe(false);
});
});
Loading