Last active
July 13, 2018 11:48
-
-
Save jeffwhelpley/2d14a615790af18b3549 to your computer and use it in GitHub Desktop.
Using window object in Angular 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// interface for Window | |
interface Window { | |
// add some stuff here | |
} | |
let someMockWindowObject = { | |
// add some mock functionality here | |
}; | |
// for browser providers | |
provide(Window, { useValue: window }); | |
// for node and testing | |
provide(Window, { useValue: someMockWindowObject }) | |
// inject it | |
class Foo { | |
constructor(private window: Window) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For reference and unit testing, here is what worked for me:
In my service (can be done in a component too):
In my spec (Jasmine testing):
And another test: