title: How to use window object in angular 7
body: <p>I want to use scrollTo() function of window object. Directly by accessing window.scrollTo(0,0), i can achieve this functionality. When i googled about using window in angular, people are creating a provider like below one:</p>
  
  <pre><code>import {InjectionToken, FactoryProvider} from '@angular/core';
  
  export const WINDOW = new InjectionToken&lt;Window&gt;('window');
  
  const windowProvider: FactoryProvider = {
    provide: WINDOW,
    useFactory: () =&gt; window,
  };
  
  export const WINDOW_PROVIDERS = [windowProvider];
  </code></pre>
  
  <p>Inside service using like the below code:</p>
  
  <pre><code>import {Injectable, Inject} from '@angular/core';
  import {WINDOW} from './window.provider';
  
  @Injectable({
    providedIn: 'root',
  })
  export class WindowService {
    constructor(@Inject(WINDOW) public window: Window) {}
  }
  
  </code></pre>
  
  <p>In module </p>
  
  <pre><code>    {provide: WINDOW_PROVIDERS, useValue: window},
  </code></pre>
  
  <p>Everything works with the above code, but while running test cases i m getting the below error. Almost half of the application test cases are failing with below error</p>
  
  <pre><code>NullInjectorError: No provider for InjectionToken window!
  
  </code></pre>
  
  <p>Default Test case for window.service</p>
  
  <pre><code>import { TestBed } from '@angular/core/testing';
  
  import { WindowService } from './window.service';
  
  describe('WindowService', () =&gt; {
    beforeEach(() =&gt; TestBed.configureTestingModule({}));
  
    it('should be created', () =&gt; {
      const service: WindowService = TestBed.get(WindowService);
      expect(service).toBeTruthy();
    });
  });
  
  </code></pre>
  
  <p>How to fix this?? </p>