|
1 | | -import { expect } from 'chai'; |
| 1 | +import { assert, expect } from 'chai'; |
2 | 2 | import { afterEach, describe, it } from 'mocha'; |
3 | 3 |
|
4 | 4 | import { |
5 | 5 | collectEvents, |
6 | 6 | getTracingChannel, |
7 | 7 | } from '../../__testUtils__/diagnosticsTestUtils.js'; |
8 | 8 |
|
| 9 | +import { isAsyncIterable } from '../../jsutils/isAsyncIterable.js'; |
| 10 | + |
9 | 11 | import { parse } from '../../language/parser.js'; |
10 | 12 |
|
11 | 13 | import { buildSchema } from '../../utilities/buildASTSchema.js'; |
12 | 14 |
|
13 | | -import type { ExecutionArgs } from '../execute.js'; |
14 | 15 | import { |
15 | 16 | execute, |
16 | 17 | executeIgnoringIncremental, |
17 | | - executeSubscriptionEvent, |
18 | 18 | executeSync, |
19 | | - validateExecutionArgs, |
| 19 | + subscribe, |
20 | 20 | } from '../execute.js'; |
21 | 21 |
|
22 | 22 | const schema = buildSchema(` |
@@ -109,30 +109,50 @@ describe('execute diagnostics channel', () => { |
109 | 109 | ]); |
110 | 110 | }); |
111 | 111 |
|
112 | | - it('emits for each executeSubscriptionEvent call with resolved operation ctx', () => { |
113 | | - const args: ExecutionArgs = { |
114 | | - schema, |
115 | | - document: parse('query Q { sync }'), |
116 | | - rootValue, |
117 | | - }; |
118 | | - const validated = validateExecutionArgs(args); |
119 | | - if (!('schema' in validated)) { |
120 | | - throw new Error('unexpected validation failure'); |
| 112 | + it('emits for each subscription event with resolved operation ctx', async () => { |
| 113 | + const subscriptionSchema = buildSchema(` |
| 114 | + type Query { |
| 115 | + dummy: String |
| 116 | + } |
| 117 | +
|
| 118 | + type Subscription { |
| 119 | + tick: String |
| 120 | + } |
| 121 | + `); |
| 122 | + |
| 123 | + async function* tickGenerator() { |
| 124 | + await Promise.resolve(); |
| 125 | + yield { tick: 'one' }; |
| 126 | + yield { tick: 'two' }; |
121 | 127 | } |
122 | 128 |
|
| 129 | + const document = parse('subscription S { tick }'); |
| 130 | + |
123 | 131 | active = collectEvents(executeChannel); |
124 | 132 |
|
125 | | - // eslint-disable-next-line @typescript-eslint/no-floating-promises |
126 | | - executeSubscriptionEvent(validated); |
127 | | - // eslint-disable-next-line @typescript-eslint/no-floating-promises |
128 | | - executeSubscriptionEvent(validated); |
| 133 | + const subscription = await subscribe({ |
| 134 | + schema: subscriptionSchema, |
| 135 | + document, |
| 136 | + rootValue: { tick: tickGenerator }, |
| 137 | + }); |
| 138 | + assert(isAsyncIterable(subscription)); |
| 139 | + |
| 140 | + expect(await subscription.next()).to.deep.equal({ |
| 141 | + done: false, |
| 142 | + value: { data: { tick: 'one' } }, |
| 143 | + }); |
| 144 | + expect(await subscription.next()).to.deep.equal({ |
| 145 | + done: false, |
| 146 | + value: { data: { tick: 'two' } }, |
| 147 | + }); |
129 | 148 |
|
130 | 149 | const starts = active.events.filter((e) => e.kind === 'start'); |
131 | 150 | expect(starts.length).to.equal(2); |
132 | 151 | for (const ev of starts) { |
133 | | - expect(ev.ctx.operationType).to.equal('query'); |
134 | | - expect(ev.ctx.operationName).to.equal('Q'); |
135 | | - expect(ev.ctx.schema).to.equal(schema); |
| 152 | + expect(ev.ctx.operationType).to.equal('subscription'); |
| 153 | + expect(ev.ctx.operationName).to.equal('S'); |
| 154 | + expect(ev.ctx.operation).to.equal(document.definitions[0]); |
| 155 | + expect(ev.ctx.schema).to.equal(subscriptionSchema); |
136 | 156 | } |
137 | 157 | }); |
138 | 158 |
|
|
0 commit comments