Skip to content

Commit

Permalink
New awaitRefetchQueries tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hwillson committed Jul 23, 2018
1 parent 4315a1a commit 5e9a6ce
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions packages/apollo-client/src/core/__tests__/QueryManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3933,6 +3933,7 @@ describe('QueryManager', () => {
});
});
});

describe('refetchQueries', () => {
const oldWarn = console.warn;
let warned: any;
Expand Down Expand Up @@ -4447,6 +4448,119 @@ describe('QueryManager', () => {
done();
});
});

describe('awaitRefetchQueries', () => {
function awaitRefetchTest({ awaitRefetchQueries }) {
const query = gql`
query getAuthors($id: ID!) {
author(id: $id) {
firstName
lastName
}
}
`;

const queryData = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};

const mutation = gql`
mutation changeAuthorName {
changeAuthorName(newName: "Jack Smith") {
firstName
lastName
}
}
`;

const mutationData = {
changeAuthorName: {
firstName: 'Jack',
lastName: 'Smith',
},
};

const secondReqData = {
author: {
firstName: 'Jane',
lastName: 'Johnson',
},
};

const variables = { id: '1234' };

const queryManager = mockQueryManager(
{
request: { query, variables },
result: { data: queryData },
},
{
request: { query: mutation },
result: { data: mutationData },
},
{
request: { query, variables },
result: { data: secondReqData },
},
);

const observable = queryManager.watchQuery<any>({
query,
variables,
notifyOnNetworkStatusChange: false,
});

let mutationComplete = false;
return observableToPromise(
{ observable },
result => {
expect(stripSymbols(result.data)).toEqual(queryData);
const mutateOptions = {
mutation,
refetchQueries: ['getAuthors'],
};
if (awaitRefetchQueries) {
mutateOptions.awaitRefetchQueries = awaitRefetchQueries;
}
queryManager.mutate(mutateOptions).then(() => {
mutationComplete = true;
});
},
result => {
if (awaitRefetchQueries) {
expect(mutationComplete).not.toBeTruthy();
} else {
expect(mutationComplete).toBeTruthy();
}
expect(stripSymbols(observable.currentResult().data)).toEqual(
secondReqData,
);
expect(stripSymbols(result.data)).toEqual(secondReqData);
},
);
}

it(
'should not wait for `refetchQueries` to complete before resolving ' +
'the mutation, when `awaitRefetchQueries` is falsy',
() => {
awaitRefetchTest({ awaitRefetchQueries: undefined });
awaitRefetchTest({ awaitRefetchQueries: false });
},
);

it(
'should wait for `refetchQueries` to complete before resolving ' +
'the mutation, when `awaitRefetchQueries` is `true`',
() => {
awaitRefetchTest({ awaitRefetchQueries: true });
},
);
});

describe('store watchers', () => {
it('does not fill up the store on resolved queries', () => {
const query1 = gql`
Expand Down

0 comments on commit 5e9a6ce

Please sign in to comment.