From 0c1946d5dac474466406172bf8acdd14a9a7fd6a Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Fri, 11 Oct 2019 11:13:51 +0300 Subject: [PATCH 1/2] test(core): add lazy-load-service.spec --- .../src/lib/services/lazy-load.service.ts | 6 +- .../src/lib/tests/lazy-load.service.spec.ts | 92 +++++++++++++++++++ 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 npm/ng-packs/packages/core/src/lib/tests/lazy-load.service.spec.ts diff --git a/npm/ng-packs/packages/core/src/lib/services/lazy-load.service.ts b/npm/ng-packs/packages/core/src/lib/services/lazy-load.service.ts index 0cdf546ff0..fa6ab37fa1 100644 --- a/npm/ng-packs/packages/core/src/lib/services/lazy-load.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/lazy-load.service.ts @@ -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 { if (!urlOrUrls && !content) { - return; + return throwError('Should pass url or content'); } else if (!urlOrUrls && content) { urlOrUrls = [null]; } diff --git a/npm/ng-packs/packages/core/src/lib/tests/lazy-load.service.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/lazy-load.service.spec.ts new file mode 100644 index 0000000000..8afb0745f6 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/lazy-load.service.spec.ts @@ -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; + 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(); + }); +}); From 9b733bffe36c7c96b5573464a402918327447ccb Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Fri, 11 Oct 2019 14:12:51 +0300 Subject: [PATCH 2/2] chore: upgrade angular versions --- templates/app/angular/package.json | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/templates/app/angular/package.json b/templates/app/angular/package.json index 6a8fb96d91..45d2c1592a 100644 --- a/templates/app/angular/package.json +++ b/templates/app/angular/package.json @@ -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",