-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmanifest-action-handler.test.ts
84 lines (65 loc) · 2.74 KB
/
manifest-action-handler.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { beforeEach, describe, expect, it, vi } from "vitest";
import { SALEOR_SCHEMA_VERSION } from "@/const";
import { MockAdapter } from "@/test-utils/mock-adapter";
import { AppManifest } from "@/types";
import { ManifestActionHandler } from "./manifest-action-handler";
describe("ManifestActionHandler", () => {
const mockManifest: AppManifest = {
id: "test-app",
name: "Test Application",
version: "1.0.0",
appUrl: "http://example.com",
permissions: [],
tokenTargetUrl: "http://example.com/token",
};
let adapter: MockAdapter;
beforeEach(() => {
adapter = new MockAdapter({
mockHeaders: {
[SALEOR_SCHEMA_VERSION]: "3.20",
},
baseUrl: "http://example.com",
});
adapter.method = "GET";
});
it("should call manifest factory and return 200 status when it resolves", async () => {
const handler = new ManifestActionHandler(adapter);
const manifestFactory = vi.fn().mockResolvedValue(mockManifest);
const result = await handler.handleAction({ manifestFactory });
expect(result.status).toBe(200);
expect(result.body).toEqual(mockManifest);
expect(manifestFactory).toHaveBeenCalledWith({
appBaseUrl: "http://example.com",
request: {},
schemaVersion: [3, 20],
});
});
it("should call manifest factory and return 500 when it throws an error", async () => {
const handler = new ManifestActionHandler(adapter);
const manifestFactory = vi.fn().mockRejectedValue(new Error("Test error"));
const result = await handler.handleAction({ manifestFactory });
expect(result.status).toBe(500);
expect(result.body).toBe("Error resolving manifest file.");
});
it("should return 405 when not called using HTTP GET method", async () => {
adapter.method = "POST";
const handler = new ManifestActionHandler(adapter);
const manifestFactory = vi.fn().mockResolvedValue(mockManifest);
const result = await handler.handleAction({ manifestFactory });
expect(result.status).toBe(405);
expect(result.body).toBe("Method not allowed");
expect(manifestFactory).not.toHaveBeenCalled();
});
/**
* api/manifest endpoint is GET and header should be optional. It can be used to install the app eventually,
* but also to preview the manifest from the plain GET request
*/
it("should NOT return 400 when receives null schema version header from unsupported legacy Saleor version", async () => {
adapter.getHeader = vi.fn().mockReturnValue(null);
const handler = new ManifestActionHandler(adapter);
const manifestFactory = vi.fn().mockResolvedValue(mockManifest);
const result = await handler.handleAction({ manifestFactory });
expect(result.status).toBe(200);
expect(manifestFactory).toHaveBeenCalled();
});
});