Skip to content

Commit

Permalink
Add test to ensure getCurrentResult doesn't have data loss
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Feb 12, 2024
1 parent 5b1853d commit 5d70cd8
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions src/core/__tests__/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
itAsync,
MockLink,
mockSingleLink,
MockSubscriptionLink,
subscribeAndCount,
wait,
} from "../../testing";
Expand Down Expand Up @@ -2389,6 +2390,128 @@ describe("ObservableQuery", () => {
}
);

it("handles multiple calls to getCurrentResult without losing data", async () => {
const query = gql`
{
greeting {
message
... on Greeting @defer {
recipient {
name
}
}
}
}
`;

const link = new MockSubscriptionLink();

const client = new ApolloClient({
link,
cache: new InMemoryCache(),
});

const obs = client.watchQuery({ query });
const stream = new ObservableStream(obs);

setTimeout(() => {
link.simulateResult({
result: {
data: {
greeting: {
message: "Hello world",
__typename: "Greeting",
},
},
hasNext: true,
},
});
});

{
const result = await stream.takeNext();
expect(result.data).toEqual({
greeting: {
message: "Hello world",
__typename: "Greeting",
},
});
}

expect(obs.getCurrentResult().data).toEqual({
greeting: {
message: "Hello world",
__typename: "Greeting",
},
});

expect(obs.getCurrentResult().data).toEqual({
greeting: {
message: "Hello world",
__typename: "Greeting",
},
});

setTimeout(() => {
link.simulateResult(
{
result: {
incremental: [
{
data: {
recipient: {
name: "Alice",
__typename: "Person",
},
__typename: "Greeting",
},
path: ["greeting"],
},
],
hasNext: false,
},
},
true
);
});

{
const result = await stream.takeNext();
expect(result.data).toEqual({
greeting: {
message: "Hello world",
recipient: {
name: "Alice",
__typename: "Person",
},
__typename: "Greeting",
},
});
}

expect(obs.getCurrentResult().data).toEqual({
greeting: {
message: "Hello world",
recipient: {
name: "Alice",
__typename: "Person",
},
__typename: "Greeting",
},
});

expect(obs.getCurrentResult().data).toEqual({
greeting: {
message: "Hello world",
recipient: {
name: "Alice",
__typename: "Person",
},
__typename: "Greeting",
},
});
});

{
type Result = Partial<ApolloQueryResult<{ hello: string }>>;

Expand Down

0 comments on commit 5d70cd8

Please sign in to comment.