|
1 |
| -import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 1 | +import * as angularCore from '@angular/core'; |
2 | 2 | import { MultipleSourcesContainerComponent } from './multiple-sources-container.component';
|
| 3 | +import { PunkApiService } from '../service/punk-api.service'; |
| 4 | +import { Beer, Brews } from '../model/brews'; |
| 5 | +import { cold } from 'jest-marbles'; |
| 6 | +import { BrewDateForm } from '../model/form'; |
| 7 | +import { |
| 8 | + HttpRequestState, |
| 9 | + loadedState, |
| 10 | + loadingState, |
| 11 | +} from 'ngx-http-request-state'; |
3 | 12 |
|
4 | 13 | describe('MultipleSourcesContainerComponent', () => {
|
5 |
| - let component: MultipleSourcesContainerComponent; |
6 |
| - let fixture: ComponentFixture<MultipleSourcesContainerComponent>; |
| 14 | + function setup(): { |
| 15 | + component: MultipleSourcesContainerComponent; |
| 16 | + service: jest.Mocked<PunkApiService>; |
| 17 | + } { |
| 18 | + const service = { |
| 19 | + brewedBefore: jest.fn(), |
| 20 | + brewedAfter: jest.fn(), |
| 21 | + } as unknown as jest.Mocked<PunkApiService>; |
| 22 | + jest.spyOn(angularCore, 'inject').mockImplementation((token) => { |
| 23 | + if (token === PunkApiService) { |
| 24 | + return service; |
| 25 | + } |
| 26 | + throw new Error('No provider for ' + token); |
| 27 | + }); |
| 28 | + const component = new MultipleSourcesContainerComponent(); |
| 29 | + return { component, service }; |
| 30 | + } |
7 | 31 |
|
8 |
| - beforeEach(async () => { |
9 |
| - await TestBed.configureTestingModule({ |
10 |
| - imports: [MultipleSourcesContainerComponent], |
11 |
| - }).compileComponents(); |
| 32 | + describe('#beers$', () => { |
| 33 | + it('should emit search results after both streams have loaded', () => { |
| 34 | + const beforeTiming = ' --(d|)'; |
| 35 | + const afterTiming = ' ----(d|)'; |
| 36 | + const searches = ' --a----------b----'; |
| 37 | + const expected = ' --L---a------L---b'; |
12 | 38 |
|
13 |
| - fixture = TestBed.createComponent(MultipleSourcesContainerComponent); |
14 |
| - component = fixture.componentInstance; |
15 |
| - fixture.detectChanges(); |
16 |
| - }); |
| 39 | + const before: Beer[] = [ |
| 40 | + { id: 'a', name: 'Duff', description: 'Duff beer' }, |
| 41 | + ]; |
| 42 | + const after: Beer[] = [ |
| 43 | + { id: 'b', name: 'Stuff', description: 'Stuff beer' }, |
| 44 | + ]; |
| 45 | + |
| 46 | + const searchValues: Record<'a' | 'b', BrewDateForm> = { |
| 47 | + a: { year: '1985', month: '02' }, |
| 48 | + b: { year: '2000', month: '10' }, |
| 49 | + }; |
| 50 | + |
| 51 | + const expectedValues: Record< |
| 52 | + string, |
| 53 | + HttpRequestState<Brews & BrewDateForm> |
| 54 | + > = { |
| 55 | + L: loadingState(), |
| 56 | + a: loadedState({ |
| 57 | + before, |
| 58 | + after, |
| 59 | + ...searchValues.a, |
| 60 | + }), |
| 61 | + b: loadedState({ |
| 62 | + before, |
| 63 | + after, |
| 64 | + ...searchValues.b, |
| 65 | + }), |
| 66 | + }; |
| 67 | + |
| 68 | + const { component, service } = setup(); |
| 69 | + |
| 70 | + service.brewedBefore.mockReturnValue(cold(beforeTiming, { d: before })); |
| 71 | + service.brewedAfter.mockReturnValue(cold(afterTiming, { d: after })); |
| 72 | + |
| 73 | + cold(searches, searchValues).subscribe((s) => component.search(s)); |
| 74 | + |
| 75 | + expect(component.beers$).toBeObservable(cold(expected, expectedValues)); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should continue to emit for new searches after an earlier error', () => { |
| 79 | + const { component, service } = setup(); |
| 80 | + //TODO |
| 81 | + }); |
17 | 82 |
|
18 |
| - it('should create', () => { |
19 |
| - expect(component).toBeTruthy(); |
| 83 | + it('should drop inflight requests when a new search has been triggered', () => { |
| 84 | + const { component, service } = setup(); |
| 85 | + //TODO |
| 86 | + }); |
20 | 87 | });
|
21 | 88 | });
|
0 commit comments