From 303717fc369ae475cfe423278220d4e16cc98b45 Mon Sep 17 00:00:00 2001 From: Anlanther Date: Sun, 10 Nov 2024 14:24:09 +0800 Subject: [PATCH] fix(app): set token works again --- calendar-app/src/app/state/app.actions.ts | 2 +- calendar-app/src/app/state/app.effect.spec.ts | 37 +++++++++++++++---- calendar-app/src/app/state/app.effect.ts | 21 ++++++++--- .../src/app/state/app.reducer.spec.ts | 2 +- calendar-app/src/app/state/app.reducer.ts | 4 +- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/calendar-app/src/app/state/app.actions.ts b/calendar-app/src/app/state/app.actions.ts index bef7457..45836a1 100644 --- a/calendar-app/src/app/state/app.actions.ts +++ b/calendar-app/src/app/state/app.actions.ts @@ -76,7 +76,7 @@ export const AppActions = createActionGroup({ calendar, gameEvent, }), - 'API Failed': emptyProps(), + 'API Failed': (failed: boolean) => ({ failed }), 'Toggle Event Nav': (isOpen: boolean) => ({ isOpen }), 'Toggle Season Nav': (isOpen: boolean) => ({ isOpen }), Initialise: emptyProps(), diff --git a/calendar-app/src/app/state/app.effect.spec.ts b/calendar-app/src/app/state/app.effect.spec.ts index 5c4d7e8..590892f 100644 --- a/calendar-app/src/app/state/app.effect.spec.ts +++ b/calendar-app/src/app/state/app.effect.spec.ts @@ -114,8 +114,9 @@ describe('AppEffects', () => { ); }); it('should call on getCalendarsSuccess with a list of calendars obtained from calendar data service', () => { - const expected = cold('-a', { + const expected = cold('-(ab)', { a: AppActions.getCalendarsSuccess(mockCalendarsResponseFromApi), + b: AppActions.aPIFailed(false), }); expect(spectator.service.getAllCalendars$).toBeObservable(expected); @@ -128,8 +129,9 @@ describe('AppEffects', () => { it('should call on getCalendarsSuccess with a list of calendars obtained from offline data service when the app is on offline mode', () => { mockStore.overrideSelector(AppFeature.selectOfflineMode, true); - const expected = cold('-a', { + const expected = cold('-(ab)', { a: AppActions.getCalendarsSuccess(mockCalendarsResponseFromApi), + b: AppActions.aPIFailed(false), }); expect(spectator.service.getAllCalendars$).toBeObservable(expected); @@ -144,8 +146,8 @@ describe('AppEffects', () => { throwError(() => mockError), ); mockActions$ = hot('-a', { a: AppActions.getCalendars() }); - const expected = cold('-(a|)', { - a: AppActions.aPIFailed(), + const expected = cold('-a', { + a: AppActions.aPIFailed(true), }); expect(spectator.service.getAllCalendars$).toBeObservable(expected); @@ -266,6 +268,7 @@ describe('AppEffects', () => { describe('getOrCreateSystemEvents$', () => { const mockGameEventResponseFromApi: GameEvent[] = MOCK_GAME_EVENTS; + const mockError = { message: 'Error' }; beforeEach(() => { mockStore.overrideSelector(AppFeature.selectOfflineMode, false); @@ -282,10 +285,11 @@ describe('AppEffects', () => { mockGameEventDataService.getOrCreateDefaults.mockReturnValue( of(mockGameEventResponseFromApi), ); - const expected = cold('-a', { + const expected = cold('-(ab)', { a: AppActions.createDefaultGameEventsSuccess( mockGameEventResponseFromApi, ), + b: AppActions.aPIFailed(false), }); mockActions$ = hot('-a', { a: AppActions.createDefaultGameEvents() }); @@ -298,10 +302,11 @@ describe('AppEffects', () => { }); }); it('should trigger gameEventDataService to get or create default game events when updatedSystemEventsSuccess is called', () => { - const expected = cold('-a', { + const expected = cold('-(ab)', { a: AppActions.createDefaultGameEventsSuccess( mockGameEventResponseFromApi, ), + b: AppActions.aPIFailed(false), }); mockActions$ = hot('-a', { a: AppActions.updatedSystemEventsSuccess() }); @@ -316,10 +321,11 @@ describe('AppEffects', () => { it('should trigger offlineDataService to get or create default game events when updatedSystemEventsSuccess is called and app is set to offline mode', () => { mockStore.overrideSelector(AppFeature.selectOfflineMode, true); - const expected = cold('-a', { + const expected = cold('-(ab)', { a: AppActions.createDefaultGameEventsSuccess( mockGameEventResponseFromApi, ), + b: AppActions.aPIFailed(false), }); mockActions$ = hot('-a', { a: AppActions.updatedSystemEventsSuccess() }); @@ -337,10 +343,11 @@ describe('AppEffects', () => { it('should trigger offlineDataService API to get or create default game events when createDefaultGameEvents is called and app is set to offline mode', () => { mockStore.overrideSelector(AppFeature.selectOfflineMode, true); - const expected = cold('-a', { + const expected = cold('-(ab)', { a: AppActions.createDefaultGameEventsSuccess( mockGameEventResponseFromApi, ), + b: AppActions.aPIFailed(false), }); mockActions$ = hot('-a', { a: AppActions.createDefaultGameEvents() }); @@ -354,6 +361,20 @@ describe('AppEffects', () => { ).toHaveBeenCalled(); }); }); + + it('should trigger aPIFailed action when game event data service fails to return a list of game events', () => { + mockGameEventDataService.getOrCreateDefaults.mockReturnValue( + throwError(() => mockError), + ); + mockActions$ = hot('-a', { a: AppActions.createDefaultGameEvents() }); + const expected = cold('-a', { + a: AppActions.aPIFailed(true), + }); + + expect(spectator.service.getOrCreateSystemEvents$).toBeObservable( + expected, + ); + }); }); describe('selectCalendar$', () => { diff --git a/calendar-app/src/app/state/app.effect.ts b/calendar-app/src/app/state/app.effect.ts index 34be182..3e3a861 100644 --- a/calendar-app/src/app/state/app.effect.ts +++ b/calendar-app/src/app/state/app.effect.ts @@ -53,11 +53,16 @@ export class AppEffects { (offlineMode ? this.offlineDataService.getAllCalendars() : this.calendarDataService.getAll() - ).pipe(map((calendars) => AppActions.getCalendarsSuccess(calendars))), + ).pipe( + switchMap((calendars) => [ + AppActions.getCalendarsSuccess(calendars), + AppActions.aPIFailed(false), + ]), + catchError(() => { + return of(AppActions.aPIFailed(true)); + }), + ), ), - catchError(() => { - return of(AppActions.aPIFailed()); - }), ), ); @@ -140,9 +145,13 @@ export class AppEffects { ? this.offlineDataService.getOrCreateEventDefaults() : this.gameEventDataService.getOrCreateDefaults() ).pipe( - map((gameEvents) => + switchMap((gameEvents) => [ AppActions.createDefaultGameEventsSuccess(gameEvents), - ), + AppActions.aPIFailed(false), + ]), + catchError(() => { + return of(AppActions.aPIFailed(true)); + }), ), ), ), diff --git a/calendar-app/src/app/state/app.reducer.spec.ts b/calendar-app/src/app/state/app.reducer.spec.ts index 143c133..2cf9a3e 100644 --- a/calendar-app/src/app/state/app.reducer.spec.ts +++ b/calendar-app/src/app/state/app.reducer.spec.ts @@ -208,7 +208,7 @@ describe('AppReducer', () => { }); describe(AppActions.aPIFailed.type, () => { it('should update the state', () => { - const action = AppActions.aPIFailed(); + const action = AppActions.aPIFailed(true); const actual = appReducer(state, action); diff --git a/calendar-app/src/app/state/app.reducer.ts b/calendar-app/src/app/state/app.reducer.ts index d8ac853..a1d501d 100644 --- a/calendar-app/src/app/state/app.reducer.ts +++ b/calendar-app/src/app/state/app.reducer.ts @@ -325,9 +325,9 @@ export const appReducer = createReducer( seasonNavOpen: false, }; }), - on(AppActions.aPIFailed, (state) => ({ + on(AppActions.aPIFailed, (state, action) => ({ ...state, - apiFailed: true, + apiFailed: action.failed, })), on(AppActions.setOfflineMode, (state) => ({ ...state,