Skip to content

Commit fd89719

Browse files
committed
feat: add more tests to improve coverage
1 parent 93fe2cb commit fd89719

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

test/RedisQueue.publish.spec.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*!
2+
* Additional RedisQueue tests: publish() branches
3+
*/
4+
import './mocks';
5+
import { expect } from 'chai';
6+
import * as sinon from 'sinon';
7+
import { RedisQueue, IMQMode } from '../src';
8+
9+
function makeLogger() {
10+
return {
11+
log: (..._args: any[]) => undefined,
12+
info: (..._args: any[]) => undefined,
13+
warn: (..._args: any[]) => undefined,
14+
error: (..._args: any[]) => undefined,
15+
} as any;
16+
}
17+
18+
describe('RedisQueue.publish()', function() {
19+
this.timeout(10000);
20+
21+
it('should throw when writer is not connected', async () => {
22+
const logger = makeLogger();
23+
const rq: any = new RedisQueue('PubNoWriter', { logger }, IMQMode.PUBLISHER);
24+
25+
let thrown: any;
26+
try {
27+
await rq.publish({ a: 1 });
28+
} catch (err) {
29+
thrown = err;
30+
}
31+
32+
expect(thrown).to.be.instanceof(TypeError);
33+
expect(`${thrown}`).to.include('Writer is not connected');
34+
35+
await rq.destroy().catch(() => undefined);
36+
});
37+
38+
it('should publish to default channel when writer is connected', async () => {
39+
const logger = makeLogger();
40+
const rq: any = new RedisQueue('PubDefault', { logger }, IMQMode.PUBLISHER);
41+
await rq.start();
42+
43+
const pubSpy = sinon.spy((rq as any).writer, 'publish');
44+
await rq.publish({ hello: 'world' });
45+
46+
expect(pubSpy.called).to.equal(true);
47+
const [channel, msg] = pubSpy.getCall(0).args;
48+
expect(channel).to.equal('imq:PubDefault');
49+
expect(() => JSON.parse(msg)).not.to.throw();
50+
51+
pubSpy.restore();
52+
await rq.destroy().catch(() => undefined);
53+
});
54+
55+
it('should publish to provided toName channel when given', async () => {
56+
const logger = makeLogger();
57+
const rq: any = new RedisQueue('PubOther', { logger }, IMQMode.PUBLISHER);
58+
await rq.start();
59+
60+
const pubSpy = sinon.spy((rq as any).writer, 'publish');
61+
await rq.publish({ t: true }, 'OtherChannel');
62+
63+
expect(pubSpy.called).to.equal(true);
64+
const [channel] = pubSpy.getCall(0).args;
65+
expect(channel).to.equal('imq:OtherChannel');
66+
67+
pubSpy.restore();
68+
await rq.destroy().catch(() => undefined);
69+
});
70+
});

0 commit comments

Comments
 (0)