@@ -5,9 +5,10 @@ import { AppError } from '../../common/errors';
55import * as fileUtils from '../../common/fileUtils' ;
66import * as mongoPoolModule from '../../common/MongoPool' ;
77import type { TargetUnit } from '../../common/types' ;
8- import type { Employee , Project } from '../../services/FinApp' ;
9- import type { IFinAppRepository } from '../../services/FinApp' ;
8+ import type { Employee , IFinAppRepository , Project } from '../../services/FinApp' ;
109import * as finAppService from '../../services/FinApp' ;
10+ import type { CustomerRevenueByRef } from '../../services/QBO' ;
11+ import * as qboService from '../../services/QBO' ;
1112import { fetchFinancialAppData } from './fetchFinancialAppData' ;
1213
1314type MongoPoolMock = {
@@ -30,6 +31,9 @@ vi.mock('../../common/MongoPool', () => ({
3031vi . mock ( '../../services/FinApp' , ( ) => ( {
3132 FinAppRepository : vi . fn ( ) ,
3233} ) ) ;
34+ vi . mock ( '../../services/QBO' , ( ) => ( {
35+ QBORepository : vi . fn ( ) ,
36+ } ) ) ;
3337
3438const mockTargetUnits : TargetUnit [ ] = [
3539 {
@@ -49,11 +53,19 @@ const mockEmployees: Employee[] = [
4953const mockProjects : Project [ ] = [
5054 {
5155 redmine_id : 2 ,
52- quick_books_id : 10 ,
56+ quick_books_id : '10' ,
5357 history : { rate : { '2024-01-01' : 200 } } ,
5458 } ,
5559] ;
5660
61+ const mockEffectiveRevenue : CustomerRevenueByRef = {
62+ '10' : {
63+ customerName : 'Test Customer' ,
64+ totalAmount : 5000 ,
65+ invoiceCount : 3 ,
66+ } ,
67+ } ;
68+
5769function createRepoInstance (
5870 overrides : Partial < IFinAppRepository > = { } ,
5971) : IFinAppRepository {
@@ -83,8 +95,10 @@ describe('getFinAppData', () => {
8395 let connect : Mock ;
8496 let disconnect : Mock ;
8597 let FinAppRepository : Mock ;
98+ let QBORepository : Mock ;
8699 let dateSpy : ReturnType < typeof vi . spyOn > ;
87100 let repoInstance : IFinAppRepository ;
101+ let qboRepoInstance : { getEffectiveRevenue : Mock } ;
88102 let mongoPoolInstance : MongoPoolMock ;
89103
90104 const fileLink = 'input.json' ;
@@ -100,6 +114,7 @@ describe('getFinAppData', () => {
100114 ( repoInstance . getProjectsByRedmineIds as Mock ) . mockResolvedValue (
101115 mockProjects ,
102116 ) ;
117+ qboRepoInstance . getEffectiveRevenue . mockResolvedValue ( mockEffectiveRevenue ) ;
103118 }
104119
105120 async function expectAppError ( promise : Promise < unknown > , msg : string ) {
@@ -113,10 +128,16 @@ describe('getFinAppData', () => {
113128 readJsonFile = vi . mocked ( fileUtils . readJsonFile ) ;
114129 writeJsonFile = vi . mocked ( fileUtils . writeJsonFile ) ;
115130 FinAppRepository = vi . mocked ( finAppService . FinAppRepository ) ;
131+ QBORepository = vi . mocked ( qboService . QBORepository ) ;
116132
117133 repoInstance = createRepoInstance ( ) ;
118134 FinAppRepository . mockImplementation ( ( ) => repoInstance ) ;
119135
136+ qboRepoInstance = {
137+ getEffectiveRevenue : vi . fn ( ) . mockResolvedValue ( mockEffectiveRevenue ) ,
138+ } ;
139+ QBORepository . mockImplementation ( ( ) => qboRepoInstance ) ;
140+
120141 connect = vi . fn ( ) . mockResolvedValue ( undefined ) ;
121142 disconnect = vi . fn ( ) . mockResolvedValue ( undefined ) ;
122143 mongoPoolInstance = createMongoPoolInstance ( connect , disconnect ) ;
@@ -141,13 +162,19 @@ describe('getFinAppData', () => {
141162 expect ( readJsonFile ) . toHaveBeenCalledWith ( fileLink ) ;
142163 expect ( writeJsonFile ) . toHaveBeenCalledWith ( expectedFilename , {
143164 employees : mockEmployees ,
144- projects : mockProjects ,
165+ projects : [
166+ {
167+ ...mockProjects [ 0 ] ,
168+ effectiveRevenue : 5000 ,
169+ } ,
170+ ] ,
171+ effectiveRevenue : mockEffectiveRevenue ,
145172 } ) ;
146173 } ) ;
147174
148175 it ( 'always disconnects the mongo pool' , async ( ) => {
149176 setupSuccessMocks ( ) ;
150- await fetchFinancialAppData ( fileLink ) . catch ( ( ) => { } ) ;
177+ await fetchFinancialAppData ( fileLink ) . catch ( ( ) => { } ) ;
151178 expect ( ( ) => mongoPoolInstance . disconnect ( ) ) . not . toThrow ( ) ;
152179 } ) ;
153180 } ) ;
0 commit comments