Debian Codegraph @

__tests__/external/shared/orm/belongs-to/7-named-one-way-reflexive/create-test.js

raw
1 import Helper from "./_helper";
2 import { Model } from "miragejs";
3
4 describe("External | Shared | ORM | Belongs To | Named One-Way Reflexive | create", () => {
5 let helper;
6 beforeEach(() => {
7 helper = new Helper();
8 helper.schema.registerModel("foo", Model);
9 });
10 afterEach(() => {
11 helper.shutdown();
12 });
13
14 test("it sets up associations correctly when passing in the foreign key", () => {
15 let { schema } = helper;
16 let parent = schema.create("user");
17 let child = schema.create("user", {
18 parentId: parent.id,
19 });
20
21 expect(child.parentId).toEqual(parent.id);
22 expect(child.parent.attrs).toEqual(parent.attrs);
23 expect(schema.db.users).toHaveLength(2);
24 expect(schema.db.users[0]).toEqual({ id: "1", parentId: null });
25 expect(schema.db.users[1]).toEqual({ id: "2", parentId: "1" });
26 });
27
28 test("it sets up associations correctly when passing in the association itself", () => {
29 let { schema } = helper;
30 let parent = schema.create("user");
31 let child = schema.create("user", {
32 parent,
33 });
34
35 expect(child.parentId).toEqual(parent.id);
36 expect(child.parent.attrs).toEqual(parent.attrs);
37 expect(schema.db.users).toHaveLength(2);
38 expect(schema.db.users[0]).toEqual({ id: "1", parentId: null });
39 expect(schema.db.users[1]).toEqual({ id: "2", parentId: "1" });
40 });
41
42 test("it throws an error if a model is passed in without a defined relationship", () => {
43 let { schema } = helper;
44
45 expect(function () {
46 schema.create("user", {
47 foo: schema.create("foo"),
48 });
49 }).toThrow();
50 });
51
52 test("it throws an error if a collection is passed in without a defined relationship", () => {
53 let { schema } = helper;
54 schema.create("foo");
55 schema.create("foo");
56
57 expect(function () {
58 schema.create("user", {
59 foos: schema.foos.all(),
60 });
61 }).toThrow();
62 });
63 });