| 1 | import createMetaResolver from "../src/meta-resolver"; |
| 2 | |
| 3 | describe("createMetaResolver", () => { |
| 4 | const resolver = createMetaResolver({ |
| 5 | global: { |
| 6 | GlobalVar: 1, |
| 7 | }, |
| 8 | static: { |
| 9 | Obj: { |
| 10 | staticProperty: 2, |
| 11 | }, |
| 12 | }, |
| 13 | instance: { |
| 14 | instanceProperty: 3, |
| 15 | }, |
| 16 | }); |
| 17 | |
| 18 | describe("globals", () => { |
| 19 | it("used as global", () => { |
| 20 | expect( |
| 21 | resolver({ |
| 22 | kind: "global", |
| 23 | name: "GlobalVar", |
| 24 | }), |
| 25 | ).toStrictEqual({ kind: "global", name: "GlobalVar", desc: 1 }); |
| 26 | }); |
| 27 | |
| 28 | it("used as global object property", () => { |
| 29 | expect( |
| 30 | resolver({ |
| 31 | kind: "property", |
| 32 | placement: "static", |
| 33 | object: "globalThis", |
| 34 | key: "GlobalVar", |
| 35 | }), |
| 36 | ).toStrictEqual({ kind: "global", name: "GlobalVar", desc: 1 }); |
| 37 | }); |
| 38 | |
| 39 | it("fallbacks to instance when used as global object property", () => { |
| 40 | expect( |
| 41 | resolver({ |
| 42 | kind: "property", |
| 43 | placement: "static", |
| 44 | object: "globalThis", |
| 45 | key: "instanceProperty", |
| 46 | }), |
| 47 | ).toStrictEqual({ kind: "instance", name: "instanceProperty", desc: 3 }); |
| 48 | }); |
| 49 | |
| 50 | it("does not resolve to object with static property polyfill", () => { |
| 51 | expect( |
| 52 | resolver({ |
| 53 | kind: "global", |
| 54 | name: "Obj", |
| 55 | }), |
| 56 | ).toBeUndefined(); |
| 57 | }); |
| 58 | |
| 59 | it("does not resolve to a static property polyfill", () => { |
| 60 | expect( |
| 61 | resolver({ |
| 62 | kind: "global", |
| 63 | name: "staticProperty", |
| 64 | }), |
| 65 | ).toBeUndefined(); |
| 66 | }); |
| 67 | }); |
| 68 | |
| 69 | describe("static properties", () => { |
| 70 | it("used as static property", () => { |
| 71 | expect( |
| 72 | resolver({ |
| 73 | kind: "property", |
| 74 | placement: "static", |
| 75 | object: "Obj", |
| 76 | key: "staticProperty", |
| 77 | }), |
| 78 | ).toStrictEqual({ kind: "static", name: "Obj$staticProperty", desc: 2 }); |
| 79 | }); |
| 80 | |
| 81 | it("the base object must match", () => { |
| 82 | expect( |
| 83 | resolver({ |
| 84 | kind: "property", |
| 85 | placement: "static", |
| 86 | object: "AbotherObj", |
| 87 | key: "staticProperty", |
| 88 | }), |
| 89 | ).toBeUndefined(); |
| 90 | }); |
| 91 | |
| 92 | it("fallbacks to instance properties for known objects", () => { |
| 93 | expect( |
| 94 | resolver({ |
| 95 | kind: "property", |
| 96 | placement: "static", |
| 97 | object: "Obj", |
| 98 | key: "instanceProperty", |
| 99 | }), |
| 100 | ).toStrictEqual({ kind: "instance", name: "instanceProperty", desc: 3 }); |
| 101 | }); |
| 102 | |
| 103 | it("fallbacks to instance properties for unknown objects", () => { |
| 104 | expect( |
| 105 | resolver({ |
| 106 | kind: "property", |
| 107 | placement: "static", |
| 108 | object: "AnotherObj", |
| 109 | key: "instanceProperty", |
| 110 | }), |
| 111 | ).toStrictEqual({ kind: "instance", name: "instanceProperty", desc: 3 }); |
| 112 | }); |
| 113 | }); |
| 114 | |
| 115 | describe("instance properties", () => { |
| 116 | it("used as instance property", () => { |
| 117 | expect( |
| 118 | resolver({ |
| 119 | kind: "property", |
| 120 | placement: "protoyype", |
| 121 | key: "instanceProperty", |
| 122 | }), |
| 123 | ).toStrictEqual({ kind: "instance", name: "instanceProperty", desc: 3 }); |
| 124 | }); |
| 125 | |
| 126 | it("does not resolve to static properties", () => { |
| 127 | expect( |
| 128 | resolver({ |
| 129 | kind: "property", |
| 130 | placement: "protoyype", |
| 131 | object: "Obj", |
| 132 | key: "staticProperty", |
| 133 | }), |
| 134 | ).toBeUndefined(); |
| 135 | }); |
| 136 | |
| 137 | it("does not resolve to globals", () => { |
| 138 | expect( |
| 139 | resolver({ |
| 140 | kind: "property", |
| 141 | placement: "protoyype", |
| 142 | key: "GlobalVar", |
| 143 | }), |
| 144 | ).toBeUndefined(); |
| 145 | }); |
| 146 | }); |
| 147 | }); |