@@ -236,6 +236,82 @@ describe('LedgerCollection unit tests', () => {
236236 } ) ;
237237 } ) ;
238238 } ) ;
239+ describe ( 'and getCashFlowYearAverage' , ( ) => {
240+ describe ( 'and no date' , ( ) => {
241+ test ( 'should return 0' , ( ) => {
242+ expect ( instance . getCashFlowYearAverage ( null ) ) . toEqual ( 0 ) ;
243+ } ) ;
244+ } ) ;
245+ describe ( 'and no ledgerItem' , ( ) => {
246+ test ( 'should return empty data' , ( ) => {
247+ expect ( instance . getCashFlowYearAverage ( dateUtc ) ) . toEqual ( 0 ) ;
248+ } ) ;
249+ } ) ;
250+ describe ( 'and ledgerItems' , ( ) => {
251+ describe ( 'with matches' , ( ) => {
252+ let createdDate : Date ;
253+ let cashFlow : LedgerItem ;
254+ let cashFlowTwo : LedgerItem ;
255+
256+ beforeEach ( ( ) => {
257+ createdDate = new Date ( Date . UTC ( dateUtc . getUTCFullYear ( ) + 1 , dateUtc . getUTCMonth ( ) , 1 ) ) ;
258+
259+ cashFlow = new LedgerItem ( ) ;
260+ cashFlow . created = createdDate ;
261+ cashFlow . amount = 1 ;
262+ cashFlow . type = LedgerItemType . CashFlow ;
263+
264+ cashFlowTwo = new LedgerItem ( ) ;
265+ cashFlowTwo . created = createdDate ;
266+ cashFlowTwo . amount = 3 ;
267+ cashFlowTwo . type = LedgerItemType . CashFlow ;
268+
269+ const cashFlowOut = new LedgerItem ( ) ;
270+ cashFlowOut . created = new Date ( Date . UTC ( dateUtc . getUTCFullYear ( ) , dateUtc . getUTCMonth ( ) , 1 ) ) ;
271+ cashFlowOut . created . setUTCMonth ( cashFlowOut . created . getUTCMonth ( ) + 2 ) ;
272+ cashFlowOut . amount = 111111 ;
273+ cashFlowOut . type = LedgerItemType . CashFlow ;
274+
275+ const equity = new LedgerItem ( ) ;
276+ equity . created = createdDate ;
277+ equity . amount = 2 ;
278+ equity . type = LedgerItemType . Equity ;
279+
280+ const purchase = new LedgerItem ( ) ;
281+ purchase . created = createdDate ;
282+ purchase . amount = - 3 ;
283+ purchase . type = LedgerItemType . Purchase ;
284+
285+ const salary = new LedgerItem ( ) ;
286+ salary . created = createdDate ;
287+ salary . amount = 4 ;
288+ salary . type = LedgerItemType . Salary ;
289+
290+ instance . add ( [ cashFlow , cashFlowTwo , cashFlowOut , equity , purchase , salary ] ) ;
291+ } ) ;
292+
293+ describe ( 'with date' , ( ) => {
294+ test ( 'should return average result' , ( ) => {
295+ expect ( instance . getCashFlowYearAverage ( ) ) . toEqual ( ( cashFlow . amount + cashFlowTwo . amount ) / 2 ) ;
296+ } ) ;
297+ } ) ;
298+ } ) ;
299+
300+ test ( 'and no matching dates' , ( ) => {
301+ const createdDate = new Date ( Date . UTC ( dateUtc . getUTCFullYear ( ) + 1 , dateUtc . getUTCMonth ( ) , 1 ) ) ;
302+
303+ const cashFlowOut = new LedgerItem ( ) ;
304+ cashFlowOut . created = new Date ( Date . UTC ( dateUtc . getUTCFullYear ( ) , dateUtc . getUTCMonth ( ) , 1 ) ) ;
305+ cashFlowOut . created . setUTCMonth ( cashFlowOut . created . getUTCMonth ( ) + 2 ) ;
306+ cashFlowOut . amount = 111111 ;
307+ cashFlowOut . type = LedgerItemType . CashFlow ;
308+
309+ instance . add ( [ cashFlowOut ] ) ;
310+
311+ expect ( instance . getCashFlowYearAverage ( createdDate ) ) . toEqual ( 0 ) ;
312+ } ) ;
313+ } ) ;
314+ } ) ;
239315 describe ( 'and getSummariesAnnual' , ( ) => {
240316 describe ( 'and no date' , ( ) => {
241317 test ( 'should throw error' , ( ) => {
0 commit comments