|
| 1 | +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | +import {TestBed} from '@angular/core/testing'; |
| 16 | +import {EffectsModule} from '@ngrx/effects'; |
| 17 | +import {provideMockActions} from '@ngrx/effects/testing'; |
| 18 | +import {Action, createAction, Store} from '@ngrx/store'; |
| 19 | +import {MockStore, provideMockStore} from '@ngrx/store/testing'; |
| 20 | +import {ReplaySubject} from 'rxjs'; |
| 21 | +import {State} from '../app_state'; |
| 22 | +import * as alertActions from './actions'; |
| 23 | +import {AlertActionModule} from './alert_action_module'; |
| 24 | +import {AlertEffects} from './effects'; |
| 25 | + |
| 26 | +const alertActionOccurred = createAction('[Test] Action Occurred (need alert)'); |
| 27 | +const noAlertActionOccurred = createAction('[Test] Action Occurred (no alert)'); |
| 28 | + |
| 29 | +describe('alert_effects', () => { |
| 30 | + let actions$: ReplaySubject<Action>; |
| 31 | + let store: MockStore<Partial<State>>; |
| 32 | + let recordedActions: Action[] = []; |
| 33 | + let shouldReportAlert: boolean; |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + shouldReportAlert = false; |
| 37 | + actions$ = new ReplaySubject<Action>(1); |
| 38 | + |
| 39 | + await TestBed.configureTestingModule({ |
| 40 | + imports: [ |
| 41 | + AlertActionModule.registerAlertActions(() => [ |
| 42 | + { |
| 43 | + actionCreator: alertActionOccurred, |
| 44 | + alertFromAction: (action: Action) => { |
| 45 | + if (shouldReportAlert) { |
| 46 | + return {localizedMessage: 'alert details'}; |
| 47 | + } |
| 48 | + return null; |
| 49 | + }, |
| 50 | + }, |
| 51 | + ]), |
| 52 | + EffectsModule.forFeature([AlertEffects]), |
| 53 | + EffectsModule.forRoot([]), |
| 54 | + ], |
| 55 | + providers: [provideMockActions(actions$), provideMockStore({})], |
| 56 | + }).compileComponents(); |
| 57 | + |
| 58 | + store = TestBed.inject<Store<State>>(Store) as MockStore<State>; |
| 59 | + recordedActions = []; |
| 60 | + spyOn(store, 'dispatch').and.callFake((action: Action) => { |
| 61 | + recordedActions.push(action); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it(`reports an alert when 'alertFromAction' returns a report`, () => { |
| 66 | + shouldReportAlert = true; |
| 67 | + actions$.next(alertActionOccurred); |
| 68 | + |
| 69 | + expect(recordedActions).toEqual([ |
| 70 | + alertActions.alertReported({localizedMessage: 'alert details'}), |
| 71 | + ]); |
| 72 | + }); |
| 73 | + |
| 74 | + it(`does not alert when 'alertFromAction' returns null`, () => { |
| 75 | + shouldReportAlert = false; |
| 76 | + actions$.next(alertActionOccurred); |
| 77 | + |
| 78 | + expect(recordedActions).toEqual([]); |
| 79 | + }); |
| 80 | + |
| 81 | + it(`does not alert when a non-matching action is fired`, () => { |
| 82 | + shouldReportAlert = true; |
| 83 | + actions$.next(noAlertActionOccurred); |
| 84 | + |
| 85 | + expect(recordedActions).toEqual([]); |
| 86 | + }); |
| 87 | +}); |
0 commit comments