Skip to content

Commit

Permalink
Removed ApiResponse from the Angular frontend [#465]
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpierce committed Dec 3, 2020
1 parent 2f9a268 commit 437d148
Show file tree
Hide file tree
Showing 15 changed files with 105 additions and 237 deletions.
1 change: 1 addition & 0 deletions comixed-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@angular-devkit/build-angular": "~0.1000.5",
"@angular/cli": "~10.0.5",
"@angular/compiler-cli": "~10.0.6",
"@ngrx/store-devtools": "^10.1.0",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ import { provideMockActions } from '@ngrx/effects/testing';
import { Observable, of, throwError } from 'rxjs';
import { ComicImportEffects } from './comic-import.effects';
import { ComicImportService } from '@app/comic-import/services/comic-import.service';
import { AlertService, ApiResponse } from '@app/core';
import { AlertService } from '@app/core';
import { LoggerModule } from '@angular-ru/logger';
import { TranslateModule } from '@ngx-translate/core';
import {
COMIC_FILE_1,
COMIC_FILE_2,
COMIC_FILE_3,
COMIC_FILE_4,
ROOT_DIRECTORY,
ROOT_DIRECTORY
} from '@app/comic-import/comic-import.fixtures';
import { LoadComicFilesResponse } from '@app/comic-import/models/net/load-comic-files-response';
import {
Expand All @@ -38,10 +38,10 @@ import {
loadComicFiles,
loadComicFilesFailed,
sendComicFiles,
sendComicFilesFailed,
sendComicFilesFailed
} from '@app/comic-import/actions/comic-import.actions';
import { hot } from 'jasmine-marbles';
import { HttpErrorResponse } from '@angular/common/http';
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { MatSnackBarModule } from '@angular/material/snack-bar';

describe('ComicImportEffects', () => {
Expand All @@ -57,7 +57,7 @@ describe('ComicImportEffects', () => {
imports: [
TranslateModule.forRoot(),
LoggerModule.forRoot(),
MatSnackBarModule,
MatSnackBarModule
],
providers: [
ComicImportEffects,
Expand All @@ -70,10 +70,10 @@ describe('ComicImportEffects', () => {
),
sendComicFiles: jasmine.createSpy(
'ComicFileService.sendComicFiles()'
),
},
},
],
)
}
}
]
});

effects = TestBed.inject(ComicImportEffects);
Expand All @@ -91,13 +91,10 @@ describe('ComicImportEffects', () => {

describe('loading comic files', () => {
it('fires an action on success', () => {
const serviceResponse = {
success: true,
result: { files: FILES },
} as ApiResponse<LoadComicFilesResponse>;
const serviceResponse = { files: FILES } as LoadComicFilesResponse;
const action = loadComicFiles({
directory: ROOT_DIRECTORY,
maximum: 100,
maximum: 100
});
const outcome = comicFilesLoaded({ files: FILES });

Expand All @@ -109,29 +106,11 @@ describe('ComicImportEffects', () => {
expect(alertService.info).toHaveBeenCalledWith(jasmine.any(String));
});

it('fires an action on failure', () => {
const serviceResponse = { success: false } as ApiResponse<
LoadComicFilesResponse
>;
const action = loadComicFiles({
directory: ROOT_DIRECTORY,
maximum: 100,
});
const outcome = loadComicFilesFailed();

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

const expected = hot('-b', { b: outcome });
expect(effects.loadComicFiles$).toBeObservable(expected);
expect(alertService.error).toHaveBeenCalledWith(jasmine.any(String));
});

it('fires an action on service failure', () => {
const serviceResponse = new HttpErrorResponse({});
const action = loadComicFiles({
directory: ROOT_DIRECTORY,
maximum: 100,
maximum: 100
});
const outcome = loadComicFilesFailed();

Expand All @@ -148,7 +127,7 @@ describe('ComicImportEffects', () => {
it('fires an action on general failure', () => {
const action = loadComicFiles({
directory: ROOT_DIRECTORY,
maximum: 100,
maximum: 100
});
const outcome = loadComicFilesFailed();

Expand All @@ -163,11 +142,11 @@ describe('ComicImportEffects', () => {

describe('sending comic files', () => {
it('fires an action on success', () => {
const serviceResponse = { success: true } as ApiResponse<void>;
const serviceResponse = new HttpResponse({ status: 200 });
const action = sendComicFiles({
files: FILES,
ignoreMetadata: false,
deleteBlockedPages: true,
deleteBlockedPages: true
});
const outcome = comicFilesSent();

Expand All @@ -179,29 +158,12 @@ describe('ComicImportEffects', () => {
expect(alertService.info).toHaveBeenCalledWith(jasmine.any(String));
});

it('fires an action on failure', () => {
const serviceResponse = { success: false } as ApiResponse<void>;
const action = sendComicFiles({
files: FILES,
ignoreMetadata: false,
deleteBlockedPages: true,
});
const outcome = sendComicFilesFailed();

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

const expected = hot('-b', { b: outcome });
expect(effects.sendComicFiles$).toBeObservable(expected);
expect(alertService.error).toHaveBeenCalledWith(jasmine.any(String));
});

it('fires an action on service failure', () => {
const serviceResponse = new HttpErrorResponse({});
const action = sendComicFiles({
files: FILES,
ignoreMetadata: false,
deleteBlockedPages: true,
deleteBlockedPages: true
});
const outcome = sendComicFilesFailed();

Expand All @@ -219,7 +181,7 @@ describe('ComicImportEffects', () => {
const action = sendComicFiles({
files: FILES,
ignoreMetadata: false,
deleteBlockedPages: true,
deleteBlockedPages: true
});
const outcome = sendComicFilesFailed();

Expand Down
52 changes: 18 additions & 34 deletions comixed-web/src/app/comic-import/effects/comic-import.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { LoggerService } from '@angular-ru/logger';
import { ComicImportService } from '@app/comic-import/services/comic-import.service';
import { AlertService, ApiResponse } from '@app/core';
import { AlertService } from '@app/core';
import { TranslateService } from '@ngx-translate/core';
import {
comicFilesLoaded,
Expand Down Expand Up @@ -56,24 +56,16 @@ export class ComicImportEffects {
})
.pipe(
tap(response => this.logger.debug('Response received:', response)),
tap((response: ApiResponse<LoadComicFilesResponse>) =>
response.success
? this.alertService.info(
this.translateService.instant(
'load-comic-files.effect-success-detail',
{ count: response.result.files.length }
)
)
: this.alertService.error(
this.translateService.instant(
'load-comic-files.effect-failure-detail'
)
)
tap((response: LoadComicFilesResponse) =>
this.alertService.info(
this.translateService.instant(
'load-comic-files.effect-success-detail',
{ count: response.files.length }
)
)
),
map((response: ApiResponse<LoadComicFilesResponse>) =>
response.success
? comicFilesLoaded({ files: response.result.files })
: loadComicFilesFailed()
map((response: LoadComicFilesResponse) =>
comicFilesLoaded({ files: response.files })
),
catchError(error => {
this.logger.error('Service failure:', error);
Expand Down Expand Up @@ -109,23 +101,15 @@ export class ComicImportEffects {
})
.pipe(
tap(response => this.logger.debug('Response received:', response)),
tap((response: ApiResponse<void>) =>
response.success
? this.alertService.info(
this.translateService.instant(
'send-comic-files.effect-success-detail',
{ count: action.files.length }
)
)
: this.alertService.error(
this.translateService.instant(
'send-comic-files.effect-failure-detail'
)
)
),
map((response: ApiResponse<void>) =>
response.success ? comicFilesSent() : sendComicFilesFailed()
tap(() =>
this.alertService.info(
this.translateService.instant(
'send-comic-files.effect-success-detail',
{ count: action.files.length }
)
)
),
map(() => comicFilesSent()),
catchError(error => {
this.logger.error('Service failure:', error);
this.alertService.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ import {
COMIC_FILE_2,
COMIC_FILE_3,
COMIC_FILE_4,
ROOT_DIRECTORY,
ROOT_DIRECTORY
} from '@app/comic-import/comic-import.fixtures';
import { ApiResponse, interpolate } from '@app/core';
import { interpolate } from '@app/core';
import { LoadComicFilesResponse } from '@app/comic-import/models/net/load-comic-files-response';
import {
HttpClientTestingModule,
HttpTestingController,
HttpTestingController
} from '@angular/common/http/testing';
import {
LOAD_COMIC_FILES_URL,
SEND_COMIC_FILES_URL,
SEND_COMIC_FILES_URL
} from '@app/comic-import/comic-import.constants';
import { LoadComicFilesRequest } from '@app/comic-import/models/net/load-comic-files-request';
import { SendComicFilesRequest } from '@app/comic-import/models/net/send-comic-files-request';
import { HttpResponse } from '@angular/common/http';

describe('ComicImportService', () => {
const FILES = [COMIC_FILE_1, COMIC_FILE_2, COMIC_FILE_3, COMIC_FILE_4];
Expand All @@ -50,7 +51,7 @@ describe('ComicImportService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, LoggerModule.forRoot()],
imports: [HttpClientTestingModule, LoggerModule.forRoot()]
});

service = TestBed.inject(ComicImportService);
Expand All @@ -63,38 +64,37 @@ describe('ComicImportService', () => {

it('can load comic files', () => {
const serviceResponse = {
success: true,
result: { files: FILES },
} as ApiResponse<LoadComicFilesResponse>;
files: FILES
} as LoadComicFilesResponse;
service
.loadComicFiles({ directory: ROOT_DIRECTORY, maximum: MAXIMUM })
.subscribe((response) => expect(response).toEqual(serviceResponse));
.subscribe(response => expect(response).toEqual(serviceResponse));

const req = httpMock.expectOne(interpolate(LOAD_COMIC_FILES_URL));
expect(req.request.method).toEqual('POST');
expect(req.request.body).toEqual({
directory: ROOT_DIRECTORY,
maximum: MAXIMUM,
maximum: MAXIMUM
} as LoadComicFilesRequest);
req.flush(serviceResponse);
});

it('can send comic files', () => {
const serviceResponse = { success: true } as ApiResponse<void>;
const serviceResponse = new HttpResponse({ status: 200 });
service
.sendComicFiles({
files: FILES,
ignoreMetadata: IGNORE_METADATA,
deleteBlockedPages: DELETE_BLOCKED_PAGES,
deleteBlockedPages: DELETE_BLOCKED_PAGES
})
.subscribe((response) => expect(response).toEqual(serviceResponse));
.subscribe(response => expect(response).toEqual(serviceResponse));

const req = httpMock.expectOne(interpolate(SEND_COMIC_FILES_URL));
expect(req.request.method).toEqual('POST');
expect(req.request.body).toEqual({
filenames: FILES.map((file) => file.filename),
filenames: FILES.map(file => file.filename),
ignoreMetadata: IGNORE_METADATA,
deleteBlockedPages: DELETE_BLOCKED_PAGES,
deleteBlockedPages: DELETE_BLOCKED_PAGES
} as SendComicFilesRequest);
req.flush(serviceResponse);
});
Expand Down
1 change: 0 additions & 1 deletion comixed-web/src/app/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export { TokenService } from '@app/core/services/token.service';
export { AlertService } from '@app/core/services/alert.service';
export { ConfirmationService } from '@app/core/services/confirmation.service';

export { ApiResponse } from '@app/core/models/api-response';
export * from '@app/core/core.functions';
export { PageClickEvent } from '@app/core/models/event/page-click-event';

Expand Down
22 changes: 0 additions & 22 deletions comixed-web/src/app/core/models/api-response.ts

This file was deleted.

0 comments on commit 437d148

Please sign in to comment.