1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-12-23 01:29:41 +01:00

getCountry test

This commit is contained in:
Matthias Kretschmann 2019-04-28 00:13:55 +02:00
parent d1d6c4b1f0
commit 7f0e1ca39d
Signed by: m
GPG Key ID: 606EEEF3C479A91F

View File

@ -1,7 +1,5 @@
import { getCountry } from './getCountry'
const responseMock = 'loc=DE'
const mockFetch = data =>
jest.fn().mockImplementationOnce(() =>
Promise.resolve({
@ -13,15 +11,21 @@ const mockFetch = data =>
)
describe('getCountry', () => {
beforeEach(() => {
global.fetch = mockFetch(responseMock)
})
it('fetches and returns correct value', async () => {
global.fetch = mockFetch('loc=DE')
const country = await getCountry()
expect(global.fetch).toHaveBeenCalledTimes(1)
expect(global.fetch).toHaveBeenCalledWith('/cdn-cgi/trace?no-cache=1')
expect(country).toBe('DE')
})
it('returns nothing when XX country', async () => {
global.fetch = mockFetch('loc=XX')
const country = await getCountry()
expect(global.fetch).toHaveBeenCalledTimes(1)
expect(global.fetch).toHaveBeenCalledWith('/cdn-cgi/trace?no-cache=1')
expect(country).toBe(undefined)
})
})