-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aba966d
commit 0119cff
Showing
1 changed file
with
16 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); | ||
}); | ||
}); |