Debian Codegraph @

__tests__/external/shared/orm/model-associations-test.js

raw
1 import { Server, Model, belongsTo, hasMany } from "miragejs";
2
3 describe("External | Shared | ORM | Model | associations", () => {
4 let server;
5 let post;
6
7 beforeEach(() => {
8 server = new Server({
9 environment: "test",
10 models: {
11 user: Model,
12 post: Model.extend({
13 user: belongsTo(),
14 comments: hasMany(),
15 }),
16 comment: Model,
17 },
18 });
19
20 let peter = server.create("user", { name: "Peter" });
21 post = server.create("post", { user: peter });
22 });
23
24 afterEach(() => {
25 server.shutdown();
26 });
27
28 test("associations returns a hashmap of the model's associations", function () {
29 let postAssociations = post.associations;
30
31 expect(Object.keys(postAssociations)).toEqual(["user", "comments"]);
32 expect(postAssociations.user.name).toEqual("user");
33 expect(postAssociations.user.modelName).toEqual("user");
34 expect(postAssociations.user.type).toEqual("belongsTo");
35 expect(postAssociations.comments.name).toEqual("comments");
36 expect(postAssociations.comments.modelName).toEqual("comment");
37 expect(postAssociations.comments.type).toEqual("hasMany");
38 });
39 });