/
countries.effects.spec.ts
60 lines (46 loc) · 2.32 KB
/
countries.effects.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { TestBed } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Action, StoreModule } from '@ngrx/store';
import { cold, hot } from 'jest-marbles';
import { Observable, of, throwError } from 'rxjs';
import { instance, mock, when } from 'ts-mockito';
import { Country } from '../../models/country/country.model';
import { HttpError } from '../../models/http-error/http-error.model';
import { CountryService } from '../../services/country/country.service';
import { coreReducers } from '../core-store.module';
import { CountryActionTypes, LoadCountriesFail, LoadCountriesSuccess } from './countries.actions';
import { CountriesEffects } from './countries.effects';
describe('Countries Effects', () => {
let actions$: Observable<Action>;
let effects: CountriesEffects;
let countryServiceMock: CountryService;
const countries = [{ countryCode: 'BG', name: 'Bulgaria' }, { countryCode: 'DE', name: 'Germany' }] as Country[];
beforeEach(() => {
countryServiceMock = mock(CountryService);
when(countryServiceMock.getCountries()).thenReturn(of(countries));
TestBed.configureTestingModule({
imports: [StoreModule.forRoot(coreReducers)],
providers: [
CountriesEffects,
provideMockActions(() => actions$),
{ provide: CountryService, useFactory: () => instance(countryServiceMock) },
],
});
effects = TestBed.get(CountriesEffects);
});
describe('loadCountries$', () => {
it('should load all countries on effects init and dispatch a LoadCountriesSuccess action', () => {
const action = { type: CountryActionTypes.LoadCountries } as Action;
const expected = new LoadCountriesSuccess({ countries });
actions$ = hot('-a-------', { a: action });
expect(effects.loadCountries$).toBeObservable(cold('-b-------', { b: expected }));
});
it('should dispatch a LoadCountriesFail action if a load error occurs', () => {
when(countryServiceMock.getCountries()).thenReturn(throwError({ message: 'error' }));
const action = { type: CountryActionTypes.LoadCountries } as Action;
const expected = new LoadCountriesFail({ error: { message: 'error' } as HttpError });
actions$ = hot('-a', { a: action });
expect(effects.loadCountries$).toBeObservable(cold('-b', { b: expected }));
});
});
});