11import { expect } from 'chai' ;
22import * as sinon from 'sinon' ;
3- import { NO_MESSAGE_HANDLER } from '../../constants' ;
4- import { ServerTCP } from '../../server/server-tcp' ;
53import { ServerKafka } from '../../server' ;
6- import { Consumer , IHeaders , KafkaMessage } from '../../external/kafka.interface' ;
4+ import { Logger } from '@nestjs/common' ;
5+
6+ class NoopLogger extends Logger {
7+ log ( message : any , context ?: string ) : void { }
8+ error ( message : any , trace ?: string , context ?: string ) : void { }
9+ warn ( message : any , context ?: string ) : void { }
10+ }
711
812describe ( 'ServerKafka' , ( ) => {
913 let server : ServerKafka ;
@@ -12,9 +16,6 @@ describe('ServerKafka', () => {
1216 server = new ServerKafka ( { } ) ;
1317 } ) ;
1418
15- afterEach ( ( ) => {
16- server . close ( ) ;
17- } ) ;
1819 describe ( 'close' , ( ) => {
1920 it ( 'should close server' , ( ) => {
2021 server . close ( ) ;
@@ -23,17 +24,67 @@ describe('ServerKafka', () => {
2324 expect ( server . client ) . to . be . null ;
2425 } ) ;
2526 } ) ;
27+
28+ let callback : sinon . SinonSpy ;
29+ let bindEventsStub : sinon . SinonStub ;
30+ let connect : sinon . SinonSpy ;
31+ let subscribe : sinon . SinonSpy ;
32+ let run : sinon . SinonSpy ;
33+ let consumerStub : sinon . SinonStub ;
34+ let producerStub : sinon . SinonStub ;
35+ let client ;
36+ beforeEach ( ( ) => {
37+ callback = sinon . spy ( ) ;
38+ connect = sinon . spy ( ) ;
39+ subscribe = sinon . spy ( ) ;
40+ run = sinon . spy ( ) ;
41+ bindEventsStub = sinon
42+ . stub ( server , 'bindEvents' )
43+ . callsFake ( ( ) => ( { } as any ) ) ;
44+ consumerStub = sinon . stub ( server , 'consumer' )
45+ . callsFake ( ( ) => {
46+ return {
47+ connect,
48+ subscribe,
49+ run,
50+ } ;
51+ } ) ;
52+ producerStub = sinon . stub ( server , 'producer' )
53+ . callsFake ( ( ) => {
54+ return {
55+ connect,
56+ } ;
57+ } ) ;
58+ client = {
59+ consumer : consumerStub ,
60+ producer : producerStub ,
61+ } ;
62+ sinon . stub ( server , 'createClient' ) . callsFake ( ( ) => client ) ;
63+ } ) ;
64+
2665 describe ( 'listen' , ( ) => {
27- it ( 'should start server' , async ( ) => {
28- const callback = sinon . spy ( ) ;
66+ it ( 'should call "bindEvents"' , async ( ) => {
67+ await server . listen ( callback ) ;
68+ expect ( bindEventsStub . called ) . to . be . true ;
69+ } ) ;
70+ it ( 'should call "client.start"' , async ( ) => {
71+
72+ await server . listen ( callback ) ;
73+ expect ( client . producer . called ) . to . be . true ;
74+ } ) ;
75+ it ( 'should call callback' , async ( ) => {
2976 await server . listen ( callback ) ;
3077 expect ( callback . called ) . to . be . true ;
3178 } ) ;
32- it ( 'should have kafka, producer and consumer connected' , async ( ) => {
33- await server . listen ( ( ) => { } ) ;
34- expect ( server . client ) . to . be . not . null ;
35- expect ( server . producer ) . to . be . not . null ;
36- expect ( server . consumer ) . to . be . not . null ;
79+ } ) ;
80+
81+ describe ( 'bindEvents' , ( ) => {
82+ it ( 'should not call subscribe nor run on consumer when there are no messageHandlers' , async ( ) => {
83+ ( server as any ) . logger = new NoopLogger ( ) ;
84+ await server . bindEvents ( server . consumer ) ;
85+ expect ( subscribe . called ) . to . be . not . true ;
86+ expect ( run . called ) . to . be . not . true ;
87+ expect ( connect . called ) . to . be . not . true ;
3788 } ) ;
3889 } ) ;
3990} ) ;
0 commit comments