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