Skip to content

Commit

Permalink
more explicit test of case with unsubscribed query
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonphillips committed Sep 27, 2016
1 parent 3d02c5e commit 82db6e5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ export class QueryManager {
private refetchQueryByName(queryName: string) {
const refetchedQueries = this.queryIdsByName[queryName];
// Warn if the query named does not exist (misnamed, or merely not yet fetched)
if (!refetchedQueries) {
if (refetchedQueries === undefined) {
console.warn(`Warning: unknown query with name ${queryName} asked to refetch`);
} else {
refetchedQueries.forEach((queryId) => {
Expand Down
77 changes: 77 additions & 0 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3276,6 +3276,83 @@ describe('QueryManager', () => {
},
});
});

it('should ignore without warning a query name that is asked to refetch with no active subscriptions', (done) => {
const oldWarn = console.warn;
let timesWarned = 0;
console.warn = (...args: any[]) => {
timesWarned++;
};

const mutation = gql`
mutation changeAuthorName {
changeAuthorName(newName: "Jack Smith") {
firstName
lastName
}
}`;
const mutationData = {
changeAuthorName: {
firstName: 'Jack',
lastName: 'Smith',
},
};
const query = gql`
query getAuthors {
author {
firstName
lastName
}
}`;
const data = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};
const secondReqData = {
author: {
firstName: 'Jane',
lastName: 'Johnson',
},
};
const queryManager = mockQueryManager(
{
request: { query },
result: { data },
},
{
request: { query },
result: { data: secondReqData },
},
{
request: { query: mutation },
result: { data: mutationData },
}
);

let resultsReceived = 0;
const temporarySubscription = queryManager.watchQuery({ query }).subscribe({
next(result) {
if (resultsReceived === 0) {
assert.deepEqual(result.data, data);
// unsubscribe before the mutation
temporarySubscription.unsubscribe();
queryManager.mutate({ mutation, refetchQueries: ['getAuthors'] });
} else if (resultsReceived === 1) {
// should not be subscribed
done(new Error('Returned data when it should have been unsubscribed.'));
}
resultsReceived++;
},
});
// no warning should have been fired
setTimeout(() => {
assert.equal(timesWarned, 0);
console.warn = oldWarn;
done();
}, 10);
});
});

describe('result transformation', () => {
Expand Down

0 comments on commit 82db6e5

Please sign in to comment.