Debian Codegraph @

__tests__/internal/unit/server-test.js

raw
1 import {
2 createServer,
3 Server,
4 Model,
5 Factory,
6 belongsTo,
7 hasMany,
8 trait,
9 association,
10 } from "@lib";
11
12 describe("Unit | Server", function () {
13 test("it can be instantiated", () => {
14 let server = new Server({ environment: "test" });
15
16 expect(server).toBeTruthy();
17
18 server.shutdown();
19 });
20
21 test("routes return pretender handler", () => {
22 let server = new Server({ environment: "test" });
23
24 let handler = server.post("foo");
25
26 expect(handler.numberOfCalls).toBe(0);
27
28 server.shutdown();
29 });
30
31 test("it runs the default scenario in non-test environments", () => {
32 expect.assertions(1);
33
34 let server = new Server({
35 environment: "development",
36 seeds() {
37 expect(true).toBeTruthy();
38 },
39 });
40
41 server.shutdown();
42 });
43 });
44
45 describe("Unit | createServer", function () {
46 test("it returns a server instance", () => {
47 let server = createServer();
48
49 expect(server).toBeTruthy();
50
51 server.shutdown();
52 });
53
54 test("routes return pretender handler", () => {
55 let server = createServer({ environment: "test" });
56
57 let handler = server.post("foo");
58
59 expect(handler.numberOfCalls).toBe(0);
60
61 server.shutdown();
62 });
63
64 test("it runs the default scenario in non-test environments", () => {
65 expect.assertions(1);
66
67 let server = createServer({
68 environment: "development",
69 seeds() {
70 expect(true).toBeTruthy();
71 },
72 });
73
74 server.shutdown();
75 });
76 });
77
78 describe("Unit | Server #loadConfig", function () {
79 test("forces timing to 0 in test environment", () => {
80 let server = new Server({ environment: "test" });
81
82 server.loadConfig(function () {
83 this.timing = 50;
84 });
85
86 expect(server.timing).toEqual(0);
87
88 server.shutdown();
89 });
90
91 test("doesn't modify user's timing config in other environments", () => {
92 let server = new Server({ environment: "blah" });
93
94 server.loadConfig(function () {
95 this.timing = 50;
96 });
97
98 expect(server.timing).toEqual(50);
99
100 server.shutdown();
101 });
102 });
103
104 describe("Unit | Server #db", function () {
105 test("its db is isolated across instances", () => {
106 let server1 = new Server({ environment: "test" });
107
108 server1.db.createCollection("contacts");
109 server1.db.contacts.insert({ name: "Sam" });
110
111 server1.shutdown();
112
113 let server2 = new Server({ environment: "test" });
114
115 expect(server2.contacts).toBeUndefined();
116
117 server2.shutdown();
118 });
119 });
120
121 describe("Unit | Server #create", function () {
122 test("create fails when no factories or models are registered", () => {
123 let server = new Server({ environment: "test" });
124
125 expect(function () {
126 server.create("contact");
127 }).toThrow(
128 "Mirage: You called server.create('contact') but no model or factory was found. Make sure you're passing in the singularized version of the model or factory name."
129 );
130
131 server.shutdown();
132 });
133
134 test("create fails when an expected factory isn't registered", () => {
135 let server = new Server({
136 environment: "test",
137 factories: {
138 address: Factory,
139 },
140 });
141
142 expect(function () {
143 server.create("contact");
144 }).toThrow(
145 "Mirage: You called server.create('contact') but no model or factory was found. Make sure you're passing in the singularized version of the model or factory name."
146 );
147
148 server.shutdown();
149 });
150
151 test("create works when models but no factories are registered", () => {
152 let server = new Server({
153 environment: "test",
154 models: {
155 contact: Model,
156 },
157 });
158
159 server.create("contact");
160
161 expect(server.db.contacts).toHaveLength(1);
162
163 server.shutdown();
164 });
165
166 test("create adds the data to the db", () => {
167 let server = new Server({
168 environment: "test",
169 factories: {
170 contact: Factory.extend({
171 name: "Sam",
172 }),
173 },
174 });
175
176 server.create("contact");
177 let contactsInDb = server.db.contacts;
178
179 expect(contactsInDb).toHaveLength(1);
180 expect(contactsInDb[0]).toEqual({ id: "1", name: "Sam" });
181
182 server.shutdown();
183 });
184
185 test("create returns the new data in the db", () => {
186 let server = new Server({
187 environment: "test",
188 factories: {
189 contact: Factory.extend({
190 name: "Sam",
191 }),
192 },
193 });
194
195 let contact = server.create("contact");
196
197 expect(contact).toEqual({ id: "1", name: "Sam" });
198
199 server.shutdown();
200 });
201
202 test("create allows for attr overrides", () => {
203 let server = new Server({
204 environment: "test",
205 factories: {
206 contact: Factory.extend({
207 name: "Sam",
208 }),
209 },
210 });
211
212 let sam = server.create("contact");
213 let link = server.create("contact", { name: "Link" });
214
215 expect(sam).toEqual({ id: "1", name: "Sam" });
216 expect(link).toEqual({ id: "2", name: "Link" });
217
218 server.shutdown();
219 });
220
221 test("create allows for attr overrides with extended factories", () => {
222 let ContactFactory = Factory.extend({
223 name: "Link",
224 age: 500,
225 });
226 let FriendFactory = ContactFactory.extend({
227 is_young() {
228 return this.age < 18;
229 },
230 });
231
232 let server = new Server({
233 environment: "test",
234 factories: {
235 contact: ContactFactory,
236 friend: FriendFactory,
237 },
238 });
239
240 let link = server.create("friend");
241 let youngLink = server.create("friend", { age: 10 });
242
243 expect(link).toEqual({ id: "1", name: "Link", age: 500, is_young: false });
244 expect(youngLink).toEqual({
245 id: "2",
246 name: "Link",
247 age: 10,
248 is_young: true,
249 });
250
251 server.shutdown();
252 });
253
254 test("create allows for attr overrides with arrays", () => {
255 let server = new Server({
256 environment: "test",
257 factories: {
258 contact: Factory.extend({
259 name: ["Sam", "Carl"],
260 }),
261 },
262 });
263
264 let sam = server.create("contact");
265 let link = server.create("contact", { name: ["Link"] });
266 let noname = server.create("contact", { name: [] });
267
268 expect(sam).toEqual({ id: "1", name: ["Sam", "Carl"] });
269 expect(link).toEqual({ id: "2", name: ["Link"] });
270 expect(noname).toEqual({ id: "3", name: [] });
271
272 server.shutdown();
273 });
274
275 test("create allows for nested attr overrides", () => {
276 let server = new Server({
277 environment: "test",
278 factories: {
279 contact: Factory.extend({
280 address: {
281 streetName: "Main",
282 streetAddress(i) {
283 return 1000 + i;
284 },
285 },
286 }),
287 },
288 });
289
290 let contact1 = server.create("contact");
291 let contact2 = server.create("contact");
292
293 expect(contact1).toEqual({
294 id: "1",
295 address: { streetName: "Main", streetAddress: 1000 },
296 });
297 expect(contact2).toEqual({
298 id: "2",
299 address: { streetName: "Main", streetAddress: 1001 },
300 });
301
302 server.shutdown();
303 });
304
305 test("factories can have dynamic properties that depend on attr overrides", () => {
306 let server = new Server({
307 environment: "test",
308 factories: {
309 baz: Factory.extend({
310 bar() {
311 return this.name.substr(1);
312 },
313 }),
314 },
315 });
316
317 let baz1 = server.create("baz", { name: "foo" });
318
319 expect(baz1).toEqual({ id: "1", name: "foo", bar: "oo" });
320
321 server.shutdown();
322 });
323
324 test("create allows for arrays of attr overrides", () => {
325 let server = new Server({
326 environment: "test",
327 factories: {
328 contact: Factory.extend({
329 websites: [
330 "http://example.com",
331 function (i) {
332 return `http://placekitten.com/${320 + i}/${240 + i}`;
333 },
334 ],
335 }),
336 },
337 });
338
339 let contact1 = server.create("contact");
340 let contact2 = server.create("contact");
341
342 expect(contact1).toEqual({
343 id: "1",
344 websites: ["http://example.com", "http://placekitten.com/320/240"],
345 });
346 expect(contact2).toEqual({
347 id: "2",
348 websites: ["http://example.com", "http://placekitten.com/321/241"],
349 });
350
351 server.shutdown();
352 });
353
354 test("create allows to extend factory with trait", () => {
355 let ArticleFactory = Factory.extend({
356 title: "Lorem ipsum",
357
358 published: trait({
359 isPublished: true,
360 publishedAt: "2010-01-01 10:00:00",
361 }),
362 });
363
364 let server = new Server({
365 environment: "test",
366 factories: {
367 article: ArticleFactory,
368 },
369 });
370
371 let article = server.create("article");
372 let publishedArticle = server.create("article", "published");
373
374 expect(article).toEqual({ id: "1", title: "Lorem ipsum" });
375 expect(publishedArticle).toEqual({
376 id: "2",
377 title: "Lorem ipsum",
378 isPublished: true,
379 publishedAt: "2010-01-01 10:00:00",
380 });
381
382 server.shutdown();
383 });
384
385 test("create allows to extend factory with multiple traits", () => {
386 let ArticleFactory = Factory.extend({
387 title: "Lorem ipsum",
388
389 published: trait({
390 isPublished: true,
391 publishedAt: "2010-01-01 10:00:00",
392 }),
393
394 withContent: trait({
395 content: "content",
396 }),
397 });
398
399 let server = new Server({
400 environment: "test",
401 factories: {
402 article: ArticleFactory,
403 },
404 });
405
406 let article = server.create("article");
407 let publishedArticle = server.create("article", "published");
408 let publishedArticleWithContent = server.create(
409 "article",
410 "published",
411 "withContent"
412 );
413
414 expect(article).toEqual({ id: "1", title: "Lorem ipsum" });
415 expect(publishedArticle).toEqual({
416 id: "2",
417 title: "Lorem ipsum",
418 isPublished: true,
419 publishedAt: "2010-01-01 10:00:00",
420 });
421 expect(publishedArticleWithContent).toEqual({
422 id: "3",
423 title: "Lorem ipsum",
424 isPublished: true,
425 publishedAt: "2010-01-01 10:00:00",
426 content: "content",
427 });
428
429 server.shutdown();
430 });
431
432 test("create allows to extend factory with traits containing afterCreate callbacks", () => {
433 let CommentFactory = Factory.extend({
434 content: "content",
435 });
436 let ArticleFactory = Factory.extend({
437 title: "Lorem ipsum",
438
439 withComments: trait({
440 afterCreate(article, server) {
441 server.createList("comment", 3, { article });
442 },
443 }),
444 });
445
446 let server = new Server({
447 environment: "test",
448 factories: {
449 article: ArticleFactory,
450 comment: CommentFactory,
451 },
452 });
453
454 let articleWithComments = server.create("article", "withComments");
455
456 expect(articleWithComments).toEqual({ id: "1", title: "Lorem ipsum" });
457 expect(server.db.comments).toHaveLength(3);
458
459 server.shutdown();
460 });
461
462 test("create does not execute afterCreate callbacks from traits that are not applied", () => {
463 let CommentFactory = Factory.extend({
464 content: "content",
465 });
466 let ArticleFactory = Factory.extend({
467 title: "Lorem ipsum",
468
469 withComments: trait({
470 afterCreate(article, server) {
471 server.createList("comment", 3, { article });
472 },
473 }),
474 });
475
476 let server = new Server({
477 environment: "test",
478 factories: {
479 article: ArticleFactory,
480 comment: CommentFactory,
481 },
482 });
483
484 let articleWithComments = server.create("article");
485
486 expect(articleWithComments).toEqual({ id: "1", title: "Lorem ipsum" });
487 expect(server.db.comments).toHaveLength(0);
488
489 server.shutdown();
490 });
491
492 test("create allows to extend with multiple traits and to apply attr overrides", () => {
493 let ArticleFactory = Factory.extend({
494 title: "Lorem ipsum",
495
496 published: trait({
497 isPublished: true,
498 publishedAt: "2010-01-01 10:00:00",
499 }),
500
501 withContent: trait({
502 content: "content",
503 }),
504 });
505
506 let server = new Server({
507 environment: "test",
508 factories: {
509 article: ArticleFactory,
510 },
511 });
512
513 let overrides = {
514 publishedAt: "2012-01-01 10:00:00",
515 };
516 let publishedArticleWithContent = server.create(
517 "article",
518 "published",
519 "withContent",
520 overrides
521 );
522
523 expect(publishedArticleWithContent).toEqual({
524 id: "1",
525 title: "Lorem ipsum",
526 isPublished: true,
527 publishedAt: "2012-01-01 10:00:00",
528 content: "content",
529 });
530
531 server.shutdown();
532 });
533
534 test("create throws errors when using trait that is not defined and distinquishes between traits and non-traits", () => {
535 let ArticleFactory = Factory.extend({
536 title: "Lorem ipsum",
537
538 published: trait({
539 isPublished: true,
540 publishedAt: "2010-01-01 10:00:00",
541 }),
542
543 private: {
544 someAttr: "value",
545 },
546 });
547
548 let server = new Server({
549 environment: "test",
550 factories: {
551 article: ArticleFactory,
552 },
553 });
554
555 expect(() => {
556 server.create("article", "private");
557 }).toThrow("'private' trait is not registered in 'article' factory");
558
559 server.shutdown();
560 });
561
562 test("create allows to create objects with associations", () => {
563 let AuthorFactory = Factory.extend({
564 name: "Sam",
565 });
566 let CategoryFactory = Factory.extend({
567 name: "splendid software",
568 });
569 let ArticleFactory = Factory.extend({
570 title: "Lorem ipsum",
571
572 withCategory: trait({
573 awesomeCategory: association(),
574 }),
575
576 author: association(),
577 });
578
579 let server = new Server({
580 environment: "test",
581 models: {
582 author: Model.extend({
583 articles: hasMany(),
584 }),
585 category: Model.extend({}),
586 article: Model.extend({
587 author: belongsTo(),
588 awesomeCategory: belongsTo("category"),
589 }),
590 },
591 factories: {
592 article: ArticleFactory,
593 author: AuthorFactory,
594 category: CategoryFactory,
595 },
596 });
597
598 let article = server.create("article", "withCategory");
599
600 expect(article.attrs).toEqual({
601 title: "Lorem ipsum",
602 id: "1",
603 authorId: "1",
604 awesomeCategoryId: "1",
605 });
606 expect(server.db.authors).toHaveLength(1);
607 expect(server.db.categories).toHaveLength(1);
608
609 let anotherArticle = server.create("article", "withCategory");
610 expect(anotherArticle.attrs).toEqual({
611 title: "Lorem ipsum",
612 id: "2",
613 authorId: "2",
614 awesomeCategoryId: "2",
615 });
616 expect(server.db.authors).toHaveLength(2);
617 expect(server.db.categories).toHaveLength(2);
618
619 server.shutdown();
620 });
621
622 test("create allows to create objects with associations with traits and overrides for associations", () => {
623 let CategoryFactory = Factory.extend({
624 name: "splendid software",
625
626 published: trait({
627 isPublished: true,
628 publishedAt: "2014-01-01 10:00:00",
629 }),
630 });
631 let ArticleFactory = Factory.extend({
632 title: "Lorem ipsum",
633
634 withCategory: trait({
635 category: association("published", {
636 publishedAt: "2016-01-01 12:00:00",
637 }),
638 }),
639 });
640
641 let server = new Server({
642 environment: "test",
643 factories: {
644 article: ArticleFactory,
645 category: CategoryFactory,
646 },
647 models: {
648 category: Model.extend({}),
649 article: Model.extend({
650 category: belongsTo("category"),
651 }),
652 },
653 });
654
655 let article = server.create("article", "withCategory");
656
657 expect(article.attrs).toEqual({
658 title: "Lorem ipsum",
659 id: "1",
660 categoryId: "1",
661 });
662 expect(server.db.categories).toHaveLength(1);
663 expect(server.db.categories[0]).toEqual({
664 name: "splendid software",
665 id: "1",
666 isPublished: true,
667 publishedAt: "2016-01-01 12:00:00",
668 });
669
670 server.shutdown();
671 });
672
673 test("create does not create (extra) models on associations when they are passed in as overrides", () => {
674 let MotherFactory = Factory.extend({
675 name: "Should not create",
676 });
677 let ChildFactory = Factory.extend({
678 mother: association(),
679 });
680
681 let server = new Server({
682 environment: "test",
683 factories: {
684 mother: MotherFactory,
685 child: ChildFactory,
686 },
687 models: {
688 mother: Model.extend({
689 children: hasMany("child"),
690 }),
691 child: Model.extend({
692 mother: belongsTo("mother"),
693 }),
694 },
695 });
696
697 let mother = server.create("mother", { name: "Lynda" });
698 server.create("child", { name: "Don", mother });
699 server.create("child", { name: "Dan", mother });
700
701 expect(server.db.mothers).toHaveLength(1);
702
703 server.shutdown();
704 });
705 });
706
707 describe("Unit | Server #createList", function () {
708 let server = null;
709 beforeEach(function () {
710 server = new Server({ environment: "test" });
711 });
712
713 afterEach(function () {
714 server.shutdown();
715 });
716
717 test("createList adds the given number of elements to the db", () => {
718 server.loadFactories({
719 contact: Factory.extend({ name: "Sam" }),
720 });
721
722 server.createList("contact", 3);
723 let contactsInDb = server.db.contacts;
724
725 expect(contactsInDb).toHaveLength(3);
726 expect(contactsInDb[0]).toEqual({ id: "1", name: "Sam" });
727 expect(contactsInDb[1]).toEqual({ id: "2", name: "Sam" });
728 expect(contactsInDb[2]).toEqual({ id: "3", name: "Sam" });
729 });
730
731 test("createList returns the created elements", () => {
732 server.loadFactories({
733 contact: Factory.extend({ name: "Sam" }),
734 });
735
736 server.create("contact");
737 let contacts = server.createList("contact", 3);
738
739 expect(contacts).toHaveLength(3);
740 expect(contacts[0]).toEqual({ id: "2", name: "Sam" });
741 expect(contacts[1]).toEqual({ id: "3", name: "Sam" });
742 expect(contacts[2]).toEqual({ id: "4", name: "Sam" });
743 });
744
745 test("createList respects sequences", () => {
746 server.loadFactories({
747 contact: Factory.extend({
748 name(i) {
749 return `name${i}`;
750 },
751 }),
752 });
753
754 let contacts = server.createList("contact", 3);
755
756 expect(contacts[0]).toEqual({ id: "1", name: "name0" });
757 expect(contacts[1]).toEqual({ id: "2", name: "name1" });
758 expect(contacts[2]).toEqual({ id: "3", name: "name2" });
759 });
760
761 test("createList respects attr overrides", () => {
762 server.loadFactories({
763 contact: Factory.extend({ name: "Sam" }),
764 });
765
766 let sams = server.createList("contact", 2);
767 let links = server.createList("contact", 2, { name: "Link" });
768
769 expect(sams[0]).toEqual({ id: "1", name: "Sam" });
770 expect(sams[1]).toEqual({ id: "2", name: "Sam" });
771 expect(links[0]).toEqual({ id: "3", name: "Link" });
772 expect(links[1]).toEqual({ id: "4", name: "Link" });
773 });
774
775 test("createList respects traits", () => {
776 let ArticleFactory = Factory.extend({
777 title: "Lorem ipsum",
778
779 published: trait({
780 isPublished: true,
781 publishedAt: "2010-01-01 10:00:00",
782 }),
783
784 withContent: trait({
785 content: "content",
786 }),
787 });
788
789 server.loadFactories({
790 article: ArticleFactory,
791 });
792
793 let articles = server.createList("article", 2, "published", "withContent");
794
795 expect(articles[0]).toEqual({
796 id: "1",
797 title: "Lorem ipsum",
798 isPublished: true,
799 publishedAt: "2010-01-01 10:00:00",
800 content: "content",
801 });
802 expect(articles[1]).toEqual({
803 id: "2",
804 title: "Lorem ipsum",
805 isPublished: true,
806 publishedAt: "2010-01-01 10:00:00",
807 content: "content",
808 });
809 });
810
811 test("createList respects traits with attr overrides", () => {
812 let ArticleFactory = Factory.extend({
813 title: "Lorem ipsum",
814
815 published: trait({
816 isPublished: true,
817 publishedAt: "2010-01-01 10:00:00",
818 }),
819
820 withContent: trait({
821 content: "content",
822 }),
823 });
824
825 server.loadFactories({
826 article: ArticleFactory,
827 });
828
829 let overrides = { publishedAt: "2012-01-01 10:00:00" };
830 let articles = server.createList(
831 "article",
832 2,
833 "published",
834 "withContent",
835 overrides
836 );
837
838 expect(articles[0]).toEqual({
839 id: "1",
840 title: "Lorem ipsum",
841 isPublished: true,
842 publishedAt: "2012-01-01 10:00:00",
843 content: "content",
844 });
845 expect(articles[1]).toEqual({
846 id: "2",
847 title: "Lorem ipsum",
848 isPublished: true,
849 publishedAt: "2012-01-01 10:00:00",
850 content: "content",
851 });
852 });
853
854 test("createList throws errors when using trait that is not defined and distinquishes between traits and non-traits", () => {
855 let ArticleFactory = Factory.extend({
856 title: "Lorem ipsum",
857
858 published: trait({
859 isPublished: true,
860 publishedAt: "2010-01-01 10:00:00",
861 }),
862
863 private: {
864 someAttr: "value",
865 },
866 });
867
868 server.loadFactories({
869 article: ArticleFactory,
870 });
871
872 expect(() => {
873 server.createList("article", 2, "private");
874 }).toThrow("'private' trait is not registered in 'article' factory");
875 });
876
877 test("createList throws an error if the second argument is not an integer", () => {
878 let ArticleFactory = Factory.extend({
879 title: "Lorem ipsum",
880
881 published: trait({
882 isPublished: true,
883 publishedAt: "2010-01-01 10:00:00",
884 }),
885 });
886
887 server.loadFactories({
888 article: ArticleFactory,
889 });
890
891 expect(() => {
892 server.createList("article", "published");
893 }).toThrow(
894 "Mirage: second argument has to be an integer, you passed: string"
895 );
896 });
897 });
898
899 describe("Unit | Server #build", function () {
900 let server = null;
901 beforeEach(function () {
902 server = new Server({ environment: "test" });
903 });
904
905 afterEach(function () {
906 server.shutdown();
907 });
908
909 test("build does not add the data to the db", () => {
910 server.loadFactories({
911 contact: Factory.extend({ name: "Sam" }),
912 });
913
914 server.build("contact");
915 let contactsInDb = server.db.contacts;
916
917 expect(contactsInDb).toHaveLength(0);
918 });
919
920 test("build returns the new attrs with no id", () => {
921 server.loadFactories({
922 contact: Factory.extend({ name: "Sam" }),
923 });
924
925 let contact = server.build("contact");
926
927 expect(contact).toEqual({ name: "Sam" });
928 });
929
930 test("build allows for attr overrides", () => {
931 server.loadFactories({
932 contact: Factory.extend({ name: "Sam" }),
933 });
934
935 let sam = server.build("contact");
936 let link = server.build("contact", { name: "Link" });
937
938 expect(sam).toEqual({ name: "Sam" });
939 expect(link).toEqual({ name: "Link" });
940 });
941
942 test("build allows for attr overrides with extended factories", () => {
943 let ContactFactory = Factory.extend({
944 name: "Link",
945 age: 500,
946 });
947 let FriendFactory = ContactFactory.extend({
948 is_young() {
949 return this.age < 18;
950 },
951 });
952 server.loadFactories({
953 contact: ContactFactory,
954 friend: FriendFactory,
955 });
956
957 let link = server.build("friend");
958 let youngLink = server.build("friend", { age: 10 });
959
960 expect(link).toEqual({ name: "Link", age: 500, is_young: false });
961 expect(youngLink).toEqual({ name: "Link", age: 10, is_young: true });
962 });
963
964 test("build allows for attr overrides with arrays", () => {
965 server.loadFactories({
966 contact: Factory.extend({ name: ["Sam", "Carl"] }),
967 });
968
969 let sam = server.build("contact");
970 let link = server.build("contact", { name: ["Link"] });
971 let noname = server.build("contact", { name: [] });
972
973 expect(sam).toEqual({ name: ["Sam", "Carl"] });
974 expect(link).toEqual({ name: ["Link"] });
975 expect(noname).toEqual({ name: [] });
976 });
977
978 test("build allows for nested attr overrides", () => {
979 server.loadFactories({
980 contact: Factory.extend({
981 address: {
982 streetName: "Main",
983 streetAddress(i) {
984 return 1000 + i;
985 },
986 },
987 }),
988 });
989
990 let contact1 = server.build("contact");
991 let contact2 = server.build("contact");
992
993 expect(contact1).toEqual({
994 address: { streetName: "Main", streetAddress: 1000 },
995 });
996 expect(contact2).toEqual({
997 address: { streetName: "Main", streetAddress: 1001 },
998 });
999 });
1000
1001 test("build allows for arrays of attr overrides", () => {
1002 server.loadFactories({
1003 contact: Factory.extend({
1004 websites: [
1005 "http://example.com",
1006 function (i) {
1007 return `http://placekitten.com/${320 + i}/${240 + i}`;
1008 },
1009 ],
1010 }),
1011 });
1012
1013 let contact1 = server.build("contact");
1014 let contact2 = server.build("contact");
1015
1016 expect(contact1).toEqual({
1017 websites: ["http://example.com", "http://placekitten.com/320/240"],
1018 });
1019 expect(contact2).toEqual({
1020 websites: ["http://example.com", "http://placekitten.com/321/241"],
1021 });
1022 });
1023
1024 test("build allows to extend factory with trait", () => {
1025 let ArticleFactory = Factory.extend({
1026 title: "Lorem ipsum",
1027
1028 published: trait({
1029 isPublished: true,
1030 publishedAt: "2010-01-01 10:00:00",
1031 }),
1032 });
1033
1034 server.loadFactories({
1035 article: ArticleFactory,
1036 });
1037
1038 let article = server.build("article");
1039 let publishedArticle = server.build("article", "published");
1040
1041 expect(article).toEqual({ title: "Lorem ipsum" });
1042 expect(publishedArticle).toEqual({
1043 title: "Lorem ipsum",
1044 isPublished: true,
1045 publishedAt: "2010-01-01 10:00:00",
1046 });
1047 });
1048
1049 test("build allows to extend factory with multiple traits", () => {
1050 let ArticleFactory = Factory.extend({
1051 title: "Lorem ipsum",
1052
1053 published: trait({
1054 isPublished: true,
1055 publishedAt: "2010-01-01 10:00:00",
1056 }),
1057
1058 withContent: trait({
1059 content: "content",
1060 }),
1061 });
1062
1063 server.loadFactories({
1064 article: ArticleFactory,
1065 });
1066
1067 let article = server.build("article");
1068 let publishedArticle = server.build("article", "published");
1069 let publishedArticleWithContent = server.build(
1070 "article",
1071 "published",
1072 "withContent"
1073 );
1074
1075 expect(article).toEqual({ title: "Lorem ipsum" });
1076 expect(publishedArticle).toEqual({
1077 title: "Lorem ipsum",
1078 isPublished: true,
1079 publishedAt: "2010-01-01 10:00:00",
1080 });
1081 expect(publishedArticleWithContent).toEqual({
1082 title: "Lorem ipsum",
1083 isPublished: true,
1084 publishedAt: "2010-01-01 10:00:00",
1085 content: "content",
1086 });
1087 });
1088
1089 test("build allows to extend with multiple traits and to apply attr overrides", () => {
1090 let ArticleFactory = Factory.extend({
1091 title: "Lorem ipsum",
1092
1093 published: trait({
1094 isPublished: true,
1095 publishedAt: "2010-01-01 10:00:00",
1096 }),
1097
1098 withContent: trait({
1099 content: "content",
1100 }),
1101 });
1102
1103 server.loadFactories({
1104 article: ArticleFactory,
1105 });
1106
1107 let overrides = {
1108 publishedAt: "2012-01-01 10:00:00",
1109 };
1110 let publishedArticleWithContent = server.build(
1111 "article",
1112 "published",
1113 "withContent",
1114 overrides
1115 );
1116
1117 expect(publishedArticleWithContent).toEqual({
1118 title: "Lorem ipsum",
1119 isPublished: true,
1120 publishedAt: "2012-01-01 10:00:00",
1121 content: "content",
1122 });
1123 });
1124
1125 test("build allows to build objects with associations", () => {
1126 let AuthorFactory = Factory.extend({
1127 name: "Yehuda",
1128 });
1129 let CategoryFactory = Factory.extend({
1130 name: "splendid software",
1131 });
1132 let ArticleFactory = Factory.extend({
1133 title: "Lorem ipsum",
1134
1135 withCategory: trait({
1136 awesomeCategory: association(),
1137 }),
1138
1139 someOtherTrait: trait({
1140 user: association(),
1141 }),
1142
1143 author: association(),
1144 });
1145
1146 server.loadFactories({
1147 article: ArticleFactory,
1148 author: AuthorFactory,
1149 category: CategoryFactory,
1150 });
1151 server.schema.registerModels({
1152 author: Model.extend({
1153 articles: hasMany(),
1154 }),
1155 category: Model.extend({}),
1156 article: Model.extend({
1157 author: belongsTo(),
1158 awesomeCategory: belongsTo("category"),
1159 }),
1160 });
1161
1162 let article = server.build("article", "withCategory");
1163
1164 expect(article).toEqual({
1165 title: "Lorem ipsum",
1166 authorId: "1",
1167 awesomeCategoryId: "1",
1168 });
1169 expect(server.db.authors).toHaveLength(1);
1170 expect(server.db.categories).toHaveLength(1);
1171 });
1172
1173 test("build allows to build objects with associations with traits and overrides for associations", () => {
1174 let CategoryFactory = Factory.extend({
1175 name: "splendid software",
1176
1177 published: trait({
1178 isPublished: true,
1179 publishedAt: "2014-01-01 10:00:00",
1180 }),
1181 });
1182 let ArticleFactory = Factory.extend({
1183 title: "Lorem ipsum",
1184
1185 withCategory: trait({
1186 category: association("published", {
1187 publishedAt: "2016-01-01 12:00:00",
1188 }),
1189 }),
1190 });
1191
1192 server.config({
1193 factories: {
1194 article: ArticleFactory,
1195 category: CategoryFactory,
1196 },
1197 models: {
1198 category: Model.extend({}),
1199 article: Model.extend({
1200 category: belongsTo(),
1201 }),
1202 },
1203 });
1204
1205 let article = server.build("article", "withCategory");
1206
1207 expect(article).toEqual({ title: "Lorem ipsum", categoryId: "1" });
1208 expect(server.db.categories).toHaveLength(1);
1209 expect(server.db.categories[0]).toEqual({
1210 name: "splendid software",
1211 id: "1",
1212 isPublished: true,
1213 publishedAt: "2016-01-01 12:00:00",
1214 });
1215 });
1216
1217 test("build throws errors when using trait that is not defined and distinquishes between traits and non-traits", () => {
1218 server.config({
1219 factories: {
1220 article: Factory.extend({
1221 title: "Lorem ipsum",
1222
1223 published: trait({
1224 isPublished: true,
1225 publishedAt: "2010-01-01 10:00:00",
1226 }),
1227
1228 private: {
1229 someAttr: "value",
1230 },
1231 }),
1232 },
1233 });
1234
1235 expect(() => {
1236 server.build("article", "private");
1237 }).toThrow("'private' trait is not registered in 'article' factory");
1238 });
1239
1240 test("build does not build objects and throws error if model is not registered and association helper is used", () => {
1241 server.config({
1242 factories: {
1243 article: Factory.extend({
1244 title: "Lorem ipsum",
1245
1246 withCategory: trait({
1247 category: association("published", {
1248 publishedAt: "2016-01-01 12:00:00",
1249 }),
1250 }),
1251 }),
1252 category: Factory.extend({
1253 name: "splendid software",
1254
1255 published: trait({
1256 isPublished: true,
1257 publishedAt: "2014-01-01 10:00:00",
1258 }),
1259 }),
1260 },
1261 models: {
1262 category: Model.extend(),
1263 },
1264 });
1265
1266 expect(() => {
1267 server.build("article", "withCategory");
1268 }).toThrow("Mirage: Model not registered: article");
1269 });
1270
1271 test("build does not build objects and throws error if model for given association is not registered", () => {
1272 server.config({
1273 factories: {
1274 article: Factory.extend({
1275 title: "Lorem ipsum",
1276
1277 withCategory: trait({
1278 category: association("published", {
1279 publishedAt: "2016-01-01 12:00:00",
1280 }),
1281 }),
1282 }),
1283 category: Factory.extend({
1284 name: "splendid software",
1285
1286 published: trait({
1287 isPublished: true,
1288 publishedAt: "2014-01-01 10:00:00",
1289 }),
1290 }),
1291 },
1292 models: {
1293 article: Model.extend(),
1294 },
1295 });
1296
1297 expect(() => {
1298 server.build("article", "withCategory");
1299 }).toThrow(
1300 "Mirage: You're using the `association` factory helper on the 'category' attribute of your article factory, but that attribute is not a `belongsTo` association."
1301 );
1302 });
1303 });
1304
1305 describe("Unit | Server #buildList", function () {
1306 let server = null;
1307 beforeEach(function () {
1308 server = new Server({ environment: "test" });
1309 });
1310
1311 afterEach(function () {
1312 server.shutdown();
1313 });
1314
1315 test("buildList does not add elements to the db", () => {
1316 server.loadFactories({
1317 contact: Factory.extend({ name: "Sam" }),
1318 });
1319
1320 server.buildList("contact", 3);
1321 let contactsInDb = server.db.contacts;
1322
1323 expect(contactsInDb).toHaveLength(0);
1324 });
1325
1326 test("buildList returns the built elements without ids", () => {
1327 server.loadFactories({
1328 contact: Factory.extend({ name: "Sam" }),
1329 });
1330
1331 server.create("contact");
1332 let contacts = server.buildList("contact", 3);
1333
1334 expect(contacts).toHaveLength(3);
1335 expect(contacts[0]).toEqual({ name: "Sam" });
1336 expect(contacts[1]).toEqual({ name: "Sam" });
1337 expect(contacts[2]).toEqual({ name: "Sam" });
1338 });
1339
1340 test("buildList respects sequences", () => {
1341 server.loadFactories({
1342 contact: Factory.extend({
1343 name(i) {
1344 return `name${i}`;
1345 },
1346 }),
1347 });
1348
1349 let contacts = server.buildList("contact", 3);
1350
1351 expect(contacts[0]).toEqual({ name: "name0" });
1352 expect(contacts[1]).toEqual({ name: "name1" });
1353 expect(contacts[2]).toEqual({ name: "name2" });
1354 });
1355
1356 test("buildList respects attr overrides", () => {
1357 server.loadFactories({
1358 contact: Factory.extend({ name: "Sam" }),
1359 });
1360
1361 let sams = server.buildList("contact", 2);
1362 let links = server.buildList("contact", 2, { name: "Link" });
1363
1364 expect(sams[0]).toEqual({ name: "Sam" });
1365 expect(sams[1]).toEqual({ name: "Sam" });
1366 expect(links[0]).toEqual({ name: "Link" });
1367 expect(links[1]).toEqual({ name: "Link" });
1368 });
1369
1370 test("buildList respects traits", () => {
1371 let ArticleFactory = Factory.extend({
1372 title: "Lorem ipsum",
1373
1374 published: trait({
1375 isPublished: true,
1376 publishedAt: "2010-01-01 10:00:00",
1377 }),
1378
1379 withContent: trait({
1380 content: "content",
1381 }),
1382 });
1383
1384 server.loadFactories({
1385 article: ArticleFactory,
1386 });
1387
1388 let articles = server.buildList("article", 2, "published", "withContent");
1389
1390 expect(articles[0]).toEqual({
1391 title: "Lorem ipsum",
1392 isPublished: true,
1393 publishedAt: "2010-01-01 10:00:00",
1394 content: "content",
1395 });
1396 expect(articles[1]).toEqual({
1397 title: "Lorem ipsum",
1398 isPublished: true,
1399 publishedAt: "2010-01-01 10:00:00",
1400 content: "content",
1401 });
1402 });
1403
1404 test("buildList respects traits with attr overrides", () => {
1405 let ArticleFactory = Factory.extend({
1406 title: "Lorem ipsum",
1407
1408 published: trait({
1409 isPublished: true,
1410 publishedAt: "2010-01-01 10:00:00",
1411 }),
1412
1413 withContent: trait({
1414 content: "content",
1415 }),
1416 });
1417
1418 server.loadFactories({
1419 article: ArticleFactory,
1420 });
1421
1422 let overrides = { publishedAt: "2012-01-01 10:00:00" };
1423 let articles = server.buildList(
1424 "article",
1425 2,
1426 "published",
1427 "withContent",
1428 overrides
1429 );
1430
1431 expect(articles[0]).toEqual({
1432 title: "Lorem ipsum",
1433 isPublished: true,
1434 publishedAt: "2012-01-01 10:00:00",
1435 content: "content",
1436 });
1437 expect(articles[1]).toEqual({
1438 title: "Lorem ipsum",
1439 isPublished: true,
1440 publishedAt: "2012-01-01 10:00:00",
1441 content: "content",
1442 });
1443 });
1444
1445 test("buildList throws errors when using trait that is not defined and distinquishes between traits and non-traits", () => {
1446 let ArticleFactory = Factory.extend({
1447 title: "Lorem ipsum",
1448
1449 published: trait({
1450 isPublished: true,
1451 publishedAt: "2010-01-01 10:00:00",
1452 }),
1453
1454 private: {
1455 someAttr: "value",
1456 },
1457 });
1458
1459 server.loadFactories({
1460 article: ArticleFactory,
1461 });
1462
1463 expect(() => {
1464 server.buildList("article", 2, "private");
1465 }).toThrow("'private' trait is not registered in 'article' factory");
1466 });
1467
1468 test("buildList throws an error if the second argument is not an integer", function () {
1469 let ArticleFactory = Factory.extend({
1470 title: "Lorem ipsum",
1471
1472 published: trait({
1473 isPublished: true,
1474 publishedAt: "2010-01-01 10:00:00",
1475 }),
1476 });
1477
1478 server.loadFactories({
1479 article: ArticleFactory,
1480 });
1481
1482 expect(() => {
1483 server.buildList("article", "published");
1484 }).toThrow(
1485 "Mirage: second argument has to be an integer, you passed: string"
1486 );
1487 });
1488 });