|
1 |
| -import { getHostFromUrl } from "../zones"; |
| 1 | +import { rest } from "msw"; |
| 2 | +import { getHostFromUrl, getZoneForRoute } from "../zones"; |
| 3 | +import { mockAccountId, mockApiToken } from "./helpers/mock-account-id"; |
| 4 | +import { msw } from "./helpers/msw"; |
| 5 | +function mockGetZones(domain: string, zones: { id: string }[] = []) { |
| 6 | + msw.use( |
| 7 | + rest.get("*/zones", (req, res, ctx) => { |
| 8 | + expect([...req.url.searchParams.entries()]).toEqual([["name", domain]]); |
| 9 | + |
| 10 | + return res.once( |
| 11 | + ctx.status(200), |
| 12 | + ctx.json({ |
| 13 | + success: true, |
| 14 | + errors: [], |
| 15 | + messages: [], |
| 16 | + result: zones, |
| 17 | + }) |
| 18 | + ); |
| 19 | + }) |
| 20 | + ); |
| 21 | +} |
2 | 22 |
|
3 | 23 | describe("Zones", () => {
|
| 24 | + mockAccountId(); |
| 25 | + mockApiToken(); |
4 | 26 | describe("getHostFromUrl", () => {
|
5 | 27 | test.each`
|
6 | 28 | pattern | host
|
@@ -35,4 +57,84 @@ describe("Zones", () => {
|
35 | 57 | expect(getHostFromUrl(pattern)).toBe(host);
|
36 | 58 | });
|
37 | 59 | });
|
| 60 | + describe("getZoneForRoute", () => { |
| 61 | + test("string route", async () => { |
| 62 | + mockGetZones("example.com", [{ id: "example-id" }]); |
| 63 | + expect(await getZoneForRoute("example.com/*")).toEqual({ |
| 64 | + host: "example.com", |
| 65 | + id: "example-id", |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + test("string route (not a zone)", async () => { |
| 70 | + mockGetZones("wrong.com", []); |
| 71 | + await expect( |
| 72 | + getZoneForRoute("wrong.com/*") |
| 73 | + ).rejects.toMatchInlineSnapshot( |
| 74 | + `[Error: Could not find zone for wrong.com]` |
| 75 | + ); |
| 76 | + }); |
| 77 | + test("zone_id route", async () => { |
| 78 | + // example-id and other-id intentionally different to show that the API is not called |
| 79 | + // when a zone_id is provided in the route |
| 80 | + mockGetZones("example.com", [{ id: "example-id" }]); |
| 81 | + expect( |
| 82 | + await getZoneForRoute({ pattern: "example.com/*", zone_id: "other-id" }) |
| 83 | + ).toEqual({ |
| 84 | + host: "example.com", |
| 85 | + id: "other-id", |
| 86 | + }); |
| 87 | + }); |
| 88 | + test("zone_id route (custom hostname)", async () => { |
| 89 | + // example-id and other-id intentionally different to show that the API is not called |
| 90 | + // when a zone_id is provided in the route |
| 91 | + mockGetZones("example.com", [{ id: "example-id" }]); |
| 92 | + expect( |
| 93 | + await getZoneForRoute({ |
| 94 | + pattern: "some.third-party.com/*", |
| 95 | + zone_id: "other-id", |
| 96 | + }) |
| 97 | + ).toEqual({ |
| 98 | + host: "some.third-party.com", |
| 99 | + id: "other-id", |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + test("zone_name route (apex)", async () => { |
| 104 | + mockGetZones("example.com", [{ id: "example-id" }]); |
| 105 | + expect( |
| 106 | + await getZoneForRoute({ |
| 107 | + pattern: "example.com/*", |
| 108 | + zone_name: "example.com", |
| 109 | + }) |
| 110 | + ).toEqual({ |
| 111 | + host: "example.com", |
| 112 | + id: "example-id", |
| 113 | + }); |
| 114 | + }); |
| 115 | + test("zone_name route (subdomain)", async () => { |
| 116 | + mockGetZones("example.com", [{ id: "example-id" }]); |
| 117 | + expect( |
| 118 | + await getZoneForRoute({ |
| 119 | + pattern: "subdomain.example.com/*", |
| 120 | + zone_name: "example.com", |
| 121 | + }) |
| 122 | + ).toEqual({ |
| 123 | + host: "subdomain.example.com", |
| 124 | + id: "example-id", |
| 125 | + }); |
| 126 | + }); |
| 127 | + test("zone_name route (custom hostname)", async () => { |
| 128 | + mockGetZones("example.com", [{ id: "example-id" }]); |
| 129 | + expect( |
| 130 | + await getZoneForRoute({ |
| 131 | + pattern: "some.third-party.com/*", |
| 132 | + zone_name: "example.com", |
| 133 | + }) |
| 134 | + ).toEqual({ |
| 135 | + host: "some.third-party.com", |
| 136 | + id: "example-id", |
| 137 | + }); |
| 138 | + }); |
| 139 | + }); |
38 | 140 | });
|
0 commit comments