Debian Codegraph @

__tests__/external/shared/factories/helpers-test.js

raw
1 import {
2 Model,
3 Server,
4 Factory,
5 belongsTo,
6 hasMany,
7 trait,
8 association,
9 } from "miragejs";
10
11 describe("Eternal | Shared | Factories | helpers", () => {
12 let server;
13
14 afterEach(() => {
15 server.shutdown();
16 });
17
18 test('it creates associations with "association" helper in a dasherized factory', () => {
19 server = new Server({
20 environment: "test",
21 models: {
22 author: Model.extend({
23 blogPosts: hasMany(),
24 }),
25 blogPost: Model.extend({
26 author: belongsTo(),
27 }),
28 },
29 factories: {
30 author: Factory.extend({
31 name: "Sam",
32 }),
33 blogPost: Factory.extend({
34 title: "Lorem ipsum",
35
36 author: association(),
37 }),
38 },
39 });
40
41 let blogPost = server.create("blog-post");
42
43 expect(blogPost.author).toBeTruthy();
44
45 let { db } = server;
46
47 expect(db.authors).toHaveLength(1);
48 expect(db.authors[0]).toEqual({
49 id: "1",
50 name: "Sam",
51 blogPostIds: ["1"],
52 });
53 });
54
55 test('it creates associations with "association" helper combininig with traits', () => {
56 server = new Server({
57 environment: "test",
58 models: {
59 author: Model.extend({
60 posts: hasMany(),
61 }),
62 category: Model.extend({
63 posts: hasMany("post", { inverse: "kind" }),
64 }),
65 post: Model.extend({
66 author: belongsTo(),
67 kind: belongsTo("category"),
68 }),
69 },
70 factories: {
71 author: Factory.extend({
72 name: "Sam",
73 }),
74 category: Factory.extend({
75 name: "awesome software",
76 }),
77 post: Factory.extend({
78 title: "Lorem ipsum",
79
80 author: association(),
81
82 withCategory: trait({
83 kind: association(),
84 }),
85 }),
86 },
87 });
88
89 let post = server.create("post", "withCategory");
90
91 expect(post.kind).toBeTruthy();
92 expect(post.author).toBeTruthy();
93
94 let { db } = server;
95
96 expect(db.posts).toHaveLength(1);
97 expect(db.posts[0]).toEqual({
98 id: "1",
99 title: "Lorem ipsum",
100 authorId: "1",
101 kindId: "1",
102 });
103
104 expect(db.authors).toHaveLength(1);
105 expect(db.authors[0]).toEqual({
106 id: "1",
107 name: "Sam",
108 postIds: ["1"],
109 });
110
111 expect(db.categories).toHaveLength(1);
112 expect(db.categories[0]).toEqual({
113 id: "1",
114 name: "awesome software",
115 postIds: ["1"],
116 });
117 });
118
119 test("it throws if using the association helper on a self-referential belongsTo relationship", () => {
120 server = new Server({
121 environment: "test",
122 models: {
123 page: Model.extend({
124 parentPage: belongsTo("page", { inverse: "childPages" }),
125 childPages: hasMany("page", { inverse: "parentPage" }),
126 }),
127 },
128 factories: {
129 page: Factory.extend({
130 parentPage: association(),
131 }),
132 },
133 });
134
135 expect(() => {
136 server.create("page");
137 }).toThrow();
138 });
139
140 test("it throws if using the association helper with polymorphic relationship", () => {
141 server = new Server({
142 environment: "test",
143 models: {
144 author: Model.extend({
145 anyPost: belongsTo("base-post", { polymorphic: true }),
146 }),
147 basePost: Model.extend({}),
148 },
149 factories: {
150 author: Factory.extend({
151 anyPost: association(),
152 }),
153 },
154 });
155
156 expect(() => {
157 server.create("author");
158 }).toThrow();
159 });
160 });