Browse Source

- fix tests

pull/6798/head
muhammedaltug 5 years ago
parent
commit
cba06c2afb
  1. 19
      npm/ng-packs/packages/core/src/lib/tests/api.interceptor.spec.ts
  2. 7
      npm/ng-packs/packages/core/src/lib/tests/lazy-load.service.spec.ts
  3. 23
      npm/ng-packs/packages/theme-shared/src/lib/tests/loader-bar.component.spec.ts

19
npm/ng-packs/packages/core/src/lib/tests/api.interceptor.spec.ts

@ -1,31 +1,29 @@
import { HttpRequest } from '@angular/common/http';
import { SpyObject } from '@ngneat/spectator';
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import { Store } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc';
import { Subject, timer } from 'rxjs';
import { StartLoader, StopLoader } from '../actions';
import { ApiInterceptor } from '../interceptors';
import { SessionStateService } from '../services';
import { HttpWaitService, SessionStateService } from '../services';
describe('ApiInterceptor', () => {
let spectator: SpectatorService<ApiInterceptor>;
let interceptor: ApiInterceptor;
let store: SpyObject<Store>;
let oauthService: SpyObject<OAuthService>;
let sessionState: SpyObject<SessionStateService>;
let httpWaitService: SpyObject<HttpWaitService>;
const createService = createServiceFactory({
service: ApiInterceptor,
mocks: [OAuthService, Store, SessionStateService],
mocks: [OAuthService, SessionStateService],
});
beforeEach(() => {
spectator = createService();
interceptor = spectator.service;
store = spectator.inject(Store);
sessionState = spectator.inject(SessionStateService);
oauthService = spectator.inject(OAuthService);
httpWaitService = spectator.inject(HttpWaitService);
});
it('should add headers to http request', done => {
@ -52,8 +50,9 @@ describe('ApiInterceptor', () => {
handleRes$.complete();
});
it('should dispatch the loader', done => {
const spy = jest.spyOn(store, 'dispatch');
it('should call http wait services add request and delete request', done => {
const spyAddRequest = jest.spyOn(httpWaitService, 'addRequest');
const spyDeleteRequest = jest.spyOn(httpWaitService, 'deleteRequest');
const request = new HttpRequest('GET', 'https://abp.io');
const handleRes$ = new Subject();
@ -70,8 +69,8 @@ describe('ApiInterceptor', () => {
handleRes$.complete();
timer(0).subscribe(() => {
expect(spy.mock.calls[0][0] instanceof StartLoader).toBeTruthy();
expect(spy.mock.calls[1][0] instanceof StopLoader).toBeTruthy();
expect(spyAddRequest).toHaveBeenCalled();
expect(spyDeleteRequest).toHaveBeenCalled();
done();
});
});

7
npm/ng-packs/packages/core/src/lib/tests/lazy-load.service.spec.ts

@ -2,10 +2,12 @@ import { of, throwError } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { LazyLoadService } from '../services/lazy-load.service';
import { ScriptLoadingStrategy } from '../strategies';
import { ResourceWaitService } from '../services';
describe('LazyLoadService', () => {
describe('#load', () => {
const service = new LazyLoadService();
const resourceWaitService = new ResourceWaitService();
const service = new LazyLoadService(resourceWaitService);
const strategy = new ScriptLoadingStrategy('http://example.com/');
afterEach(() => {
@ -58,7 +60,8 @@ describe('LazyLoadService', () => {
});
describe('#remove', () => {
const service = new LazyLoadService();
const resourceWaitService = new ResourceWaitService();
const service = new LazyLoadService(resourceWaitService);
it('should remove an already lazy loaded element and return true', () => {
const script = document.createElement('script');

23
npm/ng-packs/packages/theme-shared/src/lib/tests/loader-bar.component.spec.ts

@ -1,16 +1,10 @@
import {
Router,
RouteReuseStrategy,
NavigationStart,
NavigationEnd,
NavigationError,
} from '@angular/router';
import { NavigationEnd, NavigationError, NavigationStart, Router } from '@angular/router';
import { createHostFactory, SpectatorHost, SpyObject } from '@ngneat/spectator/jest';
import { Actions, NgxsModule, Store } from '@ngxs/store';
import { Subject, Subscription, Observable, Subscriber, timer } from 'rxjs';
import { Subject, Subscription, timer } from 'rxjs';
import { LoaderBarComponent } from '../components/loader-bar/loader-bar.component';
import { StartLoader, StopLoader, SubscriptionService } from '@abp/ng.core';
import { SubscriptionService } from '@abp/ng.core';
import { HttpRequest } from '@angular/common/http';
import { HttpWaitService } from '../../../../core/src/lib/services';
describe('LoaderBarComponent', () => {
let spectator: SpectatorHost<LoaderBarComponent>;
@ -20,7 +14,6 @@ describe('LoaderBarComponent', () => {
const createHost = createHostFactory({
component: LoaderBarComponent,
mocks: [Router],
imports: [NgxsModule.forRoot()],
detectChanges: false,
providers: [SubscriptionService],
});
@ -41,7 +34,8 @@ describe('LoaderBarComponent', () => {
it('should increase the progressLevel', done => {
spectator.detectChanges();
spectator.inject(Store).dispatch(new StartLoader(new HttpRequest('GET', 'test')));
const httpWaitService = spectator.inject(HttpWaitService);
httpWaitService.addRequest(new HttpRequest('GET', 'test'));
spectator.detectChanges();
setTimeout(() => {
expect(spectator.component.progressLevel > 0).toBeTruthy();
@ -51,7 +45,8 @@ describe('LoaderBarComponent', () => {
test.skip('should be interval unsubscribed', done => {
spectator.detectChanges();
spectator.inject(Store).dispatch(new StartLoader(new HttpRequest('GET', 'test')));
const httpWaitService = spectator.inject(HttpWaitService);
httpWaitService.addRequest(new HttpRequest('GET', 'test'));
expect(spectator.component.interval.closed).toBe(false);
timer(400).subscribe(() => {
@ -80,7 +75,7 @@ describe('LoaderBarComponent', () => {
(router as any).events.next(new NavigationStart(1, 'test'));
expect(spectator.component.interval.closed).toBe(false);
spectator.inject(Store).dispatch(new StopLoader(new HttpRequest('GET', 'test')));
(router as any).events.next(new NavigationEnd(1, 'testend', 'testend'));
expect(spectator.component.progressLevel).toBe(100);
timer(2).subscribe(() => {

Loading…
Cancel
Save