Skip to content

Commit 10e0308

Browse files
committed
test: add Vitest unit tests for location-detector and country-flags
1 parent f97a32f commit 10e0308

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, test } from "vitest";
2+
import { getCountryCode } from "@/lib/geo/country-flags";
3+
4+
describe("getCountryCode", () => {
5+
test.each([
6+
["egypt", "eg"],
7+
["germany", "de"],
8+
["united_states", "us"],
9+
["uk", "gb"],
10+
])("returns the ISO code for %s", (slug, expected) => {
11+
expect(getCountryCode(slug)).toBe(expected);
12+
});
13+
14+
test.each(["atlantis", "", "EGYPT", "worldwide"])("returns null for %j", (slug) => {
15+
expect(getCountryCode(slug)).toBeNull();
16+
});
17+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { describe, expect, test } from "vitest";
2+
import { detectCountry } from "@/lib/geo/location-detector";
3+
4+
describe("detectCountry", () => {
5+
describe("empty and invalid input", () => {
6+
test("returns null for null", () => {
7+
expect(detectCountry(null)).toBeNull();
8+
});
9+
10+
test.each(["", " ", " ", "\t\n"])("returns null for blank input %j", (input) => {
11+
expect(detectCountry(input)).toBeNull();
12+
});
13+
14+
test.each(["!!!", "###", "@#$%^&*()", "🌍", "(.*)", "[", "\\"])(
15+
"returns null for special characters only: %s",
16+
(input) => {
17+
expect(detectCountry(input)).toBeNull();
18+
},
19+
);
20+
21+
test.each(["Mars", "Earth", "Remote"])(
22+
"returns null when no country is present: %s",
23+
(input) => {
24+
expect(detectCountry(input)).toBeNull();
25+
},
26+
);
27+
});
28+
29+
describe("exact match", () => {
30+
test.each([
31+
["Egypt", "egypt"],
32+
["Germany", "germany"],
33+
["Tokyo, Japan", "japan"],
34+
["Jakarta, Indonesia", "indonesia"],
35+
["Cairo", "egypt"],
36+
["New Zealand", "new_zealand"],
37+
["South Korea", "south_korea"],
38+
["Hong Kong", "hong_kong"],
39+
])("detects %s as %s", (input, expected) => {
40+
expect(detectCountry(input)).toBe(expected);
41+
});
42+
43+
test("is case-insensitive", () => {
44+
expect(detectCountry("EGYPT")).toBe("egypt");
45+
});
46+
47+
test("ignores surrounding whitespace", () => {
48+
expect(detectCountry(" Egypt ")).toBe("egypt");
49+
});
50+
51+
test("matches next to punctuation", () => {
52+
expect(detectCountry("Cairo, Egypt!")).toBe("egypt");
53+
});
54+
});
55+
56+
describe("word boundaries", () => {
57+
test.each([
58+
["Romania", "romania"],
59+
["Somalia", "somalia"],
60+
["Nigeria", "nigeria"],
61+
["Tirana", "albania"],
62+
["Indiana", "united_states"],
63+
])("%s is not matched by the country hidden inside it", (input, expected) => {
64+
expect(detectCountry(input)).toBe(expected);
65+
});
66+
67+
test.each([
68+
["Oman", "oman"],
69+
["Mali", "mali"],
70+
["Niger", "niger"],
71+
["Iran", "iran"],
72+
])("still matches the standalone word %s", (input, expected) => {
73+
expect(detectCountry(input)).toBe(expected);
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)