Skip to content

subscribe: improve coverage #2355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 64 additions & 18 deletions src/subscription/__tests__/subscribe-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ async function createSubscription(
async function expectPromiseToThrow(promise, message) {
try {
await promise();
/* istanbul ignore next */
expect.fail('promise should have thrown but did not');
} catch (error) {
expect(error && error.message).to.equal(message);
Expand Down Expand Up @@ -280,6 +281,7 @@ describe('Subscription Initialization Phase', () => {
},
nonImportantEmail: {
type: EmailEventType,
/* istanbul ignore next (shouldn't be called) */
subscribe() {
didResolveNonImportantEmail = true;
return eventEmitterAsyncIterator(new EventEmitter(), 'event');
Expand Down Expand Up @@ -516,27 +518,9 @@ describe('Subscription Initialization Phase', () => {
}
`);

const pubsub = new EventEmitter();
const rootValue = {
inbox: {
emails: [
{
from: '[email protected]',
subject: 'Hello',
message: 'Hello World',
unread: false,
},
],
},
importantEmail() {
return eventEmitterAsyncIterator(pubsub, 'importantEmail');
},
};

const result = await subscribe({
schema: emailSchema,
document: ast,
rootValue,
variableValues: { priority: 'meow' },
});

Expand Down Expand Up @@ -806,6 +790,68 @@ describe('Subscription Publish Phase', () => {
});
});

it('should not trigger when subscription is thrown', async () => {
const pubsub = new EventEmitter();
const { sendImportantEmail, subscription } = await createSubscription(
pubsub,
);
let payload = subscription.next();

// A new email arrives!
expect(
sendImportantEmail({
from: '[email protected]',
subject: 'Alright',
message: 'Tests are good',
unread: true,
}),
).to.equal(true);

expect(await payload).to.deep.equal({
done: false,
value: {
data: {
importantEmail: {
email: {
from: '[email protected]',
subject: 'Alright',
},
inbox: {
unread: 1,
total: 2,
},
},
},
},
});

payload = subscription.next();

// Throw error
let caughtError;
try {
await subscription.throw('ouch');
} catch (e) {
caughtError = e;
}
expect(caughtError).to.equal('ouch');

// A new email arrives!
expect(
sendImportantEmail({
from: '[email protected]',
subject: 'Alright 2',
message: 'Tests are good 2',
unread: true,
}),
).to.equal(false);

expect(await payload).to.deep.equal({
done: true,
value: undefined,
});
});

it('event order is correct for multiple publishes', async () => {
const pubsub = new EventEmitter();
const { sendImportantEmail, subscription } = await createSubscription(
Expand Down