|
| 1 | +import { |
| 2 | + findAlreadyInstalledApp, |
| 3 | + findInstalledAppByIdentifier, |
| 4 | +} from "./findInstalledAppByIdentifier"; |
| 5 | + |
| 6 | +describe("findInstalledAppByIdentifier", () => { |
| 7 | + const installedApps = [ |
| 8 | + { |
| 9 | + id: "app-1", |
| 10 | + identifier: "saleor.pulse", |
| 11 | + manifestUrl: "https://pulse.saleor.app/api/manifest", |
| 12 | + name: "Saleor Pulse", |
| 13 | + }, |
| 14 | + { |
| 15 | + id: "app-2", |
| 16 | + identifier: "other.app", |
| 17 | + manifestUrl: "https://other.app/api/manifest", |
| 18 | + name: "Other App", |
| 19 | + }, |
| 20 | + ]; |
| 21 | + |
| 22 | + it("returns the installed app with a matching identifier", () => { |
| 23 | + // Arrange & Act |
| 24 | + const app = findInstalledAppByIdentifier(installedApps, "saleor.pulse"); |
| 25 | + |
| 26 | + // Assert |
| 27 | + expect(app?.id).toBe("app-1"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("returns undefined when no installed app matches", () => { |
| 31 | + // Arrange & Act |
| 32 | + const app = findInstalledAppByIdentifier(installedApps, "unknown.app"); |
| 33 | + |
| 34 | + // Assert |
| 35 | + expect(app).toBeUndefined(); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("findAlreadyInstalledApp", () => { |
| 40 | + const installedApps = [ |
| 41 | + { |
| 42 | + id: "app-1", |
| 43 | + identifier: "saleor.pulse", |
| 44 | + manifestUrl: "https://pulse.saleor.app/api/manifest", |
| 45 | + name: "Saleor Pulse", |
| 46 | + }, |
| 47 | + { |
| 48 | + id: "app-2", |
| 49 | + identifier: "other.app", |
| 50 | + manifestUrl: "https://other.app/api/manifest", |
| 51 | + name: "Other App", |
| 52 | + }, |
| 53 | + ]; |
| 54 | + |
| 55 | + it("prefers identifier match over manifest URL", () => { |
| 56 | + // Arrange & Act |
| 57 | + const app = findAlreadyInstalledApp(installedApps, { |
| 58 | + identifier: "saleor.pulse", |
| 59 | + manifestUrl: "https://different-host.example/api/manifest", |
| 60 | + }); |
| 61 | + |
| 62 | + // Assert |
| 63 | + expect(app?.id).toBe("app-1"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("resolves the app from Saleor's UNIQUE error message", () => { |
| 67 | + // Arrange & Act |
| 68 | + const app = findAlreadyInstalledApp(installedApps, { |
| 69 | + manifestUrl: "https://staging.pulse.saleor.app/api/manifest", |
| 70 | + uniqueError: { |
| 71 | + field: "identifier", |
| 72 | + message: "App with the same identifier is already installed: Saleor Pulse", |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + // Assert |
| 77 | + expect(app?.id).toBe("app-1"); |
| 78 | + }); |
| 79 | + |
| 80 | + it("falls back to manifest URL when identifier is missing", () => { |
| 81 | + // Arrange & Act |
| 82 | + const app = findAlreadyInstalledApp(installedApps, { |
| 83 | + manifestUrl: "https://other.app/api/manifest", |
| 84 | + }); |
| 85 | + |
| 86 | + // Assert |
| 87 | + expect(app?.id).toBe("app-2"); |
| 88 | + }); |
| 89 | +}); |
0 commit comments