Skip to content

Commit

Permalink
Add failing observable query test
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Feb 12, 2024
1 parent 5b1853d commit b169071
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions src/core/__tests__/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3355,3 +3355,108 @@ test("handles changing variables in rapid succession before other request is com
networkStatus: NetworkStatus.ready,
});
});

test("does not return partial cache data when `returnPartialData` is false and new variables are passed in", async () => {
const partialQuery = gql`
query MyCar($id: ID) {
car(id: $id) {
id
make
}
}
`;

const query = gql`
query MyCar($id: ID) {
car(id: $id) {
id
make
model
}
}
`;

const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: new MockLink([
{
request: { query, variables: { id: 2 } },
result: {
data: {
car: { __typename: "Car", id: 2, make: "Ford", model: "Bronco" },
},
},
},
]),
});

cache.writeQuery({
query: partialQuery,
variables: { id: 1 },
data: {
car: {
__typename: "Car",
id: 1,
make: "Ford",
model: "Pinto",
},
},
});

cache.writeQuery({
query,
variables: { id: 2 },
data: {
car: {
__typename: "Car",
id: 2,
make: "Ford",
model: "Bronco",
},
},
});

const observable = client.watchQuery({
query,
variables: { id: 2 },
returnPartialData: false,
notifyOnNetworkStatusChange: true,
});

const stream = new ObservableStream(observable);

expect(await stream.takeNext()).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: {
car: { __typename: "Car", id: 2, make: "Ford", model: "Bronco" },
},
});

observable.reobserve({ variables: { id: 1 } });

expect(await stream.takeNext()).toEqual({
loading: true,
networkStatus: NetworkStatus.setVariables,
data: undefined,
});

expect(observable.getCurrentResult()).toEqual({
loading: true,
networkStatus: NetworkStatus.setVariables,
data: undefined,
});

expect(await stream.takeNext()).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: { __typename: "Car", id: 1, make: "Ford", model: "Pinto" },
});

expect(observable.getCurrentResult()).toEqual({
loading: false,
networkStatus: NetworkStatus.ready,
data: { __typename: "Car", id: 1, make: "Ford", model: "Pinto" },
});
});

0 comments on commit b169071

Please sign in to comment.