|
| 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