Browse Source

test(theme-shared): refactor error handler spec

pull/1826/head
mehmet-erim 7 years ago
parent
commit
3f816b25bd
  1. 1
      npm/ng-packs/package.json
  2. 0
      npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.scss
  3. 2
      npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts
  4. 2
      npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts
  5. 81
      npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts
  6. 2
      npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts

1
npm/ng-packs/package.json

@ -48,6 +48,7 @@
"karma-jasmine-html-reporter": "^1.4.0",
"ng-packagr": "^5.5.0",
"ngx-perfect-scrollbar": "^8.0.0",
"ngxs-reset-plugin": "^1.2.0",
"primeicons": "^1.0.0",
"primeng": "^8.0.0",
"protractor": "~5.4.0",

0
npm/ng-packs/packages/theme-shared/src/lib/components/errors/error.component.scss → npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.scss

2
npm/ng-packs/packages/theme-shared/src/lib/components/errors/error.component.ts → npm/ng-packs/packages/theme-shared/src/lib/components/error/error.component.ts

@ -4,7 +4,7 @@ import { Component, Renderer2, ElementRef } from '@angular/core';
selector: 'abp-error',
template: `
<div class="error">
<button id="abp-close-button mr-2" type="button" class="close" (click)="destroy()">
<button id="abp-close-button mr-4" type="button" class="close" (click)="destroy()">
<span aria-hidden="true">&times;</span>
</button>
<div class="row centered">

2
npm/ng-packs/packages/theme-shared/src/lib/handlers/error.handler.ts

@ -11,7 +11,7 @@ import {
import { Navigate, RouterState } from '@ngxs/router-plugin';
import { Actions, ofActionSuccessful, Store } from '@ngxs/store';
import { Observable } from 'rxjs';
import { ErrorComponent } from '../components/errors/error.component';
import { ErrorComponent } from '../components/error/error.component';
import { Toaster } from '../models/toaster';
import { ConfirmationService } from '../services/confirmation.service';
import snq from 'snq';

81
npm/ng-packs/packages/theme-shared/src/lib/tests/error.handler.spec.ts

@ -1,9 +1,9 @@
import { CoreModule, RestOccurError, RouterOutletComponent } from '@abp/ng.core';
import { ConfigState, CoreModule, RestOccurError, RouterOutletComponent } from '@abp/ng.core';
import { Location } from '@angular/common';
import { HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';
import { createHostFactory, SpectatorHost } from '@ngneat/spectator';
import { createRoutingFactory, SpectatorRouting } from '@ngneat/spectator';
import { RouterState } from '@ngxs/router-plugin';
import { NgxsModule, Store } from '@ngxs/store';
import { NgxsResetPluginModule, StateOverwrite } from 'ngxs-reset-plugin';
@ -16,87 +16,84 @@ class DummyComponent {
}
describe('With Custom Host Component', function() {
let host: SpectatorHost<DummyComponent>;
const createHost = createHostFactory({
let component: SpectatorRouting<DummyComponent>;
const createComponent = createRoutingFactory({
component: DummyComponent,
imports: [
CoreModule,
ThemeSharedModule.forRoot(),
NgxsModule.forRoot([]),
NgxsResetPluginModule.forRoot(),
RouterModule.forRoot([{ path: 'account/login', component: RouterOutletComponent }]),
],
imports: [CoreModule, ThemeSharedModule.forRoot(), NgxsModule.forRoot([]), NgxsResetPluginModule.forRoot()],
stubsEnabled: false,
routes: [{ path: '', component: DummyComponent }, { path: 'account/login', component: RouterOutletComponent }],
});
beforeEach(() => {
host = createHost(`<dummy></dummy>`);
component = createComponent();
const abpError = document.querySelector('abp-error');
if (abpError) document.body.removeChild(abpError);
});
it('should display the error component when server error occurs', () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 500 })));
host.detectChanges();
component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 500 })));
component.detectChanges();
expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError500.title);
expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError500.details);
});
it('should display the error component when authorize error occurs', () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 403 })));
host.detectChanges();
component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 403 })));
component.detectChanges();
expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError403.title);
expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError403.details);
});
it('should display the error component when unknown error occurs', () => {
host.component.store.dispatch(
component.component.store.dispatch(
new RestOccurError(new HttpErrorResponse({ status: 0, statusText: 'Unknown Error' })),
);
host.detectChanges();
component.detectChanges();
expect(document.querySelector('.error-template')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultErrorUnknown.title);
expect(document.querySelector('.error-details')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultErrorUnknown.details);
});
it('should display the confirmation when not found error occurs', () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 404 })));
host.detectChanges();
expect(host.query('.abp-confirm-summary')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError404.title);
expect(host.query('.abp-confirm-body')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError404.details);
component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 404 })));
component.detectChanges();
expect(component.query('.abp-confirm-summary')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError404.title);
expect(component.query('.abp-confirm-body')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError404.details);
});
it('should display the confirmation when default error occurs', () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 412 })));
host.detectChanges();
expect(host.query('.abp-confirm-summary')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError.title);
expect(host.query('.abp-confirm-body')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError.details);
component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 412 })));
component.detectChanges();
expect(component.query('.abp-confirm-summary')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError.title);
expect(component.query('.abp-confirm-body')).toHaveText(DEFAULT_ERROR_MESSAGES.defaultError.details);
});
it('should display the confirmation when authenticated error occurs', async () => {
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401 })));
host.detectChanges();
host.component.store.dispatch(new StateOverwrite([RouterState, { state: { url: '/' } }]));
host.click('#confirm');
await host.fixture.whenStable();
expect(host.get(Location).path()).toBe('/account/login');
component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401 })));
component.detectChanges();
component.component.store.dispatch(new StateOverwrite([RouterState, { state: { url: '/' } }]));
component.click('#confirm');
await component.fixture.whenStable();
expect(component.get(Location).path()).toBe('/account/login');
});
it('should display the confirmation when authenticated error occurs with _AbpErrorFormat header', async () => {
let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('_AbpErrorFormat', '_AbpErrorFormat');
host.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401, headers })));
host.detectChanges();
host.component.store.dispatch(new StateOverwrite([RouterState, { state: { url: '/' } }]));
host.click('#confirm');
await host.fixture.whenStable();
expect(host.get(Location).path()).toBe('/account/login');
component.component.store.dispatch(new RestOccurError(new HttpErrorResponse({ status: 401, headers })));
component.detectChanges();
component.component.store.dispatch(new StateOverwrite([RouterState, { state: { url: '/' } }]));
component.click('#confirm');
await component.fixture.whenStable();
expect(component.get(Location).path()).toBe('/account/login');
});
it('should display the confirmation when error occurs with _AbpErrorFormat header', () => {
let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('_AbpErrorFormat', '_AbpErrorFormat');
host.component.store.dispatch(
component.component.store.dispatch(
new RestOccurError(
new HttpErrorResponse({
error: { error: { message: 'test message', details: 'test detail' } },
@ -105,9 +102,9 @@ describe('With Custom Host Component', function() {
}),
),
);
host.detectChanges();
component.detectChanges();
expect(host.query('.abp-confirm-summary')).toHaveText('test message');
expect(host.query('.abp-confirm-body')).toHaveText('test detail');
expect(component.query('.abp-confirm-summary')).toHaveText('test message');
expect(component.query('.abp-confirm-body')).toHaveText('test detail');
});
});

2
npm/ng-packs/packages/theme-shared/src/lib/theme-shared.module.ts

@ -9,7 +9,7 @@ import { ButtonComponent } from './components/button/button.component';
import { ChangePasswordComponent } from './components/change-password/change-password.component';
import { ChartComponent } from './components/chart/chart.component';
import { ConfirmationComponent } from './components/confirmation/confirmation.component';
import { ErrorComponent } from './components/errors/error.component';
import { ErrorComponent } from './components/error/error.component';
import { LoaderBarComponent } from './components/loader-bar/loader-bar.component';
import { ModalComponent } from './components/modal/modal.component';
import { ProfileComponent } from './components/profile/profile.component';

Loading…
Cancel
Save