18 Commits
8bce4dcd7c
...
747495262e
18 changed files with 1272 additions and 386 deletions
-
7src/app/questions-list/[slug]/question-detail-client.tsx
-
60src/components/Componentes/error-toast.tsx
-
39src/components/Componentes/question-answer-storage.tsx
-
93src/components/Componentes/question-birthplace.test.ts
-
345src/components/Componentes/question-birthplace.tsx
-
16src/components/Componentes/question-number.tsx
-
169src/components/Componentes/question-phone.test.tsx
-
47src/components/Componentes/question-section-flow.tsx
-
76src/components/Componentes/question-sheet.tsx
-
4src/components/Componentes/slider-page.test.tsx
-
29src/components/Componentes/slider-page.tsx
-
241src/components/Componentes/ui-config.test.tsx
-
22src/data/countries.ts
-
6src/hooks/marriage/types.ts
-
149src/lib/geo-region.test.ts
-
332src/lib/geo-region.ts
-
19src/lib/marriage-field-formatter.ts
-
4src/types/window.d.ts
@ -0,0 +1,93 @@ |
|||
import { describe, expect, it } from "vitest"; |
|||
import { parseValue } from "./question-birthplace"; |
|||
|
|||
describe("QuestionBirthplace parseValue", () => { |
|||
it("parses empty and null values safely", () => { |
|||
expect(parseValue(null)).toEqual({ country: "", city: "" }); |
|||
expect(parseValue(undefined)).toEqual({ country: "", city: "" }); |
|||
expect(parseValue("")).toEqual({ country: "", city: "" }); |
|||
}); |
|||
|
|||
it("parses structured objects", () => { |
|||
expect(parseValue({ country: "Iran", city: "Tehran" })).toEqual({ |
|||
country: "Iran", |
|||
city: "Tehran", |
|||
}); |
|||
expect(parseValue({ country: "Mashhad", city: "Iran" })).toEqual({ |
|||
country: "Iran", |
|||
city: "Mashhad", |
|||
}); |
|||
expect(parseValue({ country: "مشهد", city: "ایران" })).toEqual({ |
|||
country: "ایران", |
|||
city: "مشهد", |
|||
}); |
|||
}); |
|||
|
|||
it("parses standard 'Country, City' strings", () => { |
|||
expect(parseValue("Iran, Tehran")).toEqual({ |
|||
country: "Iran", |
|||
city: "Tehran", |
|||
}); |
|||
expect(parseValue("ایران, شیراز")).toEqual({ |
|||
country: "ایران", |
|||
city: "شیراز", |
|||
}); |
|||
}); |
|||
|
|||
it("never swaps country and city even when city equals country name", () => { |
|||
// Country selected as 'Afghanistan' and city typed as 'albania'
|
|||
expect(parseValue("Afghanistan, albania")).toEqual({ |
|||
country: "Afghanistan", |
|||
city: "albania", |
|||
}); |
|||
|
|||
// Country selected as 'Albania' and city typed as 'Albania'
|
|||
expect(parseValue("Albania, Albania")).toEqual({ |
|||
country: "Albania", |
|||
city: "Albania", |
|||
}); |
|||
|
|||
// Country selected as 'United States (US)' and city typed as 'Georgia'
|
|||
expect(parseValue("United States (US), Georgia")).toEqual({ |
|||
country: "United States (US)", |
|||
city: "Georgia", |
|||
}); |
|||
}); |
|||
|
|||
it("correctly identifies country and city when formatted as 'City, Country'", () => { |
|||
expect(parseValue("Mashhad, Iran")).toEqual({ |
|||
country: "Iran", |
|||
city: "Mashhad", |
|||
}); |
|||
expect(parseValue("مشهد، ایران")).toEqual({ |
|||
country: "ایران", |
|||
city: "مشهد", |
|||
}); |
|||
expect(parseValue("Tehran, IR")).toEqual({ |
|||
country: "IR", |
|||
city: "Tehran", |
|||
}); |
|||
}); |
|||
|
|||
it("parses single country string", () => { |
|||
expect(parseValue("Iran")).toEqual({ |
|||
country: "Iran", |
|||
city: "", |
|||
}); |
|||
expect(parseValue("ایران")).toEqual({ |
|||
country: "ایران", |
|||
city: "", |
|||
}); |
|||
expect(parseValue("Afghanistan")).toEqual({ |
|||
country: "Afghanistan", |
|||
city: "", |
|||
}); |
|||
}); |
|||
|
|||
it("parses single custom city string without matching country", () => { |
|||
expect(parseValue("Rey")).toEqual({ |
|||
country: "", |
|||
city: "Rey", |
|||
}); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,149 @@ |
|||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
|||
import { |
|||
getUserGeoRegion, |
|||
getStoredUserGeoRegion, |
|||
setStoredUserGeoRegion, |
|||
resetUserGeoRegionForTesting, |
|||
} from "./geo-region"; |
|||
|
|||
describe("geo-region", () => { |
|||
const originalHabibApp = window.HabibApp; |
|||
const originalAddFlutterResponseListener = window.addFlutterResponseListener; |
|||
|
|||
beforeEach(() => { |
|||
vi.restoreAllMocks(); |
|||
localStorage.clear(); |
|||
resetUserGeoRegionForTesting(); |
|||
}); |
|||
|
|||
afterEach(() => { |
|||
window.HabibApp = originalHabibApp; |
|||
window.addFlutterResponseListener = originalAddFlutterResponseListener; |
|||
resetUserGeoRegionForTesting(); |
|||
}); |
|||
|
|||
describe("getStoredUserGeoRegion / setStoredUserGeoRegion", () => { |
|||
it("returns null when nothing is stored", () => { |
|||
expect(getStoredUserGeoRegion()).toBeNull(); |
|||
}); |
|||
|
|||
it("persists region and retrieves from memory and localStorage", () => { |
|||
const sample = { |
|||
ip: "1.2.3.4", |
|||
city: "Tehran", |
|||
country: "Iran", |
|||
countryCode: "IR", |
|||
phoneCode: "+98", |
|||
}; |
|||
setStoredUserGeoRegion(sample); |
|||
expect(getStoredUserGeoRegion()).toEqual(sample); |
|||
expect(localStorage.getItem("user_geo_region")).toBe( |
|||
JSON.stringify(sample), |
|||
); |
|||
expect(localStorage.getItem("geoIPPhoneCode")).toBe("+98"); |
|||
}); |
|||
}); |
|||
|
|||
describe("getUserGeoRegion", () => { |
|||
it("returns stored region immediately if not forced", async () => { |
|||
const cached = { |
|||
city: "London", |
|||
country: "United Kingdom", |
|||
countryCode: "GB", |
|||
phoneCode: "+44", |
|||
}; |
|||
setStoredUserGeoRegion(cached); |
|||
|
|||
const result = await getUserGeoRegion(false); |
|||
expect(result).toEqual(cached); |
|||
}); |
|||
|
|||
it("fetches via Flutter bridge when in Flutter WebView", async () => { |
|||
let listenerCallback: ((event: any) => void) | undefined; |
|||
window.addFlutterResponseListener = vi.fn().mockImplementation((cb) => { |
|||
listenerCallback = cb; |
|||
return () => {}; |
|||
}); |
|||
|
|||
const mockPostMessage = vi.fn().mockImplementation(() => { |
|||
setTimeout(() => { |
|||
listenerCallback?.({ |
|||
action: "get_location", |
|||
success: true, |
|||
data: { |
|||
ip: "5.6.7.8", |
|||
city: "Isfahan", |
|||
country: "Iran", |
|||
country_code: "IR", |
|||
}, |
|||
}); |
|||
}, 10); |
|||
}); |
|||
|
|||
window.HabibApp = { postMessage: mockPostMessage }; |
|||
|
|||
const result = await getUserGeoRegion(true); |
|||
|
|||
expect(mockPostMessage).toHaveBeenCalledWith( |
|||
JSON.stringify({ action: "get_location" }), |
|||
); |
|||
expect(result).toEqual({ |
|||
ip: "5.6.7.8", |
|||
city: "Isfahan", |
|||
country: "Iran", |
|||
countryCode: "IR", |
|||
phoneCode: "+98", |
|||
}); |
|||
expect(getStoredUserGeoRegion()?.countryCode).toBe("IR"); |
|||
}); |
|||
|
|||
it("resolves from fallback storage without making any HTTP calls when outside Flutter", async () => { |
|||
delete (window as any).HabibApp; |
|||
|
|||
const cached = { |
|||
city: "Shiraz", |
|||
country: "Iran", |
|||
countryCode: "IR", |
|||
phoneCode: "+98", |
|||
}; |
|||
setStoredUserGeoRegion(cached); |
|||
|
|||
const result = await getUserGeoRegion(false); |
|||
expect(result).toEqual(cached); |
|||
}); |
|||
|
|||
it("falls back to stored region if Flutter bridge returns failure", async () => { |
|||
const existing = { |
|||
ip: "192.168.1.1", |
|||
city: "Mashhad", |
|||
country: "Iran", |
|||
countryCode: "IR", |
|||
phoneCode: "+98", |
|||
}; |
|||
setStoredUserGeoRegion(existing); |
|||
|
|||
let listenerCallback: ((event: any) => void) | undefined; |
|||
window.addFlutterResponseListener = vi.fn().mockImplementation((cb) => { |
|||
listenerCallback = cb; |
|||
return () => {}; |
|||
}); |
|||
|
|||
const mockPostMessage = vi.fn().mockImplementation(() => { |
|||
setTimeout(() => { |
|||
listenerCallback?.({ |
|||
action: "get_location", |
|||
success: false, |
|||
}); |
|||
}, 10); |
|||
}); |
|||
|
|||
window.HabibApp = { postMessage: mockPostMessage }; |
|||
|
|||
const result = await getUserGeoRegion(true); |
|||
expect(result.city).toBe("Mashhad"); |
|||
expect(result.countryCode).toBe("IR"); |
|||
expect(result.country).toBe("Iran"); |
|||
expect(result.phoneCode).toBe("+98"); |
|||
}); |
|||
}); |
|||
}); |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue