Skip to content

Commit

Permalink
test: add missing tests for budget-cards.component
Browse files Browse the repository at this point in the history
Add tests for budget-cards effects, reducers, and selectors.
  • Loading branch information
grantwforsythe committed Apr 11, 2024
1 parent 954a48d commit 574f3ff
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/app/reports/feature/budget-cards/budget-cards.effects.spec.ts
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 src/app/reports/feature/budget-cards/budget-cards.reducers.spec.ts
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' }]);
});
});
});
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,
},
]);
});
});
});

0 comments on commit 574f3ff

Please sign in to comment.