Skip to content

Commit

Permalink
fix(app): set token works again
Browse files Browse the repository at this point in the history
  • Loading branch information
Anlanther committed Nov 10, 2024
1 parent 972b457 commit 303717f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion calendar-app/src/app/state/app.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
37 changes: 29 additions & 8 deletions calendar-app/src/app/state/app.effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -266,6 +268,7 @@ describe('AppEffects', () => {

describe('getOrCreateSystemEvents$', () => {
const mockGameEventResponseFromApi: GameEvent[] = MOCK_GAME_EVENTS;
const mockError = { message: 'Error' };

beforeEach(() => {
mockStore.overrideSelector(AppFeature.selectOfflineMode, false);
Expand All @@ -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() });
Expand All @@ -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() });
Expand All @@ -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() });
Expand All @@ -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() });
Expand All @@ -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$', () => {
Expand Down
21 changes: 15 additions & 6 deletions calendar-app/src/app/state/app.effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}),
),
);

Expand Down Expand Up @@ -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));
}),
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion calendar-app/src/app/state/app.reducer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions calendar-app/src/app/state/app.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ export const appReducer = createReducer<AppState>(
seasonNavOpen: false,
};
}),
on(AppActions.aPIFailed, (state) => ({
on(AppActions.aPIFailed, (state, action) => ({
...state,
apiFailed: true,
apiFailed: action.failed,
})),
on(AppActions.setOfflineMode, (state) => ({
...state,
Expand Down

0 comments on commit 303717f

Please sign in to comment.