Skip to content

Commit

Permalink
test: add subscription test
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide-Gheri committed Jun 15, 2021
1 parent d9d377b commit daf7afb
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"supertest": "^6.1.3",
"ts-node": "^9.1.1",
"typescript": "^4.2.4",
"uvu": "^0.5.1"
"uvu": "^0.5.1",
"ws": "^7.4.6"
},
"dependencies": {
"@nestjs/graphql": "~7.10.6"
Expand Down
1 change: 1 addition & 0 deletions tests/code-first/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { UpperCaseDirective } from '../example/directives/upper-case.directive';
schemaDirectives: {
uppercase: UpperCaseDirective,
},
subscription: true,
}),
],
providers: [CatService, DogService, AnimalResolver, CatResolver],
Expand Down
20 changes: 18 additions & 2 deletions tests/code-first/resolvers/cat.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { Args, ID, Parent, Query, Resolver } from '@nestjs/graphql';
import {
Args,
Context,
ID,
Parent,
Query,
Resolver,
Subscription,
} from '@nestjs/graphql';
import { Cat } from '../types/cat';
import { CatService } from '../services/cat.service';
import { ParseIntPipe } from '@nestjs/common';
import { LoaderQuery, ResolveLoader } from '../../../lib';
import { LoaderQuery, ResolveLoader, toAsyncIterator } from '../../../lib';
import { PubSub } from 'mercurius';

@Resolver(() => Cat)
export class CatResolver {
Expand All @@ -22,4 +31,11 @@ export class CatResolver {
hasFur(@Parent() queries: LoaderQuery<Cat>[]) {
return queries.map(({ obj }) => obj.lives > 1);
}

@Subscription(() => Cat, {
resolve: (payload) => payload,
})
onCatSub(@Context('pubsub') pubSub: PubSub) {
return toAsyncIterator(pubSub.subscribe('CAT_SUB_TOPIC'));
}
}
71 changes: 71 additions & 0 deletions tests/e2e/code-first.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { suite } from 'uvu';
import * as assert from 'uvu/assert';
import { Test } from '@nestjs/testing';
import { AppModule } from '../code-first/app.module';
import * as Websocket from 'ws';
import { FastifyInstance } from 'fastify';
import { cats } from '../code-first/services/cat.service';

interface Context {
app: NestFastifyApplication;
Expand Down Expand Up @@ -262,4 +265,72 @@ gqlSuite(
},
);

gqlSuite('subscriber should work', async ({ app }) => {
return new Promise<void>((resolve, reject) => {
app.listen(0, (err) => {
if (err) {
return reject(err);
}
const port = app.getHttpServer().address().port;
const fastifyApp = app.getHttpAdapter().getInstance() as FastifyInstance;

const ws = new Websocket(`ws://localhost:${port}/graphql`, 'graphql-ws');

const client = Websocket.createWebSocketStream(ws, {
encoding: 'utf8',
objectMode: true,
});
client.setEncoding('utf8');
client.write(
JSON.stringify({
type: 'connection_init',
}),
);

client.write(
JSON.stringify({
id: 1,
type: 'start',
payload: {
query: `
subscription {
onCatSub {
id
lives
name
hasFur
}
}
`,
},
}),
);

client.on('data', (chunk) => {
const data = JSON.parse(chunk);

if (data.type === 'connection_ack') {
fastifyApp.graphql.pubsub.publish({
topic: 'CAT_SUB_TOPIC',
payload: cats[0],
});
} else if (data.id === 1) {
const expectedCat = expectedCats[0];
const receivedCat = data.payload.data?.onCatSub;
assert.ok(receivedCat);
assert.equal(expectedCat.id, receivedCat.id);
assert.type(receivedCat.hasFur, 'boolean');

client.end();
}
});

client.on('end', () => {
client.destroy();
app.close().then(resolve).catch(reject);
});
});
});
});

gqlSuite.run();
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6487,6 +6487,11 @@ ws@^7.4.2, ws@^7.4.3:
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.5.tgz#a484dd851e9beb6fdb420027e3885e8ce48986c1"
integrity sha512-xzyu3hFvomRfXKH8vOFMU3OguG6oOvhXMo3xsGy3xWExqaM2dxBbVxuD99O7m3ZUFMvvscsZDqxfgMaRr/Nr1g==

ws@^7.4.6:
version "7.4.6"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.6.tgz#5654ca8ecdeee47c33a9a4bf6d28e2be2980377c"
integrity sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==

xdg-basedir@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-4.0.0.tgz#4bc8d9984403696225ef83a1573cbbcb4e79db13"
Expand Down

0 comments on commit daf7afb

Please sign in to comment.