Skip to content

Instantly share code, notes, and snippets.

@maatthc
Created August 30, 2021 04:05
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save maatthc/b7a1a00c80e9d3adf0b7e89484e71a17 to your computer and use it in GitHub Desktop.
Save maatthc/b7a1a00c80e9d3adf0b7e89484e71a17 to your computer and use it in GitHub Desktop.
TypeScript Jest Window Location mocking
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