-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathget-country.spec.ts
More file actions
36 lines (28 loc) · 1.02 KB
/
get-country.spec.ts
File metadata and controls
36 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import getCountry from '../get-country';
describe('getCountry', () => {
beforeEach(() => {
global.fetch = jest.fn() as jest.Mock;
});
afterEach(() => {
jest.resetAllMocks();
});
it('should return the country code in lowercase when available', async () => {
(global.fetch as jest.Mock).mockResolvedValue({
text: async () => 'loc=US\nother=info\n',
});
const country = await getCountry();
expect(country).toBe('us');
});
it('should return an empty string if the loc field is not present', async () => {
(global.fetch as jest.Mock).mockResolvedValue({
text: async () => 'other=info\n',
});
const country = await getCountry();
expect(country).toBe('');
});
it('should return an empty string if the fetch fails', async () => {
(global.fetch as jest.Mock).mockRejectedValue(new Error('Fetch failed'));
const country = await getCountry();
expect(country).toBe('');
});
});