Skip to content

Commit

Permalink
fix: updated test files
Browse files Browse the repository at this point in the history
  • Loading branch information
shayan-deriv committed Nov 15, 2024
1 parent aba966d commit 0119cff
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/utils/__test__/country.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
import { describe, beforeEach, it, expect, vi, Mock } from 'vitest';
import { getCountry } from '../country.utils';
import Cookies from 'js-cookie';
import { describe, beforeEach, it, expect, vi, Mock } from "vitest";
import { getCountry } from "../country.utils";
import Cookies from "js-cookie";


vi.mock('js-cookie');
vi.mock("js-cookie");
global.fetch = vi.fn();

describe('getCountry', () => {
describe("getCountry", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.resetModules();
});

it('should return country code from Cloudflare trace', async () => {
it("should return country code from Cloudflare trace", async () => {
(global.fetch as Mock).mockResolvedValueOnce({
text: () => Promise.resolve('loc=US\nother=value'),
text: () => Promise.resolve("loc=US\nother=value"),
});

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

it('should return country code from cookie when Cloudflare fails', async () => {
it("should return country code from cookie when Cloudflare fails", async () => {
vi.clearAllMocks();
vi.resetModules();

(global.fetch as Mock).mockRejectedValueOnce(new Error('Network error'));
(global.fetch as Mock).mockRejectedValueOnce(new Error("Network error"));
// Mock cookie value as a JSON string
(Cookies.get as Mock).mockReturnValue(JSON.stringify({ clients_country: "fr" }));

const { getCountry } = await import('../country.utils');
const { getCountry } = await import("../country.utils");
const result = await getCountry();
expect(result).toBe('fr');
expect(result).toBe("fr");
});

it('should return empty string if no country data is available', async () => {
it("should return empty string if no country data is available", async () => {
vi.clearAllMocks();
vi.resetModules();
(global.fetch as Mock).mockRejectedValueOnce(new Error('Network error'));
(global.fetch as Mock).mockRejectedValueOnce(new Error("Network error"));
(Cookies.get as Mock).mockReturnValue(JSON.stringify({}));
const { getCountry } = await import('../country.utils');
const { getCountry } = await import("../country.utils");
const result = await getCountry();
expect(result).toBe('');
expect(result).toBe("");
});
});

0 comments on commit 0119cff

Please sign in to comment.