Skip to content

Commit

Permalink
Merge pull request #5474 from apollographql/promisify-all-tests-and-f…
Browse files Browse the repository at this point in the history
…ix-no-more-mocked-responses-failures

Promise-ify all tests, and pass a Promise reject function to mocking utilities.
  • Loading branch information
benjamn authored Oct 17, 2019
2 parents 6fbecbd + 81e6872 commit 4bee850
Show file tree
Hide file tree
Showing 23 changed files with 1,319 additions and 1,035 deletions.
21 changes: 15 additions & 6 deletions src/__mocks__/mockLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ interface MockApolloLink extends ApolloLink {
// Pass in multiple mocked responses, so that you can test flows that end up
// making multiple queries to the server
export function mockSingleLink(
reject: (reason: any) => any,
...mockedResponses: MockedResponse[]
): MockApolloLink {
return new MockLink(mockedResponses);
return new MockLink(reject, mockedResponses);
}

export function mockObservableLink(): MockSubscriptionLink {
Expand Down Expand Up @@ -45,8 +46,14 @@ export class MockLink extends ApolloLink {
public operation: Operation;
private mockedResponsesByKey: { [key: string]: MockedResponse[] } = {};

constructor(mockedResponses: MockedResponse[]) {
constructor(
private reject: (reason: any) => any,
mockedResponses: MockedResponse[],
) {
super();
if (typeof this.reject !== "function") {
throw new Error("Must pass a failure callback when creating MockLink");
}
mockedResponses.forEach(mockedResponse => {
this.addMockedResponse(mockedResponse);
});
Expand All @@ -67,18 +74,20 @@ export class MockLink extends ApolloLink {
const key = requestToKey(operation);
const responses = this.mockedResponsesByKey[key];
if (!responses || responses.length === 0) {
throw new Error(
this.reject(new Error(
`No more mocked responses for the query: ${print(
operation.query,
)}, variables: ${JSON.stringify(operation.variables)}`,
);
));
return;
}

const { result, error, delay } = responses.shift()!;
if (!result && !error) {
throw new Error(
this.reject(new Error(
`Mocked response should contain either result or error: ${key}`,
);
));
return;
}

return new Observable<FetchResult>(observer => {
Expand Down
7 changes: 5 additions & 2 deletions src/__mocks__/mockQueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import { InMemoryCache } from '../cache/inmemory/inMemoryCache';

// Helper method for the tests that construct a query manager out of a
// a list of mocked responses for a mocked network interface.
export default (...mockedResponses: MockedResponse[]) => {
export default (
reject: (reason: any) => any,
...mockedResponses: MockedResponse[]
) => {
return new QueryManager({
link: mockSingleLink(...mockedResponses),
link: mockSingleLink(reject, ...mockedResponses),
cache: new InMemoryCache({ addTypename: false }),
});
};
7 changes: 5 additions & 2 deletions src/__mocks__/mockWatchQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import mockQueryManager from './mockQueryManager';

import { ObservableQuery } from '../core/ObservableQuery';

export default (...mockedResponses: MockedResponse[]): ObservableQuery<any> => {
const queryManager = mockQueryManager(...mockedResponses);
export default (
reject: (reason: any) => any,
...mockedResponses: MockedResponse[]
): ObservableQuery<any> => {
const queryManager = mockQueryManager(reject, ...mockedResponses);
const firstRequest = mockedResponses[0].request;
return queryManager.watchQuery({
query: firstRequest.query!,
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/__snapshots__/client.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`@connect should run a query with the connection directive and filter arguments and write the result to the correct store key 1`] = `
exports[`@connection should run a query with the connection directive and filter arguments and write the result to the correct store key 1`] = `
Object {
"ROOT_QUERY": Object {
"abc({\\"order\\":\\"popularity\\"})": Array [
Expand All @@ -13,7 +13,7 @@ Object {
}
`;
exports[`@connect should run a query with the connection directive and write the result to the store key defined in the directive 1`] = `
exports[`@connection should run a query with the connection directive and write the result to the store key defined in the directive 1`] = `
Object {
"ROOT_QUERY": Object {
"abc": Array [
Expand Down
Loading

0 comments on commit 4bee850

Please sign in to comment.