@@ -4,6 +4,7 @@ const Hapi = require('@hapi/hapi');
44const Hoek = require ( '@hapi/hoek' ) ;
55const Lab = require ( '@hapi/lab' ) ;
66const Mongodb = require ( 'mongodb' ) ;
7+ const Sinon = require ( 'sinon' ) ;
78
89const { describe, it, beforeEach, expect } = exports . lab = Lab . script ( ) ;
910
@@ -314,17 +315,48 @@ describe('Hapi server', () => {
314315 it ( 'should disconnect if the server stops' , async ( ) => {
315316
316317 await server . register ( {
317- plugin : require ( '../' ) ,
318- options : {
319- settings : {
320- promiseLibrary : 'bluebird'
321- }
322- }
318+ plugin : require ( '../' )
323319 } ) ;
324320
325321 await server . initialize ( ) ;
322+
323+ expect ( server . plugins [ 'hapi-mongodb' ] . client . isConnected ( ) ) . to . be . true ( ) ;
324+
326325 await server . stop ( ) ;
327326 await Hoek . wait ( 100 ) ; // Let the connections end.
327+
328+ expect ( server . plugins [ 'hapi-mongodb' ] . client . isConnected ( ) ) . to . be . false ( ) ;
329+ } ) ;
330+
331+ it ( 'should logs errors on disconnect' , async ( ) => {
332+
333+ const logEntries = [ ] ;
334+ server . events . on ( 'log' , ( entry ) => {
335+
336+ logEntries . push ( entry ) ;
337+ } ) ;
338+
339+ await server . register ( {
340+ plugin : require ( '../' )
341+ } ) ;
342+
343+ await server . initialize ( ) ;
344+
345+ expect ( server . plugins [ 'hapi-mongodb' ] . client . isConnected ( ) ) . to . be . true ( ) ;
346+ const closeStub = Sinon . stub ( server . plugins [ 'hapi-mongodb' ] . client , 'close' ) . callsFake ( ( cb ) => {
347+
348+ setTimeout ( cb , 0 , new Error ( 'Oops' ) ) ;
349+ } ) ;
350+
351+ await server . stop ( ) ;
352+ await Hoek . wait ( 100 ) ; // Let the connections end.
353+
354+ closeStub . restore ( ) ;
355+ await server . plugins [ 'hapi-mongodb' ] . client . close ( ) ;
356+
357+ expect ( logEntries ) . to . have . length ( 2 ) ;
358+ expect ( logEntries [ 1 ] . tags ) . to . equal ( [ 'hapi-mongodb' , 'error' ] ) ;
359+ expect ( logEntries [ 1 ] . error ) . to . be . an . error ( 'Oops' ) ;
328360 } ) ;
329361
330362 it ( 'should be able to find the plugin exposed objects' , async ( ) => {
0 commit comments