pull/1904/head
Halil İbrahim Kalkan 5 years ago
commit 1c0bd87845

@ -1,5 +1,5 @@
import { Inject, Injectable } from '@angular/core';
import { Observable, ReplaySubject } from 'rxjs';
import { Injectable } from '@angular/core';
import { Observable, ReplaySubject, throwError } from 'rxjs';
import { uuid } from '../utils';
@Injectable({
@ -16,7 +16,7 @@ export class LazyLoadService {
position: InsertPosition = 'afterend',
): Observable<void> {
if (!urlOrUrls && !content) {
return;
return throwError('Should pass url or content');
} else if (!urlOrUrls && content) {
urlOrUrls = [null];
}

@ -0,0 +1,92 @@
import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest';
import clone from 'just-clone';
import { LazyLoadService } from '../services/lazy-load.service';
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
describe('LazyLoadService', () => {
let spectator: SpectatorService<LazyLoadService>;
let service: LazyLoadService;
const scriptElement = document.createElement('script');
const linkElement = document.createElement('link');
const styleElement = document.createElement('style');
const cloneDocument = clone(document);
const createService = createServiceFactory({ service: LazyLoadService });
beforeEach(() => {
spectator = createService();
service = spectator.service;
});
afterEach(() => (document = clone(cloneDocument)));
test('should load script with content just one time', done => {
const spy = jest.spyOn(document, 'createElement');
spy.mockReturnValue(scriptElement);
service.load('https://abp.io', 'script', 'test').subscribe(res => {
expect(document.querySelector('script[src="https://abp.io"][type="text/javascript"]').textContent).toMatch(
'test',
);
});
scriptElement.onload(null);
service.load('https://abp.io', 'script', 'test').subscribe(res => {
expect(document.querySelectorAll('script[src="https://abp.io"][type="text/javascript"]')).toHaveLength(1);
done();
});
});
test('should load style element', done => {
const spy = jest.spyOn(document, 'createElement');
spy.mockReturnValue(styleElement);
const content = '* { color: black; }';
service.load(null, 'style', content).subscribe(res => {
expect(document.querySelector('style').textContent).toMatch(content);
done();
});
styleElement.onload(null);
});
describe('style with url', () => {
beforeEach(() => {
const spy = jest.spyOn(document, 'createElement');
spy.mockReturnValue(linkElement);
});
test('should load an link element', done => {
service.load('https://abp.io', 'style').subscribe(res => {
expect(document.querySelector('link[type="text/css"][rel="stylesheet"][href="https://abp.io"]')).toBeTruthy();
done();
});
linkElement.onload(null);
});
test('should load link elements', done => {
service.load(['https://abp.io', 'https://volosoft.com'], 'style').subscribe(res => {
expect(document.querySelector('link[href="https://volosoft.com"]')).toBeTruthy();
done();
});
linkElement.onload(null);
});
});
test('should throw error when required parameters are null', done => {
service
.load(null, 'style')
.pipe(
catchError(err => {
expect(err).toBeTruthy();
done();
return of(null);
}),
)
.subscribe();
});
});

@ -6,7 +6,7 @@
"start": "ng serve",
"start:hmr": "ng serve --configuration hmr",
"build": "ng build",
"build:prod": "ng build --configuration production",
"build:prod": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
@ -18,14 +18,14 @@
"@abp/ng.theme.basic": "^0.9.0",
"@abp/ng.tenant-management": "^0.9.0",
"@abp/ng.setting-management": "^0.9.0",
"@angular/animations": "~8.2.8",
"@angular/common": "~8.2.8",
"@angular/compiler": "~8.2.8",
"@angular/core": "~8.2.8",
"@angular/forms": "~8.2.8",
"@angular/platform-browser": "~8.2.8",
"@angular/platform-browser-dynamic": "~8.2.8",
"@angular/router": "~8.2.8",
"@angular/animations": "~8.2.10",
"@angular/common": "~8.2.10",
"@angular/compiler": "~8.2.10",
"@angular/core": "~8.2.10",
"@angular/forms": "~8.2.10",
"@angular/platform-browser": "~8.2.10",
"@angular/platform-browser-dynamic": "~8.2.10",
"@angular/router": "~8.2.10",
"@angularclass/hmr": "^2.1.3",
"@ngxs/hmr-plugin": "^3.5.0",
"@ngxs/devtools-plugin": "^3.5.0",
@ -34,10 +34,10 @@
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.6",
"@angular/cli": "~8.3.6",
"@angular/compiler-cli": "~8.2.8",
"@angular/language-service": "~8.2.8",
"@angular-devkit/build-angular": "~0.803.9",
"@angular/cli": "~8.3.9",
"@angular/compiler-cli": "~8.2.10",
"@angular/language-service": "~8.2.10",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",

Loading…
Cancel
Save