Skip to content

Commit

Permalink
adjust existing test to prevent regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Sep 22, 2023
1 parent 254ffbf commit 5ae0a2e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .changeset/early-snakes-cheer.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@apollo/client": patch
---

keep `options` reference stable when passing it thorugh QueryManager
keep `options` reference stable when passing it through QueryManager
85 changes: 57 additions & 28 deletions src/core/__tests__/fetchPolicies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,16 +814,24 @@ describe("cache-and-network", function () {

describe("nextFetchPolicy", () => {
type TData = {
linkCounter: number;
opName: string;
opVars: Record<string, any>;
echo: {
linkCounter: number;
opName: string;
opVars: Record<string, any>;
};
};

type TVars = {
refetching?: boolean;
};

const EchoQuery: TypedDocumentNode<TData> = gql`
query EchoQuery {
linkCounter
opName
opVars
echo {
linkCounter
opName
opVars
}
}
`;

Expand All @@ -835,9 +843,12 @@ describe("nextFetchPolicy", () => {
setTimeout(() => {
observer.next({
data: {
linkCounter: ++linkCounter,
opName: request.operationName,
opVars: request.variables,
echo: {
__typename: "Echo",
linkCounter: ++linkCounter,
opName: request.operationName,
opVars: request.variables,
},
},
});
observer.complete();
Expand All @@ -846,9 +857,9 @@ describe("nextFetchPolicy", () => {
);
}

const checkNextFetchPolicy = <TData, TVars extends object>(args: {
const checkNextFetchPolicy = (args: {
fetchPolicy: WatchQueryFetchPolicy;
nextFetchPolicy: WatchQueryOptions<TVars, TData>["nextFetchPolicy"];
nextFetchPolicy: WatchQueryOptions<{}, TData>["nextFetchPolicy"];
useDefaultOptions: boolean;
onResult(info: {
count: number;
Expand All @@ -867,7 +878,9 @@ describe("nextFetchPolicy", () => {
(resolve, reject) => {
const client = new ApolloClient({
link: makeLink(),
cache: new InMemoryCache(),
cache: new InMemoryCache({
addTypename: true,
}),
defaultOptions: {
watchQuery: args.useDefaultOptions
? {
Expand Down Expand Up @@ -917,7 +930,8 @@ describe("nextFetchPolicy", () => {
}) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 1,
opName: "EchoQuery",
opVars: {},
Expand All @@ -930,7 +944,8 @@ describe("nextFetchPolicy", () => {
refetching: true,
})
.then((result) => {
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -941,7 +956,8 @@ describe("nextFetchPolicy", () => {
.catch(reject);
} else if (count === 2) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -959,7 +975,8 @@ describe("nextFetchPolicy", () => {
})
.then((result) => {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 3,
opName: "EchoQuery",
opVars: {
Expand All @@ -973,7 +990,8 @@ describe("nextFetchPolicy", () => {
expect(observable.options.fetchPolicy).toBe("cache-first");
} else if (count === 3) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 3,
opName: "EchoQuery",
opVars: {
Expand Down Expand Up @@ -1044,7 +1062,8 @@ describe("nextFetchPolicy", () => {
}) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 1,
opName: "EchoQuery",
opVars: {},
Expand All @@ -1057,7 +1076,8 @@ describe("nextFetchPolicy", () => {
refetching: true,
})
.then((result) => {
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1068,7 +1088,8 @@ describe("nextFetchPolicy", () => {
.catch(reject);
} else if (count === 2) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1086,7 +1107,8 @@ describe("nextFetchPolicy", () => {
})
.then((result) => {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 3,
opName: "EchoQuery",
opVars: {
Expand All @@ -1100,7 +1122,8 @@ describe("nextFetchPolicy", () => {
// expect(observable.options.fetchPolicy).toBe("cache-and-network");
} else if (count === 3) {
expect(result.loading).toBe(true);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1112,7 +1135,8 @@ describe("nextFetchPolicy", () => {
expect(observable.options.fetchPolicy).toBe("cache-first");
} else if (count === 4) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 3,
opName: "EchoQuery",
opVars: {
Expand Down Expand Up @@ -1191,7 +1215,8 @@ describe("nextFetchPolicy", () => {
}) => {
if (count === 1) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 1,
opName: "EchoQuery",
opVars: {},
Expand All @@ -1204,7 +1229,8 @@ describe("nextFetchPolicy", () => {
refetching: true,
})
.then((result) => {
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1215,7 +1241,8 @@ describe("nextFetchPolicy", () => {
.catch(reject);
} else if (count === 2) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1233,7 +1260,8 @@ describe("nextFetchPolicy", () => {
})
.then((result) => {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand All @@ -1250,7 +1278,8 @@ describe("nextFetchPolicy", () => {
expect(observable.options.fetchPolicy).toBe("cache-first");
} else if (count === 3) {
expect(result.loading).toBe(false);
expect(result.data).toEqual({
expect(result.data.echo).toEqual({
__typename: "Echo",
linkCounter: 2,
opName: "EchoQuery",
opVars: {
Expand Down

0 comments on commit 5ae0a2e

Please sign in to comment.