Skip to content

Commit

Permalink
fix: test error
Browse files Browse the repository at this point in the history
  • Loading branch information
prince-deriv committed Oct 21, 2024
1 parent 727e350 commit ea4eb83
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/utils/src/__tests__/getCountry.spec.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import getCountry from '../getCountry';

describe('getCountry', () => {
let fetchSpy: jest.SpyInstance;

beforeEach(() => {
fetchSpy = jest.spyOn(global, 'fetch');
global.fetch = jest.fn() as jest.Mock;
});

afterEach(() => {
fetchSpy.mockRestore();
jest.resetAllMocks();
});

it('should return the country code in lowercase when available', async () => {
fetchSpy.mockResolvedValue({
// Mock fetch response
(global.fetch as jest.Mock).mockResolvedValue({
text: async () => 'loc=US\nother=info\n',
} as Response);
});

const country = await getCountry();
expect(country).toBe('us');
});

it('should return an empty string if the loc field is not present', async () => {
fetchSpy.mockResolvedValue({
(global.fetch as jest.Mock).mockResolvedValue({
text: async () => 'other=info\n',
} as Response);
});

const country = await getCountry();
expect(country).toBe('');
});

it('should return an empty string if the fetch fails', async () => {
fetchSpy.mockRejectedValue(new Error('Fetch failed'));
(global.fetch as jest.Mock).mockRejectedValue(new Error('Fetch failed'));

const country = await getCountry();
expect(country).toBe('');
Expand Down

0 comments on commit ea4eb83

Please sign in to comment.