95 lines
2.5 KiB
JavaScript
95 lines
2.5 KiB
JavaScript
import { expect, test } from "bun:test";
|
|
|
|
import {
|
|
createMobileFacilitiesLoader,
|
|
nextMobileRegionExpansion,
|
|
shouldLoadMobileFacilities,
|
|
} from "./compute-topic-mobile-data.js";
|
|
|
|
const response = (payload) => ({
|
|
ok: true,
|
|
async json() {
|
|
return payload;
|
|
},
|
|
});
|
|
|
|
test("mobile overview requests facility summaries while map cards are disabled", async () => {
|
|
const urls = [];
|
|
const loader = createMobileFacilitiesLoader({
|
|
fetchImpl: async (url) => {
|
|
urls.push(url);
|
|
return response({ total_count: 12 });
|
|
},
|
|
});
|
|
expect(shouldLoadMobileFacilities({
|
|
isMobile: true,
|
|
mobileActive: true,
|
|
cardsEnabled: false,
|
|
})).toBe(true);
|
|
|
|
let received = null;
|
|
await loader.load({
|
|
countries: ["CN", "US"],
|
|
year: "2023",
|
|
onSuccess(payload) {
|
|
received = payload;
|
|
},
|
|
});
|
|
|
|
expect(urls).toHaveLength(1);
|
|
expect(urls[0]).toContain("countries=CN%2CUS");
|
|
expect(urls[0]).toContain("year=2023");
|
|
expect(received.total_count).toBe(12);
|
|
});
|
|
|
|
test("a stale facility response cannot overwrite a newer participant selection", async () => {
|
|
const requests = [];
|
|
const loader = createMobileFacilitiesLoader({
|
|
fetchImpl: (url) => new Promise((resolve) => requests.push({ resolve, url })),
|
|
});
|
|
const received = [];
|
|
const first = loader.load({
|
|
countries: ["EUROPE"],
|
|
year: "",
|
|
onSuccess: (payload) => received.push(payload.code),
|
|
});
|
|
const second = loader.load({
|
|
countries: ["APAC"],
|
|
year: "2022",
|
|
onSuccess: (payload) => received.push(payload.code),
|
|
});
|
|
|
|
requests[1].resolve(response({ code: "APAC" }));
|
|
await second;
|
|
requests[0].resolve(response({ code: "EUROPE" }));
|
|
await first;
|
|
|
|
expect(received).toEqual(["APAC"]);
|
|
expect(requests[1].url).toContain("countries=APAC");
|
|
expect(requests[1].url).toContain("year=2022");
|
|
});
|
|
|
|
test("desktop, inactive mobile tabs, and enabled map cards skip the direct loader", () => {
|
|
expect(shouldLoadMobileFacilities({
|
|
isMobile: false,
|
|
mobileActive: true,
|
|
cardsEnabled: false,
|
|
})).toBe(false);
|
|
expect(shouldLoadMobileFacilities({
|
|
isMobile: true,
|
|
mobileActive: false,
|
|
cardsEnabled: false,
|
|
})).toBe(false);
|
|
expect(shouldLoadMobileFacilities({
|
|
isMobile: true,
|
|
mobileActive: true,
|
|
cardsEnabled: true,
|
|
})).toBe(false);
|
|
});
|
|
|
|
test("mobile region accordion keeps at most one expanded card", () => {
|
|
expect(nextMobileRegionExpansion(null, "CN")).toBe("CN");
|
|
expect(nextMobileRegionExpansion("CN", "US")).toBe("US");
|
|
expect(nextMobileRegionExpansion("US", "US")).toBeNull();
|
|
});
|