Last active
October 25, 2024 13:57
-
-
Save mayank23/7b994385eb030f1efb7075c4f1f6ac4c to your computer and use it in GitHub Desktop.
Revisions
-
mayank23 revised this gist
Apr 24, 2019 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ Jest Mock Any Property on Window Utility - with automatic cleanup. -
mayank23 revised this gist
Apr 24, 2019 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,9 +7,9 @@ export default const mockWindowProperty = (property, value) => { writable: true, value, }); }); afterAll(() => { window[property] = originalProperty; }); }; -
mayank23 revised this gist
Apr 24, 2019 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,3 @@ export default const mockWindowProperty = (property, value) => { const { [property]: originalProperty } = window; delete window[property]; -
mayank23 created this gist
Apr 24, 2019 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ Here's a utility function for mocking any window property with automatic cleanup: export default const mockWindowProperty = (property, value) => { const { [property]: originalProperty } = window; delete window[property]; beforeAll(() => { Object.defineProperty(window, property, { configurable: true, writable: true, value, }); afterAll(() => { window[property] = originalProperty; }); }); }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ import mockWindowProperty from './mock-window-property' describe('my test', () => { mockWindowProperty('localStorage', { setItem: jest.fn(), getItem: jest.fn(), removeItem: jest.fn(), }); it('works as expected', () => { window.localStorage.setItem('abc'); expect(window.localStorage.setItem).toHaveBeenCalledWith('abc'); }); });