Skip to content

Commit

Permalink
Added the ability to set the page type [#362]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed Jun 24, 2020
1 parent 15c0101 commit 3a18b7c
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 27 deletions.
24 changes: 24 additions & 0 deletions comixed-frontend/src/app/comics/actions/comic.actions.ts
Expand Up @@ -35,6 +35,9 @@ export enum ComicActionTypes {
SavePage = '[COMIC] Save a single page',
PageSaved = '[COMIC] Page was saved',
SavePageFailed = '[COMIC] Page was not saved',
SetPageType = '[COMIC] Set the page type',
PageTypeSet = '[COMIC] The page type is set',
SetPageTypeFailed = '[COMIC] Set the page type failed',
SetPageHashBlocking = '[COMIC] Set the blocking state for a page hash',
PageHashBlockingSet = '[COMIC] The blocking state is set for a page hash',
SetPageHashBlockingFailed = '[COMIC] Failed to set the block state for a page hash',
Expand Down Expand Up @@ -145,6 +148,24 @@ export class ComicSavePageFailed implements Action {
constructor() {}
}

export class ComicSetPageType implements Action {
readonly type = ComicActionTypes.SetPageType;

constructor(public payload: { page: Page; pageType: PageType }) {}
}

export class ComicPageTypeSet implements Action {
readonly type = ComicActionTypes.PageTypeSet;

constructor(public payload: { page: Page }) {}
}

export class ComicSetPageTypeFailed implements Action {
readonly type = ComicActionTypes.SetPageTypeFailed;

constructor() {}
}

export class ComicSetPageHashBlocking implements Action {
readonly type = ComicActionTypes.SetPageHashBlocking;

Expand Down Expand Up @@ -269,6 +290,9 @@ export type ComicActions =
| ComicSavePage
| ComicPageSaved
| ComicSavePageFailed
| ComicSetPageType
| ComicPageTypeSet
| ComicSetPageTypeFailed
| ComicSetPageHashBlocking
| ComicPageHashBlockingSet
| ComicSetPageHashBlockingFailed
Expand Down
63 changes: 55 additions & 8 deletions comixed-frontend/src/app/comics/adaptors/comic.adaptor.spec.ts
Expand Up @@ -38,12 +38,15 @@ import {
ComicMarkAsRead,
ComicMarkAsReadFailed,
ComicMarkedAsRead,
ComicPageTypeSet,
ComicRestore,
ComicRestored,
ComicRestoreFailed,
ComicSave,
ComicSavePage,
ComicSetPageHashBlocking
ComicSetPageHashBlocking,
ComicSetPageType,
ComicSetPageTypeFailed
} from 'app/comics/actions/comic.actions';
import { ComicEffects } from 'app/comics/effects/comic.effects';
import {
Expand All @@ -53,7 +56,6 @@ import {
} from 'app/comics/models/comic-format.fixtures';
import { COMIC_1 } from 'app/comics/models/comic.fixtures';
import { FRONT_COVER } from 'app/comics/models/page-type.fixtures';
import { PAGE_1 } from 'app/comics/models/page.fixtures';
import {
SCAN_TYPE_1,
SCAN_TYPE_3,
Expand All @@ -71,6 +73,8 @@ describe('ComicAdaptor', () => {
const COMIC = COMIC_1;
const SKIP_CACHE = false;
const LAST_READ_DATE = COMIC_1_LAST_READ_DATE;
const PAGE = COMIC.pages[1];
const PAGE_TYPE = FRONT_COVER;

let adaptor: ComicAdaptor;
let store: Store<AppState>;
Expand Down Expand Up @@ -237,26 +241,69 @@ describe('ComicAdaptor', () => {
});

it('can save changes to a page', () => {
adaptor.savePage(PAGE_1);
adaptor.savePage(PAGE);
expect(store.dispatch).toHaveBeenCalledWith(
new ComicSavePage({ page: PAGE_1 })
new ComicSavePage({ page: PAGE })
);
});

it('can block a page hash', () => {
adaptor.blockPageHash(PAGE_1);
adaptor.blockPageHash(PAGE);
expect(store.dispatch).toHaveBeenCalledWith(
new ComicSetPageHashBlocking({ page: PAGE_1, state: true })
new ComicSetPageHashBlocking({ page: PAGE, state: true })
);
});

it('can unblock a page hash', () => {
adaptor.unblockPageHash(PAGE_1);
adaptor.unblockPageHash(PAGE);
expect(store.dispatch).toHaveBeenCalledWith(
new ComicSetPageHashBlocking({ page: PAGE_1, state: false })
new ComicSetPageHashBlocking({ page: PAGE, state: false })
);
});

describe('can change the page type', () => {
beforeEach(() => {
store.dispatch(new ComicGotIssue({ comic: COMIC }));
adaptor.setPageType(PAGE, PAGE_TYPE);
});

it('fires an action', () => {
expect(store.dispatch).toHaveBeenCalledWith(
new ComicSetPageType({ page: PAGE, pageType: PAGE_TYPE })
);
});

it('provides updates on setting the page type', () => {
adaptor.settingPageType$.subscribe(response =>
expect(response).toBeTruthy()
);
});

describe('success', () => {
beforeEach(() => {
store.dispatch(new ComicPageTypeSet({ page: PAGE }));
});

it('provides updates on setting the page type', () => {
adaptor.settingPageType$.subscribe(response =>
expect(response).toBeFalsy()
);
});
});

describe('failure', () => {
beforeEach(() => {
store.dispatch(new ComicSetPageTypeFailed());
});

it('provides updates on setting the page type', () => {
adaptor.settingPageType$.subscribe(response =>
expect(response).toBeFalsy()
);
});
});
});

it('can save a comic', () => {
adaptor.saveComic(COMIC);
expect(store.dispatch).toHaveBeenCalledWith(
Expand Down
18 changes: 17 additions & 1 deletion comixed-frontend/src/app/comics/adaptors/comic.adaptor.ts
Expand Up @@ -37,7 +37,8 @@ import {
ComicRestore,
ComicSave,
ComicSavePage,
ComicSetPageHashBlocking
ComicSetPageHashBlocking,
ComicSetPageType
} from 'app/comics/actions/comic.actions';
import {
COMIC_FEATURE_KEY,
Expand All @@ -63,6 +64,7 @@ export class ComicAdaptor {
private _deletingComic$ = new BehaviorSubject<boolean>(false);
private _restoringComic$ = new BehaviorSubject<boolean>(false);
private _markingAsRead$ = new BehaviorSubject<boolean>(false);
private _settingPageType$ = new BehaviorSubject<boolean>(false);

constructor(private logger: LoggerService, private store: Store<AppState>) {
this.store
Expand Down Expand Up @@ -109,6 +111,9 @@ export class ComicAdaptor {
if (state.settingReadState !== this._markingAsRead$.getValue()) {
this._markingAsRead$.next(state.settingReadState);
}
if (state.settingPageType !== this._settingPageType$.getValue()) {
this._settingPageType$.next(state.settingPageType);
}
});
}

Expand Down Expand Up @@ -236,4 +241,15 @@ export class ComicAdaptor {
this.logger.debug('marking comic as unread:', comic);
this.store.dispatch(new ComicMarkAsRead({ comic: comic, read: false }));
}

setPageType(page: Page, pageType: PageType): void {
this.logger.debug('firing action to set page type:', page, pageType);
this.store.dispatch(
new ComicSetPageType({ page: page, pageType: pageType })
);
}

get settingPageType$(): Observable<boolean> {
return this._settingPageType$.asObservable();
}
}
1 change: 1 addition & 0 deletions comixed-frontend/src/app/comics/comics.constants.ts
Expand Up @@ -34,6 +34,7 @@ export const MARK_COMIC_AS_UNREAD_URL = `${API_ROOT_URL}/comics/\${id}/read`;
export const MISSING_COMIC_IMAGE_URL = '/assets/img/missing-comic-file.png';

export const SAVE_PAGE_URL = `${API_ROOT_URL}/pages/\${id}`;
export const SET_PAGE_TYPE_URL = `${API_ROOT_URL}/pages/\${id}/type`;
export const GET_PAGE_CONTENT_URL = `${API_ROOT_URL}/pages/\${id}/content`;
export const BLOCK_PAGE_HASH_URL = `${API_ROOT_URL}/pages/\${id}/block/\${hash}`;
export const UNBLOCK_PAGE_HASH_URL = `${API_ROOT_URL}/pages/\${id}/unblock/\${hash}`;
Expand Down
81 changes: 73 additions & 8 deletions comixed-frontend/src/app/comics/effects/comic.effects.spec.ts
Expand Up @@ -44,6 +44,7 @@ import {
ComicMetadataCleared,
ComicPageHashBlockingSet,
ComicPageSaved,
ComicPageTypeSet,
ComicRestore,
ComicRestored,
ComicRestoreFailed,
Expand All @@ -53,15 +54,21 @@ import {
ComicSavePage,
ComicSavePageFailed,
ComicSetPageHashBlocking,
ComicSetPageHashBlockingFailed
ComicSetPageHashBlockingFailed,
ComicSetPageType,
ComicSetPageTypeFailed
} from 'app/comics/actions/comic.actions';
import {
FORMAT_1,
FORMAT_3,
FORMAT_5
} from 'app/comics/models/comic-format.fixtures';
import { COMIC_1 } from 'app/comics/models/comic.fixtures';
import { BACK_COVER, FRONT_COVER } from 'app/comics/models/page-type.fixtures';
import {
BACK_COVER,
FRONT_COVER,
STORY
} from 'app/comics/models/page-type.fixtures';
import { PAGE_1 } from 'app/comics/models/page.fixtures';
import {
SCAN_TYPE_1,
Expand All @@ -83,6 +90,8 @@ describe('ComicEffects', () => {
const COMIC = COMIC_1;
const SKIP_CACHE = false;
const LAST_READ_DATE = COMIC_1_LAST_READ_DATE;
const PAGE = PAGE_1;
const PAGE_TYPE = STORY;

let actions$: Observable<any>;
let effects: ComicEffects;
Expand Down Expand Up @@ -115,6 +124,7 @@ describe('ComicEffects', () => {
provide: PageService,
useValue: {
savePage: jasmine.createSpy('PageService.savePage'),
setPageType: jasmine.createSpy('PageService.setPageType'),
setPageHashBlocking: jasmine.createSpy(
'PageService.setPageHashBlocking'
)
Expand Down Expand Up @@ -310,7 +320,7 @@ describe('ComicEffects', () => {
describe('when saving a page', () => {
it('fires an action on success', () => {
const serviceResponse = COMIC;
const action = new ComicSavePage({ page: PAGE_1 });
const action = new ComicSavePage({ page: PAGE });
const outcome = new ComicPageSaved({ comic: serviceResponse });

actions$ = hot('-a', { a: action });
Expand All @@ -325,7 +335,7 @@ describe('ComicEffects', () => {

it('fires an action on service failure', () => {
const serviceResponse = new HttpErrorResponse({});
const action = new ComicSavePage({ page: PAGE_1 });
const action = new ComicSavePage({ page: PAGE });
const outcome = new ComicSavePageFailed();

actions$ = hot('-a', { a: action });
Expand All @@ -339,7 +349,7 @@ describe('ComicEffects', () => {
});

it('fires an action on general failure', () => {
const action = new ComicSavePage({ page: PAGE_1 });
const action = new ComicSavePage({ page: PAGE });
const outcome = new ComicSavePageFailed();

actions$ = hot('-a', { a: action });
Expand All @@ -353,11 +363,66 @@ describe('ComicEffects', () => {
});
});

describe('when setting the page type', () => {
it('fires an action on success', () => {
const serviceResponse = PAGE;
const action = new ComicSetPageType({
page: PAGE,
pageType: PAGE_TYPE
});
const outcome = new ComicPageTypeSet({ page: PAGE });

actions$ = hot('-a', { a: action });
pageService.setPageType.and.returnValue(of(serviceResponse));

const expected = hot('-b', { b: outcome });
expect(effects.setPageType$).toBeObservable(expected);
expect(messageService.add).toHaveBeenCalledWith(
objectContaining({ severity: 'info' })
);
});

it('fires an action on service failure', () => {
const serviceResponse = new HttpErrorResponse({});
const action = new ComicSetPageType({
page: PAGE,
pageType: PAGE_TYPE
});
const outcome = new ComicSetPageTypeFailed();

actions$ = hot('-a', { a: action });
pageService.setPageType.and.returnValue(throwError(serviceResponse));

const expected = hot('-b', { b: outcome });
expect(effects.setPageType$).toBeObservable(expected);
expect(messageService.add).toHaveBeenCalledWith(
objectContaining({ severity: 'error' })
);
});

it('fires an action on general failure', () => {
const action = new ComicSetPageType({
page: PAGE,
pageType: PAGE_TYPE
});
const outcome = new ComicSetPageTypeFailed();

actions$ = hot('-a', { a: action });
pageService.setPageType.and.throwError('expected');

const expected = hot('-(b|)', { b: outcome });
expect(effects.setPageType$).toBeObservable(expected);
expect(messageService.add).toHaveBeenCalledWith(
objectContaining({ severity: 'error' })
);
});
});

describe('when setting the blocked state for a page hash', () => {
it('fires an action on success', () => {
const serviceResponse = COMIC;
const action = new ComicSetPageHashBlocking({
page: PAGE_1,
page: PAGE,
state: true
});
const outcome = new ComicPageHashBlockingSet({ comic: serviceResponse });
Expand All @@ -375,7 +440,7 @@ describe('ComicEffects', () => {
it('fires an action on service failure', () => {
const serviceResponse = new HttpErrorResponse({});
const action = new ComicSetPageHashBlocking({
page: PAGE_1,
page: PAGE,
state: true
});
const outcome = new ComicSetPageHashBlockingFailed();
Expand All @@ -394,7 +459,7 @@ describe('ComicEffects', () => {

it('fires an action on general failure', () => {
const action = new ComicSetPageHashBlocking({
page: PAGE_1,
page: PAGE,
state: true
});
const outcome = new ComicSetPageHashBlockingFailed();
Expand Down

0 comments on commit 3a18b7c

Please sign in to comment.