1
+ import TypeormMock from '@lomray/microservice-helpers/mocks/typeorm' ;
1
2
import { Microservice } from '@lomray/microservice-nodejs-lib' ;
2
3
import { RemoteMiddlewareServer } from '@lomray/microservice-remote-middleware' ;
3
4
import { expect } from 'chai' ;
5
+ import rewiremock from 'rewiremock' ;
4
6
import sinon from 'sinon' ;
5
7
import type { Connection } from 'typeorm' ;
8
+ import ConfigRepositoryMock from '@__mocks__/config-repository' ;
6
9
import * as DBConfig from '@config/db' ;
7
10
import { microserviceOptions , microserviceParams } from '@config/ms' ;
8
11
import { MS_NAME , MS_CONNECTION } from '@constants/index' ;
9
- import { start } from '../src' ;
12
+ import { start as OriginalStart } from '../src' ;
13
+
14
+ const { start } = rewiremock . proxy < { start : typeof OriginalStart } > ( ( ) => require ( '../src' ) , {
15
+ typeorm : TypeormMock . mock ,
16
+ } ) ;
10
17
11
18
describe ( 'start' , ( ) => {
12
- const middlewareRepo = { middleware : 'repo' } ;
13
19
const dbConnectionMock = {
14
- getRepository : sinon . stub ( ) . returns ( middlewareRepo ) ,
20
+ getRepository : sinon . stub ( ) . returns ( { } ) ,
15
21
} as unknown as Promise < Connection > ;
22
+ const configRepository = new ConfigRepositoryMock ( ) ;
16
23
17
24
before ( ( ) => {
18
25
sinon . stub ( console , 'info' ) ;
26
+ TypeormMock . sandbox . reset ( ) ;
27
+
28
+ TypeormMock . stubs . getCustomRepository . returns ( configRepository ) ;
19
29
} ) ;
20
30
21
31
beforeEach ( ( ) => {
@@ -29,7 +39,8 @@ describe('start', () => {
29
39
it ( 'should correct start microservice' , async ( ) => {
30
40
const spyCreate = sinon . spy ( Microservice , 'create' ) ;
31
41
let stubbedStart ;
32
- let remoteMiddlewareInstance ;
42
+ let addRegisterEndpointSpy ;
43
+ let addObtainEndpointSpy ;
33
44
let dbConnection ;
34
45
let isRunBeforeStart = false ;
35
46
@@ -43,7 +54,8 @@ describe('start', () => {
43
54
dbConnection = connection ;
44
55
} ,
45
56
afterInitRemoteMiddleware : ( remoteMiddleware ) => {
46
- remoteMiddlewareInstance = remoteMiddleware ;
57
+ addRegisterEndpointSpy = sinon . spy ( remoteMiddleware , 'addRegisterEndpoint' ) ;
58
+ addObtainEndpointSpy = sinon . spy ( remoteMiddleware , 'addObtainEndpoint' ) ;
47
59
} ,
48
60
beforeStart : ( ) => {
49
61
isRunBeforeStart = true ;
@@ -57,7 +69,9 @@ describe('start', () => {
57
69
expect ( stubbedStart ) . to . calledOnce ;
58
70
expect ( isRunBeforeStart ) . to . ok ;
59
71
expect ( dbConnection ) . to . deep . equal ( dbConnectionMock ) ;
60
- expect ( remoteMiddlewareInstance ) . property ( 'repository' ) . to . deep . equal ( middlewareRepo ) ;
72
+ expect ( addRegisterEndpointSpy ) . to . calledOnce ;
73
+ expect ( addObtainEndpointSpy ) . to . calledOnce ;
74
+ expect ( configRepository . bulkSave ) . to . calledOnce ;
61
75
} ) ;
62
76
63
77
it ( 'should correct start microservice without remote middleware' , async ( ) => {
0 commit comments