diff --git a/libs/ngx-cookieconsent/src/lib/service/window.service.spec.ts b/libs/ngx-cookieconsent/src/lib/service/window.service.spec.ts index 23a6248..79d79d8 100644 --- a/libs/ngx-cookieconsent/src/lib/service/window.service.spec.ts +++ b/libs/ngx-cookieconsent/src/lib/service/window.service.spec.ts @@ -1,12 +1,15 @@ /* tslint:disable:no-unused-variable */ -import { TestBed, async, inject } from '@angular/core/testing'; +import { DOCUMENT } from '@angular/common'; +import { TestBed, inject } from '@angular/core/testing'; import { WindowService } from './window.service'; +declare const document: Document + describe('Service: Window, Angular Tests', () => { beforeEach(() => { TestBed.configureTestingModule({ - providers: [WindowService] + providers: [{ provide: DOCUMENT, useValue: document}, WindowService] }); }); @@ -17,14 +20,3 @@ describe('Service: Window, Angular Tests', () => { })); }); -describe('Service: Window, Isolated Tests', () => { - - let service: WindowService; - - beforeEach(() => { service = new WindowService(); }); - - it('nativeWindow should be defined and equaled to "window" object', () => { - expect(service.nativeWindow).toBeDefined(); - expect(service.nativeWindow).toBe(window); - }); -}); diff --git a/libs/ngx-cookieconsent/src/lib/service/window.service.ts b/libs/ngx-cookieconsent/src/lib/service/window.service.ts index ecf83d1..1c55eb1 100644 --- a/libs/ngx-cookieconsent/src/lib/service/window.service.ts +++ b/libs/ngx-cookieconsent/src/lib/service/window.service.ts @@ -1,17 +1,16 @@ -import { Injectable } from '@angular/core'; +import { DOCUMENT } from '@angular/common'; +import { Inject, Injectable } from '@angular/core'; /** - * Service to interact with the window object. + * Service to interact with the `window` object (or its equivalent on a server platform). */ @Injectable() export class WindowService { + constructor(@Inject(DOCUMENT) private _doc: Document) {} + get nativeWindow(): any { - return _window(); + return this._doc?.defaultView || window; } } -function _window(): any { - // Return the global native browser window object - return typeof window !== 'undefined' ? window : undefined; -}