From 0119cff4aa0684b4ce4941bbe8260480c9784fe6 Mon Sep 17 00:00:00 2001 From: shayan khaleghparast Date: Fri, 15 Nov 2024 18:35:55 +0800 Subject: [PATCH] fix: updated test files --- src/utils/__test__/country.utils.spec.ts | 33 ++++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/utils/__test__/country.utils.spec.ts b/src/utils/__test__/country.utils.spec.ts index 5027ccc..0ce2b01 100644 --- a/src/utils/__test__/country.utils.spec.ts +++ b/src/utils/__test__/country.utils.spec.ts @@ -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(""); }); });