Debian Codegraph @

__tests__/external/shared/orm/where-test.js

raw
1 import { Server, Model, Collection } from "miragejs";
2
3 describe("External | Shared | ORM | #where", () => {
4 let server;
5 let User;
6
7 beforeEach(() => {
8 User = Model.extend();
9 server = new Server({
10 environment: "test",
11 models: {
12 user: User,
13 },
14 });
15
16 server.db.loadData({
17 users: [
18 { id: 1, name: "Link", good: true },
19 { id: 2, name: "Zelda", good: true },
20 { id: 3, name: "Ganon", good: false },
21 ],
22 });
23 });
24
25 afterEach(() => {
26 server.shutdown();
27 });
28
29 test("it returns models that match a query with where", () => {
30 let users = server.schema.users.where({ good: false });
31
32 expect(users instanceof Collection).toBeTruthy();
33 expect(users.models).toHaveLength(1);
34 expect(users.models[0] instanceof User).toBeTruthy();
35 expect(users.models[0].attrs).toEqual({
36 id: "3",
37 name: "Ganon",
38 good: false,
39 });
40 });
41
42 test("it returns models that match using a query function", () => {
43 let users = server.schema.users.where(function (rec) {
44 return !rec.good;
45 });
46
47 expect(users instanceof Collection).toBeTruthy();
48 expect(users.models).toHaveLength(1);
49 expect(users.models[0] instanceof User).toBeTruthy();
50 expect(users.models[0].attrs).toEqual({
51 id: "3",
52 name: "Ganon",
53 good: false,
54 });
55 });
56
57 test("it returns an empty collection if no models match a query", () => {
58 let users = server.schema.users.where({ name: "Link", good: false });
59
60 expect(users instanceof Collection).toBeTruthy();
61 expect(users.models).toHaveLength(0);
62 });
63 });