Skip to content

Commit 4d0601b

Browse files
subscribe: improve coverage (#2355)
1 parent d951f46 commit 4d0601b

File tree

1 file changed

+64
-18
lines changed

1 file changed

+64
-18
lines changed

src/subscription/__tests__/subscribe-test.js

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ async function createSubscription(
139139
async function expectPromiseToThrow(promise, message) {
140140
try {
141141
await promise();
142+
/* istanbul ignore next */
142143
expect.fail('promise should have thrown but did not');
143144
} catch (error) {
144145
expect(error && error.message).to.equal(message);
@@ -280,6 +281,7 @@ describe('Subscription Initialization Phase', () => {
280281
},
281282
nonImportantEmail: {
282283
type: EmailEventType,
284+
/* istanbul ignore next (shouldn't be called) */
283285
subscribe() {
284286
didResolveNonImportantEmail = true;
285287
return eventEmitterAsyncIterator(new EventEmitter(), 'event');
@@ -516,27 +518,9 @@ describe('Subscription Initialization Phase', () => {
516518
}
517519
`);
518520

519-
const pubsub = new EventEmitter();
520-
const rootValue = {
521-
inbox: {
522-
emails: [
523-
{
524-
from: 'joe@graphql.org',
525-
subject: 'Hello',
526-
message: 'Hello World',
527-
unread: false,
528-
},
529-
],
530-
},
531-
importantEmail() {
532-
return eventEmitterAsyncIterator(pubsub, 'importantEmail');
533-
},
534-
};
535-
536521
const result = await subscribe({
537522
schema: emailSchema,
538523
document: ast,
539-
rootValue,
540524
variableValues: { priority: 'meow' },
541525
});
542526

@@ -806,6 +790,68 @@ describe('Subscription Publish Phase', () => {
806790
});
807791
});
808792

793+
it('should not trigger when subscription is thrown', async () => {
794+
const pubsub = new EventEmitter();
795+
const { sendImportantEmail, subscription } = await createSubscription(
796+
pubsub,
797+
);
798+
let payload = subscription.next();
799+
800+
// A new email arrives!
801+
expect(
802+
sendImportantEmail({
803+
from: 'yuzhi@graphql.org',
804+
subject: 'Alright',
805+
message: 'Tests are good',
806+
unread: true,
807+
}),
808+
).to.equal(true);
809+
810+
expect(await payload).to.deep.equal({
811+
done: false,
812+
value: {
813+
data: {
814+
importantEmail: {
815+
email: {
816+
from: 'yuzhi@graphql.org',
817+
subject: 'Alright',
818+
},
819+
inbox: {
820+
unread: 1,
821+
total: 2,
822+
},
823+
},
824+
},
825+
},
826+
});
827+
828+
payload = subscription.next();
829+
830+
// Throw error
831+
let caughtError;
832+
try {
833+
await subscription.throw('ouch');
834+
} catch (e) {
835+
caughtError = e;
836+
}
837+
expect(caughtError).to.equal('ouch');
838+
839+
// A new email arrives!
840+
expect(
841+
sendImportantEmail({
842+
from: 'yuzhi@graphql.org',
843+
subject: 'Alright 2',
844+
message: 'Tests are good 2',
845+
unread: true,
846+
}),
847+
).to.equal(false);
848+
849+
expect(await payload).to.deep.equal({
850+
done: true,
851+
value: undefined,
852+
});
853+
});
854+
809855
it('event order is correct for multiple publishes', async () => {
810856
const pubsub = new EventEmitter();
811857
const { sendImportantEmail, subscription } = await createSubscription(

0 commit comments

Comments
 (0)