|
| 1 | +import { getConfig } from "../config"; |
| 2 | +import { parseResolveResponse, Resolve } from "./resolve"; |
| 3 | + |
| 4 | +describe("resolve", () => { |
| 5 | + test("forwards identifier when present", () => { |
| 6 | + const config = getConfig({ host: "host", site: "site" }); |
| 7 | + |
| 8 | + const fetchSpy = jest.spyOn(window, "fetch"); |
| 9 | + |
| 10 | + Resolve(config, "id"); |
| 11 | + expect(fetchSpy).toHaveBeenCalledWith( |
| 12 | + expect.objectContaining({ |
| 13 | + method: "GET", |
| 14 | + url: `https://host/site/v1/resolve?id=id&osdk=web-0.0.0-experimental&cookies=yes`, |
| 15 | + }) |
| 16 | + ); |
| 17 | + |
| 18 | + Resolve(config); |
| 19 | + expect.objectContaining({ |
| 20 | + method: "GET", |
| 21 | + url: `https://host/site/v1/resolve?osdk=web-0.0.0-experimental&cookies=yes`, |
| 22 | + }); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("parseResolveResponse", () => { |
| 27 | + test("parses expected responses", () => { |
| 28 | + const empty = { clusters: [], lmpid: "" }; |
| 29 | + |
| 30 | + const cases = [ |
| 31 | + // Unexpected response types return empty response |
| 32 | + { input: {}, output: empty }, |
| 33 | + { input: null, output: empty }, |
| 34 | + { input: undefined, output: empty }, |
| 35 | + { input: 1, output: empty }, |
| 36 | + { input: true, output: empty }, |
| 37 | + { input: [], output: empty }, |
| 38 | + |
| 39 | + { input: { clusters: [null] }, output: empty }, |
| 40 | + { input: { clusters: [undefined] }, output: empty }, |
| 41 | + { input: { clusters: [1] }, output: empty }, |
| 42 | + { input: { clusters: [true] }, output: empty }, |
| 43 | + { input: { clusters: [[]] }, output: empty }, |
| 44 | + |
| 45 | + { input: { clusters: [{ ids: {}, traits: {} }] }, output: empty }, |
| 46 | + { input: { clusters: [{ ids: null, traits: null }] }, output: empty }, |
| 47 | + { input: { clusters: [{ ids: undefined, traits: undefined }] }, output: empty }, |
| 48 | + { input: { clusters: [{ ids: 1, traits: 1 }] }, output: empty }, |
| 49 | + { input: { clusters: [{ ids: true, traits: true }] }, output: empty }, |
| 50 | + { input: { clusters: [{ ids: [], traits: [] }] }, output: empty }, |
| 51 | + |
| 52 | + { input: { clusters: [{ ids: [null], traits: [null] }] }, output: empty }, |
| 53 | + { input: { clusters: [{ ids: [undefined], traits: [undefined] }] }, output: empty }, |
| 54 | + { input: { clusters: [{ ids: [1], traits: [1] }] }, output: empty }, |
| 55 | + { input: { clusters: [{ ids: [true], traits: [true] }] }, output: empty }, |
| 56 | + |
| 57 | + // Additional properties are skipped |
| 58 | + { |
| 59 | + input: { |
| 60 | + clusters: [ |
| 61 | + { ids: ["i4:<ip>", "e:<sha256>"], traits: [{ key: "<key>", value: "<value>" }], additional: "property" }, |
| 62 | + ], |
| 63 | + }, |
| 64 | + output: { |
| 65 | + clusters: [{ ids: ["i4:<ip>", "e:<sha256>"], traits: [{ key: "<key>", value: "<value>" }] }], |
| 66 | + lmpid: "", |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + input: { clusters: [{ ids: ["i4:<ip>", "e:<sha256>"], traits: [{ key: "<key>", value: "<value>" }] }] }, |
| 71 | + output: { |
| 72 | + clusters: [{ ids: ["i4:<ip>", "e:<sha256>"], traits: [{ key: "<key>", value: "<value>" }] }], |
| 73 | + lmpid: "", |
| 74 | + }, |
| 75 | + }, |
| 76 | + |
| 77 | + // Lmpid is returned when matching expected type |
| 78 | + { input: { lmpid: 1 }, output: { clusters: [], lmpid: "" } }, |
| 79 | + { input: { lmpid: null }, output: { clusters: [], lmpid: "" } }, |
| 80 | + { input: { lmpid: undefined }, output: { clusters: [], lmpid: "" } }, |
| 81 | + { input: { lmpid: "lmpid" }, output: { clusters: [], lmpid: "lmpid" } }, |
| 82 | + ]; |
| 83 | + |
| 84 | + for (const c of cases) { |
| 85 | + expect(parseResolveResponse(c.input)).toEqual(c.output); |
| 86 | + } |
| 87 | + }); |
| 88 | +}); |
0 commit comments