Skip to content

Commit

Permalink
chore: resolved pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shayan-deriv committed Nov 15, 2024
1 parent 0f39e69 commit aba966d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
58 changes: 33 additions & 25 deletions src/utils/__test__/country.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
import { describe, beforeEach, afterEach, it, expect, vi } from "vitest";
import { getCountry } from "../country.utils";
import { describe, beforeEach, it, expect, vi, Mock } from 'vitest';
import { getCountry } from '../country.utils';
import Cookies from 'js-cookie';

describe("getCountry", () => {
beforeEach(() => {
global.fetch = vi.fn();
});

afterEach(() => {
vi.resetAllMocks();
vi.mock('js-cookie');
global.fetch = vi.fn();

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

it("should return the country code in lowercase when available", async () => {
// Mock fetch response
(global.fetch as vi.Mock).mockResolvedValue({
text: async () => "loc=US\nother=info\n",
it('should return country code from Cloudflare trace', async () => {
(global.fetch as Mock).mockResolvedValueOnce({
text: () => Promise.resolve('loc=US\nother=value'),
});

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

it("should return an empty string if the loc field is not present", async () => {
(global.fetch as vi.Mock).mockResolvedValue({
text: async () => "other=info\n",
});
it('should return country code from cookie when Cloudflare fails', async () => {
vi.clearAllMocks();
vi.resetModules();

const country = await getCountry();
expect(country).toBe("");
});
(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" }));

it("should return an empty string if the fetch fails", async () => {
(global.fetch as vi.Mock).mockRejectedValue(new Error("Fetch failed"));
const { getCountry } = await import('../country.utils');
const result = await getCountry();
expect(result).toBe('fr');
});

const country = await getCountry();
expect(country).toBe("");
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'));
(Cookies.get as Mock).mockReturnValue(JSON.stringify({}));
const { getCountry } = await import('../country.utils');
const result = await getCountry();
expect(result).toBe('');
});
});
8 changes: 3 additions & 5 deletions src/utils/country.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ export const getCountry = async (): Promise<string> => {
try {
const response = await fetch(cloudflareTrace).catch(() => null);
if (!response) {
return JSON.parse(Cookies.get("website_status") || "{}")?.loc?.toLowerCase() || "";
return JSON.parse(Cookies.get("website_status") || "{}")?.clients_country || "";
}

const text = await response.text().catch(() => "");
const entries = text ? text.split("\n").map((v) => v.split("=", 2)) : [];
const data: TraceData = entries.length ? Object.fromEntries(entries) : {};
return data.loc?.toLowerCase() || JSON.parse(Cookies.get("website_status") || "{}")?.loc || "";
return data.loc?.toLowerCase() || JSON.parse(Cookies.get("website_status") || "{}")?.clients_country || "";
} catch (error) {
return "";
} finally {
countryPromise = null;
return JSON.parse(Cookies.get("website_status") || "{}")?.clients_country?.toLowerCase() || "";
}
})();

Expand Down

0 comments on commit aba966d

Please sign in to comment.