-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add missing tests for budget-cards.component
Add tests for budget-cards effects, reducers, and selectors.
- Loading branch information
1 parent
954a48d
commit 574f3ff
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/app/reports/feature/budget-cards/budget-cards.effects.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { Actions } from '@ngrx/effects'; | ||
import { provideMockActions } from '@ngrx/effects/testing'; | ||
import { Action } from '@ngrx/store'; | ||
import { provideMockStore } from '@ngrx/store/testing'; | ||
|
||
import { Observable, of } from 'rxjs'; | ||
|
||
import { BudgetSummary } from '../../../shared/services/ynab/interfaces/budgets/summary/budgetSummary'; | ||
import { YnabService } from '../../../shared/services/ynab/ynab.service'; | ||
import { budgetActions } from './budget-cards.actions'; | ||
import { BudgetEffects } from './budget-cards.effects'; | ||
|
||
describe('BudgetEffects', () => { | ||
let actions$: Observable<Action>; | ||
let effects: BudgetEffects; | ||
let ynabSpy: jasmine.SpyObj<YnabService>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [ | ||
BudgetEffects, | ||
provideMockActions(() => actions$), | ||
provideMockStore(), | ||
{ | ||
provide: YnabService, | ||
useValue: jasmine.createSpyObj('YnabService', ['getBudgets']), | ||
}, | ||
], | ||
}); | ||
|
||
actions$ = TestBed.inject(Actions); | ||
effects = TestBed.inject(BudgetEffects); | ||
ynabSpy = TestBed.inject(YnabService) as jasmine.SpyObj<YnabService>; | ||
}); | ||
|
||
describe('loadBudgets$', () => { | ||
it('should dispatch setBudgets when initBudgets is dispatched', () => { | ||
const mockBudgets: BudgetSummary[] = [ | ||
{ | ||
id: '1', | ||
name: 'Test Budget 1', | ||
last_modified_on: '2022-01-01', | ||
}, | ||
{ | ||
id: '2', | ||
name: 'Test Budget 2', | ||
last_modified_on: '2022-02-01', | ||
}, | ||
]; | ||
actions$ = of({ type: '[Budget Page] Init Budgets' }); | ||
ynabSpy.getBudgets.and.returnValue(of(mockBudgets)); | ||
|
||
effects.loadBudgets$.subscribe((action) => { | ||
expect(action.budgets).toEqual(mockBudgets); | ||
expect(action).toEqual(budgetActions.setBudgets({ budgets: mockBudgets })); | ||
}); | ||
|
||
expect(ynabSpy.getBudgets).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
src/app/reports/feature/budget-cards/budget-cards.reducers.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { budgetActions } from './budget-cards.actions'; | ||
import * as fromReducer from './budget-cards.reducers'; | ||
|
||
describe('Budget Cards Reducers', () => { | ||
describe('unknown action', () => { | ||
it('should return the default state', () => { | ||
const { initialState } = fromReducer; | ||
const action = { | ||
type: 'Unknown', | ||
}; | ||
const state = fromReducer.budgetsReducers(initialState, action); | ||
|
||
expect(state).toBe(initialState); | ||
}); | ||
}); | ||
|
||
describe('#setBudgets()', () => { | ||
it('should set the budget cards', () => { | ||
const action = budgetActions.setBudgets({ | ||
budgets: [{ id: '1', name: 'Test Budget 1' }], | ||
}); | ||
const result = fromReducer.budgetsReducers(fromReducer.initialState, action); | ||
expect(result.budgets).toEqual([{ id: '1', name: 'Test Budget 1' }]); | ||
}); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
src/app/reports/feature/budget-cards/budget-cards.selectors.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { BudgetState } from './budget-cards.reducers'; | ||
import { selectBudgets } from './budget-cards.selectors'; | ||
|
||
describe('Budget Cards Selectors', () => { | ||
describe('selectBudgets', () => { | ||
it('should select budgets sorted by last_modified_on', () => { | ||
const initialState: BudgetState = { | ||
budgets: [ | ||
{ | ||
id: '1', | ||
name: 'Test Budget 1', | ||
last_modified_on: '2022-01-01', | ||
}, | ||
{ | ||
id: '2', | ||
name: 'Test Budget 2', | ||
last_modified_on: '2022-02-01', | ||
}, | ||
], | ||
}; | ||
const result = selectBudgets.projector(initialState); | ||
|
||
expect(result).toHaveSize(2); | ||
expect(result).toEqual([ | ||
{ | ||
id: '2', | ||
name: 'Test Budget 2', | ||
last_modified_on: new Date('2022-02-01'), | ||
}, | ||
{ | ||
id: '1', | ||
name: 'Test Budget 1', | ||
last_modified_on: new Date('2022-01-01'), | ||
}, | ||
]); | ||
}); | ||
|
||
it('should handle undefined last_modified_on', () => { | ||
const initialState: BudgetState = { | ||
budgets: [ | ||
{ | ||
id: '1', | ||
name: 'Test Budget 1', | ||
}, | ||
{ | ||
id: '2', | ||
name: 'Test Budget 2', | ||
last_modified_on: '2022-02-01', | ||
}, | ||
], | ||
}; | ||
const result = selectBudgets.projector(initialState); | ||
|
||
expect(result).toHaveSize(2); | ||
expect(result).toEqual([ | ||
{ | ||
id: '2', | ||
name: 'Test Budget 2', | ||
last_modified_on: new Date('2022-02-01'), | ||
}, | ||
{ | ||
id: '1', | ||
name: 'Test Budget 1', | ||
last_modified_on: null, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |