| 1 |
import { Server, Model } from "miragejs"; |
| 2 |
|
| 3 |
describe("External | Shared | ORM | #update", () => { |
| 4 |
let server; |
| 5 |
|
| 6 |
beforeEach(() => { |
| 7 |
server = new Server({ |
| 8 |
environment: "test", |
| 9 |
models: { |
| 10 |
user: Model, |
| 11 |
}, |
| 12 |
}); |
| 13 |
|
| 14 |
server.db.loadData({ |
| 15 |
users: [ |
| 16 |
{ id: 1, name: "Link", location: "Hyrule", evil: false }, |
| 17 |
{ id: 2, name: "Zelda", location: "Hyrule", evil: false }, |
| 18 |
], |
| 19 |
}); |
| 20 |
}); |
| 21 |
|
| 22 |
afterEach(() => { |
| 23 |
server.shutdown(); |
| 24 |
}); |
| 25 |
|
| 26 |
test("a collection can update its models with a key and value", () => { |
| 27 |
let collection = server.schema.users.all(); |
| 28 |
collection.update("evil", true); |
| 29 |
|
| 30 |
expect(server.db.users).toIncludeSameMembers([ |
| 31 |
{ id: "1", name: "Link", location: "Hyrule", evil: true }, |
| 32 |
{ id: "2", name: "Zelda", location: "Hyrule", evil: true }, |
| 33 |
]); |
| 34 |
expect(collection.models[0].attrs).toEqual({ |
| 35 |
id: "1", |
| 36 |
name: "Link", |
| 37 |
location: "Hyrule", |
| 38 |
evil: true, |
| 39 |
}); |
| 40 |
}); |
| 41 |
|
| 42 |
test("it can update its models with a hash of attrs", () => { |
| 43 |
let collection = server.schema.users.all(); |
| 44 |
collection.update({ location: "The water temple", evil: true }); |
| 45 |
|
| 46 |
expect(server.db.users).toIncludeSameMembers([ |
| 47 |
{ id: "1", name: "Link", location: "The water temple", evil: true }, |
| 48 |
{ id: "2", name: "Zelda", location: "The water temple", evil: true }, |
| 49 |
]); |
| 50 |
expect(collection.models[0].attrs).toEqual({ |
| 51 |
id: "1", |
| 52 |
name: "Link", |
| 53 |
location: "The water temple", |
| 54 |
evil: true, |
| 55 |
}); |
| 56 |
expect(collection.models[1].attrs).toEqual({ |
| 57 |
id: "2", |
| 58 |
name: "Zelda", |
| 59 |
location: "The water temple", |
| 60 |
evil: true, |
| 61 |
}); |
| 62 |
}); |
| 63 |
|
| 64 |
test("it can set an attribute and then save the model", () => { |
| 65 |
let user = server.schema.users.find(1); |
| 66 |
|
| 67 |
user.name = "Young link"; |
| 68 |
|
| 69 |
expect(user.attrs).toEqual({ |
| 70 |
id: "1", |
| 71 |
name: "Young link", |
| 72 |
location: "Hyrule", |
| 73 |
evil: false, |
| 74 |
}); |
| 75 |
expect(server.db.users.find(1)).toEqual({ |
| 76 |
id: "1", |
| 77 |
name: "Link", |
| 78 |
location: "Hyrule", |
| 79 |
evil: false, |
| 80 |
}); |
| 81 |
|
| 82 |
user.save(); |
| 83 |
|
| 84 |
expect(user.attrs).toEqual({ |
| 85 |
id: "1", |
| 86 |
name: "Young link", |
| 87 |
location: "Hyrule", |
| 88 |
evil: false, |
| 89 |
}); |
| 90 |
expect(server.db.users.find(1)).toEqual({ |
| 91 |
id: "1", |
| 92 |
name: "Young link", |
| 93 |
location: "Hyrule", |
| 94 |
evil: false, |
| 95 |
}); |
| 96 |
}); |
| 97 |
|
| 98 |
test("it can update and immediately persist a single attribute", () => { |
| 99 |
let link = server.schema.users.find(1); |
| 100 |
link.update("evil", true); |
| 101 |
|
| 102 |
expect(link.attrs).toEqual({ |
| 103 |
id: "1", |
| 104 |
name: "Link", |
| 105 |
location: "Hyrule", |
| 106 |
evil: true, |
| 107 |
}); |
| 108 |
expect(server.db.users.find(1)).toEqual({ |
| 109 |
id: "1", |
| 110 |
name: "Link", |
| 111 |
location: "Hyrule", |
| 112 |
evil: true, |
| 113 |
}); |
| 114 |
}); |
| 115 |
|
| 116 |
test("it can update a hash of attrs immediately", () => { |
| 117 |
let link = server.schema.users.find(1); |
| 118 |
link.update({ name: "Evil link", evil: true }); |
| 119 |
|
| 120 |
expect(link.attrs).toEqual({ |
| 121 |
id: "1", |
| 122 |
name: "Evil link", |
| 123 |
location: "Hyrule", |
| 124 |
evil: true, |
| 125 |
}); |
| 126 |
expect(server.db.users.find(1)).toEqual({ |
| 127 |
id: "1", |
| 128 |
name: "Evil link", |
| 129 |
location: "Hyrule", |
| 130 |
evil: true, |
| 131 |
}); |
| 132 |
}); |
| 133 |
|
| 134 |
test("it can update a non-existing attribute", () => { |
| 135 |
let link = server.schema.users.find(1); |
| 136 |
link.update({ |
| 137 |
name: "Evil link", |
| 138 |
evil: true, |
| 139 |
reallyEvil: "absolutely evil", |
| 140 |
}); |
| 141 |
|
| 142 |
expect(link.attrs).toEqual({ |
| 143 |
id: "1", |
| 144 |
name: "Evil link", |
| 145 |
location: "Hyrule", |
| 146 |
evil: true, |
| 147 |
reallyEvil: "absolutely evil", |
| 148 |
}); |
| 149 |
expect(server.db.users.find(1)).toEqual({ |
| 150 |
id: "1", |
| 151 |
name: "Evil link", |
| 152 |
location: "Hyrule", |
| 153 |
evil: true, |
| 154 |
reallyEvil: "absolutely evil", |
| 155 |
}); |
| 156 |
}); |
| 157 |
|
| 158 |
test("if users sets incorrectly an attribute without using update, it will still work", () => { |
| 159 |
let link = server.schema.users.find(1); |
| 160 |
link.reallyEvil = "absolutely evil"; |
| 161 |
link.update({ reallyEvil: "a little flower", evil: true }); |
| 162 |
expect(link.attrs).toEqual({ |
| 163 |
id: "1", |
| 164 |
reallyEvil: "a little flower", |
| 165 |
evil: true, |
| 166 |
location: "Hyrule", |
| 167 |
name: "Link", |
| 168 |
}); |
| 169 |
expect(server.db.users.find(1)).toEqual({ |
| 170 |
id: "1", |
| 171 |
reallyEvil: "a little flower", |
| 172 |
evil: true, |
| 173 |
location: "Hyrule", |
| 174 |
name: "Link", |
| 175 |
}); |
| 176 |
}); |
| 177 |
}); |