nestjs-google-pubsub-microservice
Version:
NestJS Google Cloud Pub/Sub Microservice Transport
254 lines • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const sinon = require("sinon");
const gc_pubsub_server_1 = require("./gc-pubsub.server");
const constants_1 = require("@nestjs/microservices/constants");
const base_rpc_context_1 = require("@nestjs/microservices/ctx-host/base-rpc.context");
const gc_pubsub_constants_1 = require("./gc-pubsub.constants");
describe('GCPubSubServer', () => {
let server;
let pubsub;
let topicMock;
let subscriptionMock;
let createClient;
let sandbox;
const objectToMap = (obj) => new Map(Object.keys(obj).map((key) => [key, obj[key]]));
afterEach(() => {
sandbox.restore();
});
describe('constructor', () => {
describe('when the scopedEnvKey is defined', () => {
it('should set the scopedEnvKey on topics and subscriptions', () => {
const scopedEnvKey = 'my-key';
server = getInstance({ scopedEnvKey });
(0, chai_1.expect)(server['topicName']).to.eq(`${scopedEnvKey}default_topic`);
(0, chai_1.expect)(server['subscriptionName']).to.eq(`${scopedEnvKey}default_subscription`);
});
});
});
describe('listen', () => {
describe('when is check existence is true', () => {
beforeEach(async () => {
server = getInstance({});
await server.listen(() => { });
});
it('should call "createClient"', () => {
(0, chai_1.expect)(createClient.called).to.be.true;
});
it('should call "client.topic" once', async () => {
(0, chai_1.expect)(pubsub.topic.called).to.be.true;
});
it('should call "topic.create" once', async () => {
(0, chai_1.expect)(topicMock.create.called).to.be.true;
});
it('should call "topic.subscription" once', async () => {
(0, chai_1.expect)(topicMock.subscription.called).to.be.true;
});
it('should call "subscription.create" once', async () => {
(0, chai_1.expect)(subscriptionMock.create.called).to.be.true;
});
it('should call "subscription.on" twice', async () => {
(0, chai_1.expect)(subscriptionMock.on.callCount).to.eq(2);
});
});
describe('when is check existence is false', () => {
beforeEach(async () => {
server = getInstance({
init: false,
checkExistence: false,
});
await server.listen(() => { });
});
it('should call "createClient"', () => {
(0, chai_1.expect)(createClient.called).to.be.true;
});
it('should call "client.topic" once', async () => {
(0, chai_1.expect)(pubsub.topic.called).to.be.true;
});
it('should not call "topic.exists" once', async () => {
(0, chai_1.expect)(topicMock.exists.called).to.be.false;
});
it('should call "topic.subscription" once', async () => {
(0, chai_1.expect)(topicMock.subscription.called).to.be.true;
});
it('should not call "subscription.exists" once', async () => {
(0, chai_1.expect)(subscriptionMock.exists.called).to.be.false;
});
it('should call "subscription.on" twice', async () => {
(0, chai_1.expect)(subscriptionMock.on.callCount).to.eq(2);
});
});
});
describe('close', () => {
beforeEach(async () => {
server = getInstance({});
await server.listen(() => { });
await server.close();
});
it('should call "subscription.close"', function () {
(0, chai_1.expect)(subscriptionMock.close.called).to.be.true;
});
it('should close() pubsub', () => {
(0, chai_1.expect)(pubsub.close.called).to.be.true;
});
});
describe('handleMessage', () => {
const msg = {
pattern: 'test',
data: 'tests',
id: '3',
};
beforeEach(async () => {
server = getInstance({});
await server.listen(() => { });
});
it('should send NO_MESSAGE_HANDLER error if key does not exists in handlers object', async () => {
await server.handleMessage({
ackId: 'id',
publishTime: new Date(),
attributes: {
replyTo: 'replyTo',
},
id: 'id',
received: 0,
deliveryAttempt: 1,
ack: () => { },
modAck: () => { },
nack: () => { },
data: Buffer.from(JSON.stringify(msg)),
});
(0, chai_1.expect)(topicMock.publishMessage.calledWith({
json: {
id: msg.id,
status: 'error',
err: constants_1.NO_MESSAGE_HANDLER,
},
attributes: {
id: msg.id,
},
})).to.be.true;
});
it('should call handler if exists in handlers object', async () => {
const handler = sinon.spy();
server.messageHandlers = objectToMap({
[msg.pattern]: handler,
});
await server.handleMessage({
ackId: 'id',
publishTime: new Date(),
attributes: {},
id: 'id',
received: 0,
deliveryAttempt: 1,
ack: () => { },
modAck: () => { },
nack: () => { },
data: Buffer.from(JSON.stringify(msg)),
});
(0, chai_1.expect)(handler.calledOnce).to.be.true;
});
it('should return undefined when data is not in JSON format', async () => {
const result = await server.handleMessage({
ackId: 'id',
publishTime: new Date(),
attributes: {},
id: 'id',
received: 0,
deliveryAttempt: 1,
ack: () => { },
modAck: () => { },
nack: () => { },
data: Buffer.from('text'),
});
(0, chai_1.expect)(result).to.be.undefined;
});
});
describe('sendMessage', () => {
beforeEach(async () => {
server = getInstance({});
await server.listen(() => { });
});
it('should publish message to indicated topic', async () => {
const message = { test: true };
const replyTo = 'test';
const correlationId = '0';
await server.sendMessage(message, replyTo, correlationId);
(0, chai_1.expect)(topicMock.publishMessage.calledWith({
json: { ...message, id: correlationId },
attributes: {
id: correlationId,
},
})).to.be.true;
});
describe('when scopedEnvKey is defined', () => {
beforeEach(async () => {
server = getInstance({ scopedEnvKey: 'my-key' });
await server.listen(() => { });
});
it('should set scopedEnvKey on replyTo', async () => {
const message = { test: true };
const replyTo = 'test';
const correlationId = '0';
await server.sendMessage(message, replyTo, correlationId);
(0, chai_1.expect)(Array.from(server['replyTopics'].values())).to.deep.eq(['test']);
});
});
});
describe('handleEvent', () => {
const channel = 'test';
const data = 'test';
beforeEach(async () => {
server = getInstance({});
});
it('should call handler with expected arguments', () => {
const handler = sandbox.spy();
server.messageHandlers = objectToMap({
[channel]: handler,
});
server.handleEvent(channel, { pattern: '', data }, new base_rpc_context_1.BaseRpcContext([]));
(0, chai_1.expect)(handler.calledWith(data)).to.be.true;
});
});
describe('createIfNotExists', () => {
it('should throw error', async () => {
const create = sandbox.stub().rejects({ code: 7 });
try {
await server['createIfNotExists'](create);
}
catch (error) {
(0, chai_1.expect)(error).to.include({ code: 7 });
}
(0, chai_1.expect)(create.called).to.be.true;
});
it('should skip error', async () => {
const create = sandbox.stub().rejects({ code: gc_pubsub_constants_1.ALREADY_EXISTS });
await server['createIfNotExists'](create);
(0, chai_1.expect)(create.called).to.be.true;
});
});
function getInstance(options) {
const server = new gc_pubsub_server_1.GCPubSubServer(options);
sandbox = sinon.createSandbox();
subscriptionMock = {
create: sandbox.stub().resolves(),
close: sandbox.stub().callsFake((callback) => callback()),
on: sandbox.stub().returnsThis(),
exists: sandbox.stub().resolves([true]),
};
topicMock = {
create: sandbox.stub().resolves(),
exists: sandbox.stub().resolves([true]),
flush: sandbox.stub().callsFake((callback) => callback()),
publishMessage: sandbox.stub().resolves(),
subscription: sandbox.stub().returns(subscriptionMock),
};
pubsub = {
topic: sandbox.stub().returns(topicMock),
close: sandbox.stub().callsFake((callback) => callback()),
};
createClient = sandbox.stub(server, 'createClient').returns(pubsub);
return server;
}
});
//# sourceMappingURL=gc-pubsub.server.spec.js.map