star-chart-search-enhancer/tests/market-page-bridge.test.ts

130 lines
3.1 KiB
TypeScript

// @vitest-environment jsdom
// @vitest-environment-options {"url":"https://www.xingtu.cn/ad/creator/market"}
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
describe("market-page-bridge", () => {
beforeEach(() => {
document.body.innerHTML = '<div class="base-author-list"></div>';
document.documentElement.removeAttribute("data-sces-market-request-snapshot");
vi.spyOn(window, "setInterval").mockReturnValue(0 as unknown as number);
});
afterEach(() => {
delete (
window as Window & {
__SCES_MARKET_PAGE_BRIDGE_INSTALLED__?: boolean;
}
).__SCES_MARKET_PAGE_BRIDGE_INSTALLED__;
vi.restoreAllMocks();
vi.resetModules();
});
test("ignores non-search market list responses when capturing the export snapshot", async () => {
const originalFetch = vi.fn(async () => ({
clone() {
return this;
},
json: async () => ({
authors: [
{
attribute_datas: {
nickname: "推荐达人"
},
star_id: "recommend-1"
}
],
pagination: {
limit: 20,
page: 1,
total_count: 20
}
})
}));
(
window as Window & {
fetch: typeof fetch;
}
).fetch = originalFetch as unknown as typeof fetch;
await import("../src/content/market/page-bridge");
await window.fetch("/gw/api/gauthor/demander_get_recommend_author_lists_v2", {
headers: {
Accept: "application/json, text/plain, */*"
},
method: "GET"
});
await Promise.resolve();
await Promise.resolve();
expect(
document.documentElement.getAttribute("data-sces-market-request-snapshot")
).toBeNull();
});
test("captures the search_for_author_square request for silent export", async () => {
const originalFetch = vi.fn(async () => ({
clone() {
return this;
},
json: async () => ({
authors: [
{
attribute_datas: {
nickname: "搜索达人"
},
star_id: "search-1"
}
],
pagination: {
limit: 20,
page: 1,
total_count: 20
}
})
}));
(
window as Window & {
fetch: typeof fetch;
}
).fetch = originalFetch as unknown as typeof fetch;
await import("../src/content/market/page-bridge");
await window.fetch("/gw/api/gsearch/search_for_author_square", {
body: JSON.stringify({
page_param: {
page: "1"
}
}),
headers: {
"Content-Type": "application/json"
},
method: "POST"
});
await Promise.resolve();
await Promise.resolve();
expect(
JSON.parse(
document.documentElement.getAttribute(
"data-sces-market-request-snapshot"
) ?? "null"
)
).toEqual(
expect.objectContaining({
body: JSON.stringify({
page_param: {
page: "1"
}
}),
method: "POST",
url: "/gw/api/gsearch/search_for_author_square"
})
);
});
});