Created
August 30, 2021 04:05
-
-
Save maatthc/b7a1a00c80e9d3adf0b7e89484e71a17 to your computer and use it in GitHub Desktop.
TypeScript Jest Window Location mocking
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 characters
import urlBuilder from './url' | |
let location: Location | |
describe('urlBuilder', () => { | |
beforeEach(() => { | |
location = window.location | |
jest.spyOn(window, 'location', 'get').mockRestore() | |
}) | |
test('given path, should return valid url', () => { | |
const mockedLocation = { | |
...location, | |
protocol: 'https:', | |
host: 'www.google.com.au' | |
} | |
jest.spyOn(window, 'location', 'get').mockReturnValue(mockedLocation) | |
const expectedUrl = 'https://www.google.com.br/faq' | |
const result = urlBuilder('/static/faq') | |
expect(result).toBe(expectedUrl) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment