| 1 |
import Factory from "@lib/factory"; |
| 2 |
import trait from "@lib/trait"; |
| 3 |
|
| 4 |
describe("Unit | Factory", function () { |
| 5 |
test("it exists", () => { |
| 6 |
expect(Factory).toBeTruthy(); |
| 7 |
}); |
| 8 |
|
| 9 |
test("the base class builds empty objects", () => { |
| 10 |
let f = new Factory(); |
| 11 |
let data = f.build(); |
| 12 |
|
| 13 |
expect(data).toEqual({}); |
| 14 |
}); |
| 15 |
|
| 16 |
test("a noop extension builds empty objects", () => { |
| 17 |
let EmptyFactory = Factory.extend(); |
| 18 |
let f = new EmptyFactory(); |
| 19 |
let data = f.build(); |
| 20 |
|
| 21 |
expect(data).toEqual({}); |
| 22 |
}); |
| 23 |
|
| 24 |
test("it works with strings, numbers and booleans", () => { |
| 25 |
let AFactory = Factory.extend({ |
| 26 |
name: "Sam", |
| 27 |
age: 28, |
| 28 |
alive: true, |
| 29 |
}); |
| 30 |
|
| 31 |
let f = new AFactory(); |
| 32 |
let data = f.build(); |
| 33 |
|
| 34 |
expect(data).toEqual({ name: "Sam", age: 28, alive: true }); |
| 35 |
}); |
| 36 |
|
| 37 |
test("it supports inheritance", () => { |
| 38 |
let PersonFactory = Factory.extend({ |
| 39 |
species: "human", |
| 40 |
}); |
| 41 |
let ManFactory = PersonFactory.extend({ |
| 42 |
gender: "male", |
| 43 |
}); |
| 44 |
let SamFactory = ManFactory.extend({ |
| 45 |
name: "Sam", |
| 46 |
}); |
| 47 |
|
| 48 |
let p = new PersonFactory(); |
| 49 |
let m = new ManFactory(); |
| 50 |
let s = new SamFactory(); |
| 51 |
|
| 52 |
expect(p.build()).toEqual({ species: "human" }); |
| 53 |
expect(m.build()).toEqual({ species: "human", gender: "male" }); |
| 54 |
expect(s.build()).toEqual({ |
| 55 |
species: "human", |
| 56 |
gender: "male", |
| 57 |
name: "Sam", |
| 58 |
}); |
| 59 |
}); |
| 60 |
|
| 61 |
test("it can use sequences", () => { |
| 62 |
let PostFactory = Factory.extend({ |
| 63 |
likes(i) { |
| 64 |
return 5 * i; |
| 65 |
}, |
| 66 |
}); |
| 67 |
|
| 68 |
let p = new PostFactory(); |
| 69 |
let post1 = p.build(1); |
| 70 |
let post2 = p.build(2); |
| 71 |
|
| 72 |
expect(post1).toEqual({ likes: 5 }); |
| 73 |
expect(post2).toEqual({ likes: 10 }); |
| 74 |
}); |
| 75 |
|
| 76 |
test("it can reuse static properties", () => { |
| 77 |
let BazFactory = Factory.extend({ |
| 78 |
foo: 5, |
| 79 |
bar(i) { |
| 80 |
return this.foo * i; |
| 81 |
}, |
| 82 |
}); |
| 83 |
|
| 84 |
let b = new BazFactory(); |
| 85 |
let baz1 = b.build(1); |
| 86 |
let baz2 = b.build(2); |
| 87 |
|
| 88 |
expect(baz1).toEqual({ foo: 5, bar: 5 }); |
| 89 |
expect(baz2).toEqual({ foo: 5, bar: 10 }); |
| 90 |
}); |
| 91 |
|
| 92 |
test("it can reuse dynamic properties", () => { |
| 93 |
let BazFactory = Factory.extend({ |
| 94 |
foo(i) { |
| 95 |
return 5 * i; |
| 96 |
}, |
| 97 |
bar() { |
| 98 |
return this.foo * 2; |
| 99 |
}, |
| 100 |
}); |
| 101 |
|
| 102 |
let b = new BazFactory(); |
| 103 |
let baz1 = b.build(1); |
| 104 |
let baz2 = b.build(2); |
| 105 |
|
| 106 |
expect(baz1).toEqual({ foo: 5, bar: 10 }); |
| 107 |
expect(baz2).toEqual({ foo: 10, bar: 20 }); |
| 108 |
}); |
| 109 |
|
| 110 |
test("it can have dynamic properties that depend on another", () => { |
| 111 |
let BazFactory = Factory.extend({ |
| 112 |
name() { |
| 113 |
return "foo"; |
| 114 |
}, |
| 115 |
bar() { |
| 116 |
return this.name.substr(1); |
| 117 |
}, |
| 118 |
}); |
| 119 |
|
| 120 |
let b = new BazFactory(); |
| 121 |
let baz1 = b.build(1); |
| 122 |
|
| 123 |
expect(baz1).toEqual({ name: "foo", bar: "oo" }); |
| 124 |
}); |
| 125 |
|
| 126 |
test("it can reference properties out of order", () => { |
| 127 |
let BazFactory = Factory.extend({ |
| 128 |
bar() { |
| 129 |
return this.foo + 2; |
| 130 |
}, |
| 131 |
|
| 132 |
baz: 6, |
| 133 |
|
| 134 |
foo(i) { |
| 135 |
return this.baz * i; |
| 136 |
}, |
| 137 |
}); |
| 138 |
|
| 139 |
let b = new BazFactory(); |
| 140 |
let baz1 = b.build(1); |
| 141 |
let baz2 = b.build(2); |
| 142 |
|
| 143 |
expect(baz1).toEqual({ baz: 6, foo: 6, bar: 8 }); |
| 144 |
expect(baz2).toEqual({ baz: 6, foo: 12, bar: 14 }); |
| 145 |
}); |
| 146 |
|
| 147 |
test("it can reference multiple properties in any order", () => { |
| 148 |
let FooFactory = Factory.extend({ |
| 149 |
foo() { |
| 150 |
return this.bar + this.baz; |
| 151 |
}, |
| 152 |
|
| 153 |
bar: 6, |
| 154 |
|
| 155 |
baz: 10, |
| 156 |
}); |
| 157 |
|
| 158 |
let BarFactory = Factory.extend({ |
| 159 |
bar: 6, |
| 160 |
|
| 161 |
foo() { |
| 162 |
return this.bar + this.baz; |
| 163 |
}, |
| 164 |
|
| 165 |
baz: 10, |
| 166 |
}); |
| 167 |
|
| 168 |
let BazFactory = Factory.extend({ |
| 169 |
bar: 6, |
| 170 |
|
| 171 |
baz: 10, |
| 172 |
|
| 173 |
foo() { |
| 174 |
return this.bar + this.baz; |
| 175 |
}, |
| 176 |
}); |
| 177 |
|
| 178 |
let Foo = new FooFactory(); |
| 179 |
let Bar = new BarFactory(); |
| 180 |
let Baz = new BazFactory(); |
| 181 |
|
| 182 |
let foo = Foo.build(1); |
| 183 |
let bar = Bar.build(1); |
| 184 |
let baz = Baz.build(1); |
| 185 |
|
| 186 |
expect(foo).toEqual({ foo: 16, bar: 6, baz: 10 }); |
| 187 |
expect(bar).toEqual({ foo: 16, bar: 6, baz: 10 }); |
| 188 |
expect(baz).toEqual({ foo: 16, bar: 6, baz: 10 }); |
| 189 |
}); |
| 190 |
|
| 191 |
test("it can reference properties on complex object", () => { |
| 192 |
let AbcFactory = Factory.extend({ |
| 193 |
a(i) { |
| 194 |
return this.b + i; |
| 195 |
}, |
| 196 |
b() { |
| 197 |
return this.c + 1; |
| 198 |
}, |
| 199 |
c() { |
| 200 |
return this.f + 1; |
| 201 |
}, |
| 202 |
d(i) { |
| 203 |
return this.e + i; |
| 204 |
}, |
| 205 |
e() { |
| 206 |
return this.c + 1; |
| 207 |
}, |
| 208 |
f: 1, |
| 209 |
g: 2, |
| 210 |
h: 3, |
| 211 |
}); |
| 212 |
|
| 213 |
let b = new AbcFactory(); |
| 214 |
let abc1 = b.build(1); |
| 215 |
let abc2 = b.build(2); |
| 216 |
|
| 217 |
expect(abc1).toEqual({ a: 4, b: 3, c: 2, d: 4, e: 3, f: 1, g: 2, h: 3 }); |
| 218 |
expect(abc2).toEqual({ a: 5, b: 3, c: 2, d: 5, e: 3, f: 1, g: 2, h: 3 }); |
| 219 |
}); |
| 220 |
|
| 221 |
test("throws meaningfull exception on circular reference", () => { |
| 222 |
let BazFactory = Factory.extend({ |
| 223 |
bar() { |
| 224 |
return this.foo; |
| 225 |
}, |
| 226 |
|
| 227 |
foo() { |
| 228 |
return this.bar; |
| 229 |
}, |
| 230 |
}); |
| 231 |
|
| 232 |
let b = new BazFactory(); |
| 233 |
expect(function () { |
| 234 |
b.build(1); |
| 235 |
}).toThrow(); |
| 236 |
}); |
| 237 |
|
| 238 |
test("#build skips invoking `afterCreate`", () => { |
| 239 |
let skipped = true; |
| 240 |
let PostFactory = Factory.extend({ |
| 241 |
afterCreate() { |
| 242 |
skipped = false; |
| 243 |
}, |
| 244 |
}); |
| 245 |
|
| 246 |
let factory = new PostFactory(); |
| 247 |
let post = factory.build(0); |
| 248 |
|
| 249 |
expect(skipped).toBeTruthy(); |
| 250 |
expect(typeof post.afterCreate).toEqual("undefined"); |
| 251 |
}); |
| 252 |
|
| 253 |
test("extractAfterCreateCallbacks returns all afterCreate callbacks from factory with the base one being first", () => { |
| 254 |
let PostFactory = Factory.extend({ |
| 255 |
published: trait({ |
| 256 |
afterCreate() { |
| 257 |
return "from published"; |
| 258 |
}, |
| 259 |
}), |
| 260 |
|
| 261 |
withComments: trait({ |
| 262 |
afterCreate() { |
| 263 |
return "from withComments"; |
| 264 |
}, |
| 265 |
}), |
| 266 |
|
| 267 |
otherTrait: trait({}), |
| 268 |
|
| 269 |
afterCreate() { |
| 270 |
return "from base"; |
| 271 |
}, |
| 272 |
}); |
| 273 |
|
| 274 |
let callbacks = PostFactory.extractAfterCreateCallbacks(); |
| 275 |
expect(callbacks).toHaveLength(3); |
| 276 |
expect(callbacks.map((cb) => cb())).toEqual([ |
| 277 |
"from base", |
| 278 |
"from published", |
| 279 |
"from withComments", |
| 280 |
]); |
| 281 |
}); |
| 282 |
|
| 283 |
test("extractAfterCreateCallbacks filters traits from which the afterCreate callbacks will be extracted from", () => { |
| 284 |
let PostFactory = Factory.extend({ |
| 285 |
published: trait({ |
| 286 |
afterCreate() { |
| 287 |
return "from published"; |
| 288 |
}, |
| 289 |
}), |
| 290 |
|
| 291 |
withComments: trait({ |
| 292 |
afterCreate() { |
| 293 |
return "from withComments"; |
| 294 |
}, |
| 295 |
}), |
| 296 |
|
| 297 |
otherTrait: trait({}), |
| 298 |
|
| 299 |
afterCreate() { |
| 300 |
return "from base"; |
| 301 |
}, |
| 302 |
}); |
| 303 |
|
| 304 |
expect( |
| 305 |
PostFactory.extractAfterCreateCallbacks({ traits: [] }) |
| 306 |
).toHaveLength(1); |
| 307 |
expect( |
| 308 |
PostFactory.extractAfterCreateCallbacks({ traits: [] }).map((cb) => cb()) |
| 309 |
).toEqual(["from base"]); |
| 310 |
|
| 311 |
expect( |
| 312 |
PostFactory.extractAfterCreateCallbacks({ traits: ["withComments"] }) |
| 313 |
).toHaveLength(2); |
| 314 |
expect( |
| 315 |
PostFactory.extractAfterCreateCallbacks({ |
| 316 |
traits: ["withComments"], |
| 317 |
}).map((cb) => cb()) |
| 318 |
).toEqual(["from base", "from withComments"]); |
| 319 |
|
| 320 |
expect( |
| 321 |
PostFactory.extractAfterCreateCallbacks({ |
| 322 |
traits: ["withComments", "published"], |
| 323 |
}) |
| 324 |
).toHaveLength(3); |
| 325 |
expect( |
| 326 |
PostFactory.extractAfterCreateCallbacks({ |
| 327 |
traits: ["withComments", "published"], |
| 328 |
}).map((cb) => cb()) |
| 329 |
).toEqual(["from base", "from withComments", "from published"]); |
| 330 |
|
| 331 |
expect( |
| 332 |
PostFactory.extractAfterCreateCallbacks({ |
| 333 |
traits: ["withComments", "otherTrait"], |
| 334 |
}) |
| 335 |
).toHaveLength(2); |
| 336 |
expect( |
| 337 |
PostFactory.extractAfterCreateCallbacks({ |
| 338 |
traits: ["withComments", "otherTrait"], |
| 339 |
}).map((cb) => cb()) |
| 340 |
).toEqual(["from base", "from withComments"]); |
| 341 |
}); |
| 342 |
|
| 343 |
test("isTrait returns true if there is a trait with given name", () => { |
| 344 |
let PostFactory = Factory.extend({ |
| 345 |
title: "Lorem ipsum", |
| 346 |
|
| 347 |
published: trait({ |
| 348 |
isPublished: true, |
| 349 |
}), |
| 350 |
|
| 351 |
someNestedObject: { |
| 352 |
value: "nested", |
| 353 |
}, |
| 354 |
}); |
| 355 |
|
| 356 |
expect(!PostFactory.isTrait("title")).toBeTruthy(); |
| 357 |
expect(PostFactory.isTrait("published")).toBeTruthy(); |
| 358 |
expect(!PostFactory.isTrait("someNestedObject")).toBeTruthy(); |
| 359 |
expect(!PostFactory.isTrait("notdefined")).toBeTruthy(); |
| 360 |
}); |
| 361 |
}); |