76 lines
2.4 KiB
TypeScript
76 lines
2.4 KiB
TypeScript
import { afterEach, describe, expect, test } from "vitest";
|
|
import { getActivityConfig } from "../app/services/config.server";
|
|
|
|
const originalType = process.env.METAOBJECT_TYPE;
|
|
const originalHandle = process.env.METAOBJECT_HANDLE;
|
|
|
|
afterEach(() => {
|
|
process.env.METAOBJECT_TYPE = originalType;
|
|
process.env.METAOBJECT_HANDLE = originalHandle;
|
|
});
|
|
|
|
function metaobjectResponse(fields: Array<{ key: string; value: string }>) {
|
|
return {
|
|
json: async () => ({
|
|
data: {
|
|
metaobjectByHandle: {
|
|
fields,
|
|
},
|
|
},
|
|
}),
|
|
} as Response;
|
|
}
|
|
|
|
function configFields() {
|
|
return [
|
|
{ key: "activity_id", value: "12" },
|
|
{ key: "api_app_id", value: "app-id" },
|
|
{ key: "api_secret", value: "secret" },
|
|
{ key: "status_api_url", value: "https://third.example/status" },
|
|
{ key: "push_api_url", value: "https://third.example/push" },
|
|
{ key: "status_success_code", value: "2001" },
|
|
{ key: "status_active_required", value: "false" },
|
|
{ key: "push_success_code", value: "2002" },
|
|
{ key: "redirect_url", value: "https://activity.example.com" },
|
|
{ key: "button_text", value: JSON.stringify({ en: "Claim Your Reward" }) },
|
|
];
|
|
}
|
|
|
|
describe("activity config", () => {
|
|
test("loads app-owned metaobject type by default and parses optional success criteria", async () => {
|
|
delete process.env.METAOBJECT_TYPE;
|
|
delete process.env.METAOBJECT_HANDLE;
|
|
|
|
let variables: Record<string, unknown> | undefined;
|
|
const config = await getActivityConfig({
|
|
graphql: async (_query, options) => {
|
|
variables = options?.variables;
|
|
return metaobjectResponse(configFields());
|
|
},
|
|
});
|
|
|
|
expect(variables).toEqual({ type: "$app:dreame_activity", handle: "dreame-activity-config" });
|
|
expect(config).toMatchObject({
|
|
activityId: 12,
|
|
statusSuccessCode: 2001,
|
|
statusActiveRequired: false,
|
|
pushSuccessCode: 2002,
|
|
});
|
|
});
|
|
|
|
test("allows overriding the metaobject type for merchant-owned legacy configs", async () => {
|
|
process.env.METAOBJECT_TYPE = "dreame_activity";
|
|
process.env.METAOBJECT_HANDLE = "legacy-config";
|
|
|
|
let variables: Record<string, unknown> | undefined;
|
|
await getActivityConfig({
|
|
graphql: async (_query, options) => {
|
|
variables = options?.variables;
|
|
return metaobjectResponse(configFields());
|
|
},
|
|
});
|
|
|
|
expect(variables).toEqual({ type: "dreame_activity", handle: "legacy-config" });
|
|
});
|
|
});
|