Skip to content

Instantly share code, notes, and snippets.

@Eightyplus
Last active May 10, 2019 18:01
Show Gist options
  • Save Eightyplus/765c22e81801ca58a2fd0cea35237732 to your computer and use it in GitHub Desktop.
Save Eightyplus/765c22e81801ca58a2fd0cea35237732 to your computer and use it in GitHub Desktop.
Mock window.location in jest
describe('test reading window.location', () => {
beforeEach(() =>
window.history.replaceState(
{},
'Title',
'/?code=0123456789&other=yes'
)
);
it('should have code', () => {
expect(window.location.search).toEqual(expect.stringContaining('code='));
});
it('should read code', () => {
expect(window.location.search.replace(/.*code=([0-9]*).*/, '$1')).toBe('0123456789');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment