Add country-specific companion table filtering for map page
This commit is contained in:
34
frontend/src/api/map.test.ts
Normal file
34
frontend/src/api/map.test.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { Mock } from "vitest";
|
||||
import { ENDPOINTS } from "./endpoints";
|
||||
import { fetchBansByCountry } from "./map";
|
||||
import { get } from "./client";
|
||||
|
||||
vi.mock("./client", () => ({
|
||||
get: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockedGet = get as Mock;
|
||||
|
||||
describe("fetchBansByCountry", () => {
|
||||
beforeEach(() => {
|
||||
mockedGet.mockReset();
|
||||
mockedGet.mockResolvedValue({ countries: {}, country_names: {}, bans: [], total: 0 });
|
||||
});
|
||||
|
||||
it("appends country_code when provided", async () => {
|
||||
await fetchBansByCountry("24h", "all", "fail2ban", "US");
|
||||
|
||||
expect(get).toHaveBeenCalledWith(
|
||||
`${ENDPOINTS.dashboardBansByCountry}?range=24h&country_code=US`
|
||||
);
|
||||
});
|
||||
|
||||
it("does not append country_code when undefined", async () => {
|
||||
await fetchBansByCountry("24h", "all", "fail2ban");
|
||||
|
||||
expect(get).toHaveBeenCalledWith(
|
||||
`${ENDPOINTS.dashboardBansByCountry}?range=24h`
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -18,6 +18,7 @@ export async function fetchBansByCountry(
|
||||
range: TimeRange = "24h",
|
||||
origin: BanOriginFilter = "all",
|
||||
source: "fail2ban" | "archive" = "fail2ban",
|
||||
countryCode?: string,
|
||||
): Promise<BansByCountryResponse> {
|
||||
const params = new URLSearchParams({ range });
|
||||
if (origin !== "all") {
|
||||
@@ -26,5 +27,8 @@ export async function fetchBansByCountry(
|
||||
if (source !== "fail2ban") {
|
||||
params.set("source", source);
|
||||
}
|
||||
if (countryCode) {
|
||||
params.set("country_code", countryCode);
|
||||
}
|
||||
return get<BansByCountryResponse>(`${ENDPOINTS.dashboardBansByCountry}?${params.toString()}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user