0

in my Jest test, i would like to test window.URL.createObjectURL() without override the window object for this reason I am trying to inject the Window object on my test but it still doesn't work, how can i test the window.URL.createObjectURL() without override the window object, is it possible?

import { InjectionToken } from '@angular/core';
export const WINDOW = new InjectionToken<Window>('WINDOW', {
  providedIn: 'root',
  factory: () => window,
});
Gelso77
  • 1,763
  • 6
  • 30
  • 47
  • If you inject the window object (https://www.leonelngande.com/how-to-get-a-reference-to-the-window-object-in-an-angular-8-application/) into your component's constructor, and then use `this.window.URL.createObjectUrl()` and then in your test provide the real window object (same way), will that help? – AliF50 Apr 12 '21 at 17:25
  • i don't want to overrider the original window object, i need to mock it – Gelso77 Apr 12 '21 at 17:38
  • This shouldn't override the original window object, it will provide the original window object. The reason why I don't directly use the `window` object and inject it into the constructor is because if you ever do server side rendering, window doesn't exist there and it can cause issues. Check out the comment to this answer (https://stackoverflow.com/questions/42454611/how-can-i-use-a-window-object-in-angular-2). Also, injecting it in the constructor, might give you more control over the window object in the tests. – AliF50 Apr 12 '21 at 17:54

0 Answers0