|
1 | | -import { ensureModels } from "@lib/orm/models"; |
| 1 | +import { ensureModels, createModels } from "@lib/orm/models"; |
2 | 2 | import { graphQLSchema } from "@tests/gql/schema"; |
3 | 3 |
|
4 | 4 | describe("Unit | ORM | models", function () { |
5 | | - const registeredModels = {}; |
6 | | - const mirageSchema = { |
7 | | - hasModelForModelName: () => false, |
8 | | - registerModel(typeName, model) { |
9 | | - registeredModels[typeName] = model; |
10 | | - }, |
11 | | - }; |
12 | | - |
13 | | - ensureModels({ graphQLSchema, mirageSchema }); |
14 | | - |
15 | | - it("does not register a model for the mutation type", function () { |
16 | | - expect(registeredModels.Mutation).toBeUndefined(); |
17 | | - }); |
| 5 | + describe("ensureModels", function () { |
| 6 | + const registeredModels = {}; |
| 7 | + const mirageSchema = { |
| 8 | + hasModelForModelName: () => false, |
| 9 | + registerModel(typeName, model) { |
| 10 | + registeredModels[typeName] = model; |
| 11 | + }, |
| 12 | + }; |
18 | 13 |
|
19 | | - it("does not register a model for the query type", function () { |
20 | | - expect(registeredModels.Query).toBeUndefined(); |
21 | | - }); |
| 14 | + ensureModels({ graphQLSchema, mirageSchema }); |
| 15 | + |
| 16 | + it("does not register a model for the mutation type", function () { |
| 17 | + expect(registeredModels.Mutation).toBeUndefined(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("does not register a model for the query type", function () { |
| 21 | + expect(registeredModels.Query).toBeUndefined(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("does not register a model for the subscription type", function () { |
| 25 | + expect(registeredModels.Subscription).toBeUndefined(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("does register the TestObject model", function () { |
| 29 | + expect(registeredModels.TestObject).toBeDefined(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("does register the TestCategory model", function () { |
| 33 | + expect(registeredModels.TestCategory).toBeDefined(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("does register the TestOption model", function () { |
| 37 | + expect(registeredModels.TestOption).toBeDefined(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("does not register the TestInterface model", function () { |
| 41 | + expect(registeredModels.TestInterface).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("does register the TestImplOne model", function () { |
| 45 | + expect(registeredModels.TestImplOne).toBeDefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("does register the TestImplTwo model", function () { |
| 49 | + expect(registeredModels.TestImplTwo).toBeDefined(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("does not register the TestUnion model", function () { |
| 53 | + expect(registeredModels.TestUnion).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("does register the TestUnionOne model", function () { |
| 57 | + expect(registeredModels.TestUnionOne).toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("does register the TestUnionTwo model", function () { |
| 61 | + expect(registeredModels.TestUnionTwo).toBeDefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("TestObject relationships", function () { |
| 65 | + const model = new registeredModels.TestObject({}, "TestObject"); |
| 66 | + |
| 67 | + function testRelationship( |
| 68 | + name, |
| 69 | + type, |
| 70 | + modelName, |
| 71 | + { isPolymorphic = false } = {} |
| 72 | + ) { |
| 73 | + expect(model.__proto__[name].constructor.name).toBe(type); |
| 74 | + expect(model.__proto__[name].modelName).toBe(modelName); |
| 75 | + expect(model.__proto__[name].opts.polymorphic).toBe(isPolymorphic); |
| 76 | + } |
| 77 | + |
| 78 | + // eslint-disable-next-line jest/expect-expect |
| 79 | + it("belongs to test category", function () { |
| 80 | + testRelationship("belongsToField", "BelongsTo", "test-category"); |
| 81 | + }); |
| 82 | + |
| 83 | + // eslint-disable-next-line jest/expect-expect |
| 84 | + it("has many test options", function () { |
| 85 | + testRelationship("hasManyField", "HasMany", "test-option"); |
| 86 | + }); |
| 87 | + |
| 88 | + // eslint-disable-next-line jest/expect-expect |
| 89 | + it("belongs to test interface", function () { |
| 90 | + testRelationship("interfaceField", "BelongsTo", "test-interface", { |
| 91 | + isPolymorphic: true, |
| 92 | + }); |
| 93 | + }); |
22 | 94 |
|
23 | | - it("does not register a model for the subscription type", function () { |
24 | | - expect(registeredModels.Subscription).toBeUndefined(); |
| 95 | + // eslint-disable-next-line jest/expect-expect |
| 96 | + it("has many test relay nodes", function () { |
| 97 | + testRelationship("relayConnectionField", "HasMany", "test-relay-node"); |
| 98 | + }); |
| 99 | + |
| 100 | + // eslint-disable-next-line jest/expect-expect |
| 101 | + it("has many test unions", function () { |
| 102 | + testRelationship("unionField", "HasMany", "test-union", { |
| 103 | + isPolymorphic: true, |
| 104 | + }); |
| 105 | + }); |
| 106 | + }); |
25 | 107 | }); |
26 | 108 |
|
27 | | - describe("TestObject relationships", function () { |
28 | | - const model = new registeredModels.TestObject({}, "TestObject"); |
29 | | - |
30 | | - function testRelationship( |
31 | | - name, |
32 | | - type, |
33 | | - modelName, |
34 | | - { isPolymorphic = false } = {} |
35 | | - ) { |
36 | | - expect(model.__proto__[name].constructor.name).toBe(type); |
37 | | - expect(model.__proto__[name].modelName).toBe(modelName); |
38 | | - expect(model.__proto__[name].opts.polymorphic).toBe(isPolymorphic); |
| 109 | + describe("createModels", function () { |
| 110 | + const models = createModels({ graphQLSchema }); |
| 111 | + |
| 112 | + function findModel(name) { |
| 113 | + return models.find((model) => model.name == name); |
39 | 114 | } |
40 | 115 |
|
41 | | - // eslint-disable-next-line jest/expect-expect |
42 | | - it("belongs to test category", function () { |
43 | | - testRelationship("belongsToField", "BelongsTo", "test-category"); |
| 116 | + it("does not create a model for the mutation type", function () { |
| 117 | + expect(findModel("Mutation")).toBeUndefined(); |
44 | 118 | }); |
45 | 119 |
|
46 | | - // eslint-disable-next-line jest/expect-expect |
47 | | - it("has many test options", function () { |
48 | | - testRelationship("hasManyField", "HasMany", "test-option"); |
| 120 | + it("does not create a model for the query type", function () { |
| 121 | + expect(findModel("Query")).toBeUndefined(); |
49 | 122 | }); |
50 | 123 |
|
51 | | - // eslint-disable-next-line jest/expect-expect |
52 | | - it("belongs to test interface", function () { |
53 | | - testRelationship("interfaceField", "BelongsTo", "test-interface", { |
54 | | - isPolymorphic: true, |
55 | | - }); |
| 124 | + it("does not create a model for the subscription type", function () { |
| 125 | + expect(findModel("Subscription")).toBeUndefined(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("does create the TestObject model", function () { |
| 129 | + expect(findModel("TestObject")).toBeDefined(); |
56 | 130 | }); |
57 | 131 |
|
58 | | - // eslint-disable-next-line jest/expect-expect |
59 | | - it("has many test relay nodes", function () { |
60 | | - testRelationship("relayConnectionField", "HasMany", "test-relay-node"); |
| 132 | + it("does create the TestCategory model", function () { |
| 133 | + expect(findModel("TestCategory")).toBeDefined(); |
61 | 134 | }); |
62 | 135 |
|
63 | | - // eslint-disable-next-line jest/expect-expect |
64 | | - it("has many test unions", function () { |
65 | | - testRelationship("unionField", "HasMany", "test-union", { |
66 | | - isPolymorphic: true, |
| 136 | + it("does create the TestOption model", function () { |
| 137 | + expect(findModel("TestOption")).toBeDefined(); |
| 138 | + }); |
| 139 | + |
| 140 | + it("does not create the TestInterface model", function () { |
| 141 | + expect(findModel("TestInterface")).toBeUndefined(); |
| 142 | + }); |
| 143 | + |
| 144 | + it("does create the TestImplOne model", function () { |
| 145 | + expect(findModel("TestImplOne")).toBeDefined(); |
| 146 | + }); |
| 147 | + |
| 148 | + it("does create the TestImplTwo model", function () { |
| 149 | + expect(findModel("TestImplTwo")).toBeDefined(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("does not create the TestUnion model", function () { |
| 153 | + expect(findModel("TestUnion")).toBeUndefined(); |
| 154 | + }); |
| 155 | + |
| 156 | + it("does create the TestUnionOne model", function () { |
| 157 | + expect(findModel("TestUnionOne")).toBeDefined(); |
| 158 | + }); |
| 159 | + |
| 160 | + it("does create the TestUnionTwo model", function () { |
| 161 | + expect(findModel("TestUnionTwo")).toBeDefined(); |
| 162 | + }); |
| 163 | + |
| 164 | + describe("TestObject relationships", function () { |
| 165 | + const model = findModel("TestObject"); |
| 166 | + |
| 167 | + function findRelationship(model, fieldName) { |
| 168 | + return model.associations.find((item) => item.fieldName == fieldName); |
| 169 | + } |
| 170 | + |
| 171 | + function testRelationship( |
| 172 | + fieldName, |
| 173 | + type, |
| 174 | + associationName, |
| 175 | + { isPolymorphic = false } = {} |
| 176 | + ) { |
| 177 | + const rel = findRelationship(model, fieldName); |
| 178 | + expect(rel).toBeDefined(); |
| 179 | + expect(rel.name).toBe(associationName); |
| 180 | + expect(rel.type).toBe(type); |
| 181 | + expect(rel.options.polymorphic).toBe(isPolymorphic); |
| 182 | + } |
| 183 | + |
| 184 | + // eslint-disable-next-line jest/expect-expect |
| 185 | + it("belongs to test category", function () { |
| 186 | + testRelationship("belongsToField", "belongsTo", "testCategory"); |
| 187 | + }); |
| 188 | + |
| 189 | + // eslint-disable-next-line jest/expect-expect |
| 190 | + it("has many test options", function () { |
| 191 | + testRelationship("hasManyField", "hasMany", "testOption"); |
| 192 | + }); |
| 193 | + |
| 194 | + // eslint-disable-next-line jest/expect-expect |
| 195 | + it("belongs to test interface", function () { |
| 196 | + testRelationship("interfaceField", "belongsTo", "testInterface", { |
| 197 | + isPolymorphic: true, |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + // eslint-disable-next-line jest/expect-expect |
| 202 | + it("has many test relay nodes", function () { |
| 203 | + testRelationship("relayConnectionField", "hasMany", "testRelayNode"); |
| 204 | + }); |
| 205 | + |
| 206 | + // eslint-disable-next-line jest/expect-expect |
| 207 | + it("has many test unions", function () { |
| 208 | + testRelationship("unionField", "hasMany", "testUnion", { |
| 209 | + isPolymorphic: true, |
| 210 | + }); |
67 | 211 | }); |
68 | 212 | }); |
69 | 213 | }); |
|
0 commit comments