| 1 |
import "@lib/container"; |
| 2 |
import Db from "@lib/db"; |
| 3 |
import DefaultIdentityManager from "@lib/identity-manager"; |
| 4 |
|
| 5 |
let db; |
| 6 |
|
| 7 |
describe("Unit | Db", function () { |
| 8 |
test("it can be instantiated", () => { |
| 9 |
db = new Db(); |
| 10 |
expect(Db).toBeTruthy(); |
| 11 |
}); |
| 12 |
|
| 13 |
test("it can load data on instantiation", () => { |
| 14 |
db = new Db({ |
| 15 |
users: [{ id: 1, name: "Link" }], |
| 16 |
addresses: [ |
| 17 |
{ id: 1, name: "123 Hyrule Way" }, |
| 18 |
{ id: 2, name: "Lorem ipsum" }, |
| 19 |
], |
| 20 |
}); |
| 21 |
|
| 22 |
expect(db.users).toHaveLength(1); |
| 23 |
expect(db.addresses).toHaveLength(2); |
| 24 |
}); |
| 25 |
|
| 26 |
test("it can empty its data", () => { |
| 27 |
db = new Db({ |
| 28 |
users: [{ id: 1, name: "Link" }], |
| 29 |
addresses: [ |
| 30 |
{ id: 1, name: "123 Hyrule Way" }, |
| 31 |
{ id: 2, name: "Lorem ipsum" }, |
| 32 |
], |
| 33 |
}); |
| 34 |
|
| 35 |
db.emptyData(); |
| 36 |
|
| 37 |
expect(db.users).toHaveLength(0); |
| 38 |
expect(db.addresses).toHaveLength(0); |
| 39 |
}); |
| 40 |
}); |
| 41 |
|
| 42 |
describe("Unit | Db #createCollection", function () { |
| 43 |
beforeEach(function () { |
| 44 |
db = new Db(); |
| 45 |
}); |
| 46 |
|
| 47 |
afterEach(function () { |
| 48 |
db.emptyData(); |
| 49 |
}); |
| 50 |
|
| 51 |
test("it can create an empty collection", () => { |
| 52 |
db.createCollection("contacts"); |
| 53 |
|
| 54 |
expect(db.contacts).toBeTruthy(); |
| 55 |
}); |
| 56 |
|
| 57 |
test("it can create many collections", () => { |
| 58 |
db.createCollections("contacts", "addresses"); |
| 59 |
|
| 60 |
expect(db.contacts).toBeTruthy(); |
| 61 |
expect(db.addresses).toBeTruthy(); |
| 62 |
}); |
| 63 |
}); |
| 64 |
|
| 65 |
describe("Unit | Db #loadData", function () { |
| 66 |
beforeEach(function () { |
| 67 |
db = new Db(); |
| 68 |
}); |
| 69 |
|
| 70 |
afterEach(function () { |
| 71 |
db.emptyData(); |
| 72 |
}); |
| 73 |
|
| 74 |
test("it can load an object of data", () => { |
| 75 |
let data = { |
| 76 |
contacts: [{ id: "1", name: "Link" }], |
| 77 |
addresses: [{ id: "1", name: "123 Hyrule Way" }], |
| 78 |
}; |
| 79 |
db.loadData(data); |
| 80 |
|
| 81 |
expect(db.contacts).toIncludeSameMembers(data.contacts); |
| 82 |
expect(db.addresses).toIncludeSameMembers(data.addresses); |
| 83 |
}); |
| 84 |
|
| 85 |
test("it clones all data so nothing is passed by reference", () => { |
| 86 |
let data = { |
| 87 |
contacts: [{ id: "1", someArray: ["foo", "bar"] }], |
| 88 |
}; |
| 89 |
db.loadData(data); |
| 90 |
|
| 91 |
let contactRecord = db.contacts.find(1); |
| 92 |
contactRecord.someArray.push("baz"); |
| 93 |
|
| 94 |
expect(contactRecord.someArray).toHaveLength(3); |
| 95 |
expect(data.contacts[0].someArray).toHaveLength(2); |
| 96 |
}); |
| 97 |
}); |
| 98 |
|
| 99 |
describe("Unit | Db #all", function () { |
| 100 |
let data; |
| 101 |
|
| 102 |
beforeEach(function () { |
| 103 |
data = { |
| 104 |
contacts: [{ id: "1", name: "Link" }], |
| 105 |
addresses: [{ id: "1", name: "123 Hyrule Way" }], |
| 106 |
}; |
| 107 |
|
| 108 |
db = new Db(data); |
| 109 |
}); |
| 110 |
|
| 111 |
afterEach(function () { |
| 112 |
db.emptyData(); |
| 113 |
}); |
| 114 |
|
| 115 |
test("it can return a collection", () => { |
| 116 |
expect(db.contacts).toIncludeSameMembers(data.contacts); |
| 117 |
expect(db.addresses).toIncludeSameMembers(data.addresses); |
| 118 |
}); |
| 119 |
|
| 120 |
test("the collection is a copy", () => { |
| 121 |
let { contacts } = db; |
| 122 |
|
| 123 |
expect(contacts).toIncludeSameMembers(data.contacts); |
| 124 |
contacts[0].name = "Zelda"; |
| 125 |
|
| 126 |
expect(db.contacts).toIncludeSameMembers(data.contacts); |
| 127 |
}); |
| 128 |
}); |
| 129 |
|
| 130 |
describe("Unit | Db #insert", function () { |
| 131 |
beforeEach(function () { |
| 132 |
db = new Db(); |
| 133 |
db.createCollection("contacts"); |
| 134 |
}); |
| 135 |
|
| 136 |
afterEach(function () { |
| 137 |
db.emptyData(); |
| 138 |
}); |
| 139 |
|
| 140 |
test("it inserts an object and returns it", () => { |
| 141 |
let link = db.contacts.insert({ name: "Link" }); |
| 142 |
let expectedRecord = { |
| 143 |
id: "1", |
| 144 |
name: "Link", |
| 145 |
}; |
| 146 |
|
| 147 |
expect(db.contacts).toIncludeSameMembers([expectedRecord]); |
| 148 |
expect(link).toEqual(expectedRecord); |
| 149 |
}); |
| 150 |
|
| 151 |
test("it returns a copy", () => { |
| 152 |
let link = db.contacts.insert({ name: "Link" }); |
| 153 |
let expectedRecord = { |
| 154 |
id: "1", |
| 155 |
name: "Link", |
| 156 |
}; |
| 157 |
|
| 158 |
expect(link).toEqual(expectedRecord); |
| 159 |
|
| 160 |
link.name = "Young link"; |
| 161 |
|
| 162 |
expect(db.contacts.find(1)).toEqual(expectedRecord); |
| 163 |
}); |
| 164 |
|
| 165 |
test("it can insert objects sequentially", () => { |
| 166 |
db.contacts.insert({ name: "Link" }); |
| 167 |
db.contacts.insert({ name: "Ganon" }); |
| 168 |
|
| 169 |
let records = [ |
| 170 |
{ id: "1", name: "Link" }, |
| 171 |
{ id: "2", name: "Ganon" }, |
| 172 |
]; |
| 173 |
|
| 174 |
expect(db.contacts).toIncludeSameMembers(records); |
| 175 |
}); |
| 176 |
|
| 177 |
test("it does not add an id if present", () => { |
| 178 |
let attrs = { id: "5", name: "Link" }; |
| 179 |
|
| 180 |
db.contacts.insert(attrs); |
| 181 |
|
| 182 |
expect(db.contacts).toIncludeSameMembers([attrs]); |
| 183 |
}); |
| 184 |
|
| 185 |
test("it can insert an array and return it", () => { |
| 186 |
db.contacts.insert({ name: "Link" }); |
| 187 |
|
| 188 |
let contacts = db.contacts.insert([{ name: "Zelda" }, { name: "Ganon" }]); |
| 189 |
|
| 190 |
expect(db.contacts).toIncludeSameMembers([ |
| 191 |
{ id: "1", name: "Link" }, |
| 192 |
{ id: "2", name: "Zelda" }, |
| 193 |
{ id: "3", name: "Ganon" }, |
| 194 |
]); |
| 195 |
expect(contacts).toIncludeSameMembers([ |
| 196 |
{ id: "2", name: "Zelda" }, |
| 197 |
{ id: "3", name: "Ganon" }, |
| 198 |
]); |
| 199 |
}); |
| 200 |
|
| 201 |
test("it does not add ids to array data if present", () => { |
| 202 |
db.contacts.insert([ |
| 203 |
{ id: 2, name: "Link" }, |
| 204 |
{ id: 1, name: "Ganon" }, |
| 205 |
]); |
| 206 |
|
| 207 |
expect(db.contacts).toHaveLength(2); |
| 208 |
expect(db.contacts).toContainEqual({ id: "2", name: "Link" }); |
| 209 |
expect(db.contacts).toContainEqual({ id: "1", name: "Ganon" }); |
| 210 |
}); |
| 211 |
|
| 212 |
test("it can insert a record with an id of 0", () => { |
| 213 |
db.contacts.insert({ id: 0, name: "Link" }); |
| 214 |
|
| 215 |
expect(db.contacts).toHaveLength(1); |
| 216 |
expect(db.contacts).toContainEqual({ id: "0", name: "Link" }); |
| 217 |
}); |
| 218 |
|
| 219 |
test("IDs increment correctly, even after a record is removed", () => { |
| 220 |
let records = db.contacts.insert([{ name: "Link" }, { name: "Ganon" }]); |
| 221 |
|
| 222 |
db.contacts.remove(records[0]); |
| 223 |
|
| 224 |
let record = db.contacts.insert({ name: "Zelda" }); |
| 225 |
|
| 226 |
expect(record.id).toEqual("3"); |
| 227 |
}); |
| 228 |
|
| 229 |
test("inserting a record with an already used ID throws an error", () => { |
| 230 |
expect.assertions(2); |
| 231 |
|
| 232 |
db.contacts.insert({ id: 1, name: "Duncan McCleod" }); |
| 233 |
|
| 234 |
expect(function () { |
| 235 |
db.contacts.insert({ id: 1, name: "Duncan McCleod" }); |
| 236 |
}).toThrow(); |
| 237 |
|
| 238 |
db.contacts.insert({ id: "atp", name: "Adenosine Triphosphate" }); |
| 239 |
|
| 240 |
expect(function () { |
| 241 |
db.contacts.insert({ id: "atp", name: "Adenosine Triphosphate" }); |
| 242 |
}).toThrow(); |
| 243 |
}); |
| 244 |
|
| 245 |
test("tracks the correct IDs being used", () => { |
| 246 |
db.contacts.insert({ name: "Vegeta" }); |
| 247 |
db.contacts.insert({ id: 2, name: "Krilli" }); |
| 248 |
|
| 249 |
expect(db.contacts).toHaveLength(2); |
| 250 |
}); |
| 251 |
}); |
| 252 |
|
| 253 |
describe("Unit | Db #findBy", function () { |
| 254 |
beforeEach(function () { |
| 255 |
db = new Db(); |
| 256 |
db.createCollection("contacts"); |
| 257 |
db.contacts.insert([ |
| 258 |
{ name: "Zelda" }, |
| 259 |
{ name: "Link" }, |
| 260 |
{ name: "Epona", race: "Horse" }, |
| 261 |
{ name: "Epona", race: "Centaur" }, |
| 262 |
{ id: "abc", name: "Ganon" }, |
| 263 |
]); |
| 264 |
}); |
| 265 |
|
| 266 |
afterEach(function () { |
| 267 |
db.emptyData(); |
| 268 |
}); |
| 269 |
|
| 270 |
test("returns a record that matches the given name", () => { |
| 271 |
let contact = db.contacts.findBy({ name: "Link" }); |
| 272 |
|
| 273 |
expect(contact).toEqual({ id: "2", name: "Link" }); |
| 274 |
}); |
| 275 |
|
| 276 |
test("returns a copy not a reference", () => { |
| 277 |
let contact = db.contacts.findBy({ name: "Link" }); |
| 278 |
|
| 279 |
contact.name = "blah"; |
| 280 |
|
| 281 |
expect(db.contacts.find(2)).toEqual({ id: "2", name: "Link" }); |
| 282 |
}); |
| 283 |
|
| 284 |
test("returns the first record matching the criteria", () => { |
| 285 |
let contact = db.contacts.findBy({ name: "Epona" }); |
| 286 |
|
| 287 |
expect(contact).toEqual({ id: "3", name: "Epona", race: "Horse" }); |
| 288 |
}); |
| 289 |
|
| 290 |
test("returns a record only matching multiple criteria", () => { |
| 291 |
let contact = db.contacts.findBy({ name: "Epona", race: "Centaur" }); |
| 292 |
|
| 293 |
expect(contact).toEqual({ id: "4", name: "Epona", race: "Centaur" }); |
| 294 |
}); |
| 295 |
|
| 296 |
test("returns null when no record is found", () => { |
| 297 |
let contact = db.contacts.findBy({ name: "Fi" }); |
| 298 |
|
| 299 |
expect(contact).toBeNull(); |
| 300 |
}); |
| 301 |
}); |
| 302 |
|
| 303 |
describe("Unit | Db #find", function () { |
| 304 |
beforeEach(function () { |
| 305 |
db = new Db(); |
| 306 |
db.createCollection("contacts"); |
| 307 |
db.contacts.insert([ |
| 308 |
{ name: "Zelda" }, |
| 309 |
{ name: "Link" }, |
| 310 |
{ id: "abc", name: "Ganon" }, |
| 311 |
]); |
| 312 |
}); |
| 313 |
|
| 314 |
afterEach(function () { |
| 315 |
db.emptyData(); |
| 316 |
}); |
| 317 |
|
| 318 |
test("returns a record that matches a numerical id", () => { |
| 319 |
let contact = db.contacts.find(2); |
| 320 |
|
| 321 |
expect(contact).toEqual({ id: "2", name: "Link" }); |
| 322 |
}); |
| 323 |
|
| 324 |
test("returns a copy not a reference", () => { |
| 325 |
let contact = db.contacts.find(2); |
| 326 |
|
| 327 |
expect(contact).toEqual({ id: "2", name: "Link" }); |
| 328 |
|
| 329 |
contact.name = "blah"; |
| 330 |
|
| 331 |
expect(db.contacts.find(2)).toEqual({ id: "2", name: "Link" }); |
| 332 |
}); |
| 333 |
|
| 334 |
test("returns a record that matches a string id", () => { |
| 335 |
let contact = db.contacts.find("abc"); |
| 336 |
|
| 337 |
expect(contact).toEqual({ id: "abc", name: "Ganon" }); |
| 338 |
}); |
| 339 |
|
| 340 |
test("returns multiple record that matches an array of ids", () => { |
| 341 |
let contacts = db.contacts.find([1, 2]); |
| 342 |
|
| 343 |
expect(contacts).toEqual([ |
| 344 |
{ id: "1", name: "Zelda" }, |
| 345 |
{ id: "2", name: "Link" }, |
| 346 |
]); |
| 347 |
}); |
| 348 |
|
| 349 |
test("returns a record whose id is a string that start with numbers", () => { |
| 350 |
db.contacts.insert({ |
| 351 |
id: "123-456", |
| 352 |
name: "Epona", |
| 353 |
}); |
| 354 |
|
| 355 |
let contact = db.contacts.find("123-456"); |
| 356 |
expect(contact).toEqual({ id: "123-456", name: "Epona" }); |
| 357 |
}); |
| 358 |
|
| 359 |
test("returns multiple record that match an array of ids", () => { |
| 360 |
let contacts = db.contacts.find([1, 2]); |
| 361 |
|
| 362 |
expect(contacts).toEqual([ |
| 363 |
{ id: "1", name: "Zelda" }, |
| 364 |
{ id: "2", name: "Link" }, |
| 365 |
]); |
| 366 |
}); |
| 367 |
|
| 368 |
test("returns an empty array when it doesnt find multiple ids", () => { |
| 369 |
let contacts = db.contacts.find([99, 100]); |
| 370 |
|
| 371 |
expect(contacts).toEqual([]); |
| 372 |
}); |
| 373 |
}); |
| 374 |
|
| 375 |
describe("Unit | Db #where", function () { |
| 376 |
beforeEach(function () { |
| 377 |
db = new Db(); |
| 378 |
db.createCollection("contacts"); |
| 379 |
db.contacts.insert([ |
| 380 |
{ name: "Link", evil: false, age: 17 }, |
| 381 |
{ name: "Zelda", evil: false, age: 17 }, |
| 382 |
{ name: "Ganon", evil: true, age: 45 }, |
| 383 |
]); |
| 384 |
}); |
| 385 |
|
| 386 |
afterEach(function () { |
| 387 |
db.emptyData(); |
| 388 |
}); |
| 389 |
|
| 390 |
test("returns an array of records that match the query", () => { |
| 391 |
let result = db.contacts.where({ evil: true }); |
| 392 |
|
| 393 |
expect(result).toEqual([{ id: "3", name: "Ganon", evil: true, age: 45 }]); |
| 394 |
}); |
| 395 |
|
| 396 |
test("it coerces query params to strings", () => { |
| 397 |
let result = db.contacts.where({ age: "45" }); |
| 398 |
|
| 399 |
expect(result).toEqual([{ id: "3", name: "Ganon", evil: true, age: 45 }]); |
| 400 |
}); |
| 401 |
|
| 402 |
test("returns a copy, not a referecne", () => { |
| 403 |
let result = db.contacts.where({ evil: true }); |
| 404 |
|
| 405 |
expect(result).toEqual([{ id: "3", name: "Ganon", evil: true, age: 45 }]); |
| 406 |
|
| 407 |
result[0].evil = false; |
| 408 |
|
| 409 |
expect(db.contacts.where({ evil: true })).toEqual([ |
| 410 |
{ id: "3", name: "Ganon", evil: true, age: 45 }, |
| 411 |
]); |
| 412 |
}); |
| 413 |
|
| 414 |
test("returns an empty array if no records match the query", () => { |
| 415 |
let result = db.contacts.where({ name: "Link", evil: true }); |
| 416 |
|
| 417 |
expect(result).toEqual([]); |
| 418 |
}); |
| 419 |
|
| 420 |
test("accepts a filter function", () => { |
| 421 |
let result = db.contacts.where(function (record) { |
| 422 |
return record.age === 45; |
| 423 |
}); |
| 424 |
|
| 425 |
expect(result).toEqual([{ id: "3", name: "Ganon", evil: true, age: 45 }]); |
| 426 |
}); |
| 427 |
}); |
| 428 |
|
| 429 |
describe("Unit | Db #update", function () { |
| 430 |
beforeEach(function () { |
| 431 |
db = new Db(); |
| 432 |
db.createCollection("contacts"); |
| 433 |
db.contacts.insert([ |
| 434 |
{ name: "Link", evil: false }, |
| 435 |
{ name: "Zelda", evil: false }, |
| 436 |
{ name: "Ganon", evil: true }, |
| 437 |
{ id: "123-abc", name: "Epona", evil: false }, |
| 438 |
]); |
| 439 |
}); |
| 440 |
|
| 441 |
afterEach(function () { |
| 442 |
db.emptyData(); |
| 443 |
}); |
| 444 |
|
| 445 |
test("it can update the whole collection", () => { |
| 446 |
db.contacts.update({ name: "Sam", evil: false }); |
| 447 |
|
| 448 |
expect(db.contacts).toHaveLength(4); |
| 449 |
|
| 450 |
[ |
| 451 |
{ id: "1", name: "Sam", evil: false }, |
| 452 |
{ id: "2", name: "Sam", evil: false }, |
| 453 |
{ id: "3", name: "Sam", evil: false }, |
| 454 |
{ id: "123-abc", name: "Sam", evil: false }, |
| 455 |
].forEach((contact) => { |
| 456 |
expect(db.contacts).toContainEqual(contact); |
| 457 |
}); |
| 458 |
}); |
| 459 |
|
| 460 |
test("it can update a record by id", () => { |
| 461 |
db.contacts.update(3, { name: "Ganondorf", evil: false }); |
| 462 |
let ganon = db.contacts.find(3); |
| 463 |
|
| 464 |
expect(ganon).toEqual({ id: "3", name: "Ganondorf", evil: false }); |
| 465 |
}); |
| 466 |
|
| 467 |
test("it can update a record by id when the id is a string", () => { |
| 468 |
db.contacts.update("123-abc", { evil: true }); |
| 469 |
let epona = db.contacts.find("123-abc"); |
| 470 |
|
| 471 |
expect(epona).toEqual({ id: "123-abc", name: "Epona", evil: true }); |
| 472 |
}); |
| 473 |
|
| 474 |
test("it can update multiple records by ids", () => { |
| 475 |
db.contacts.update([1, 2], { evil: true }); |
| 476 |
let link = db.contacts.find(1); |
| 477 |
let zelda = db.contacts.find(2); |
| 478 |
|
| 479 |
expect(link.evil).toEqual(true); |
| 480 |
expect(zelda.evil).toEqual(true); |
| 481 |
}); |
| 482 |
|
| 483 |
test("it can update records by query", () => { |
| 484 |
db.contacts.update({ evil: false }, { name: "Sam" }); |
| 485 |
|
| 486 |
expect(db.contacts).toHaveLength(4); |
| 487 |
[ |
| 488 |
{ id: "1", name: "Sam", evil: false }, |
| 489 |
{ id: "2", name: "Sam", evil: false }, |
| 490 |
{ id: "3", name: "Ganon", evil: true }, |
| 491 |
{ id: "123-abc", name: "Sam", evil: false }, |
| 492 |
].forEach((contact) => { |
| 493 |
expect(db.contacts).toContainEqual(contact); |
| 494 |
}); |
| 495 |
}); |
| 496 |
|
| 497 |
test("updating a single record returns that record", () => { |
| 498 |
let ganon = db.contacts.update(3, { name: "Ganondorf" }); |
| 499 |
expect(ganon).toEqual({ id: "3", name: "Ganondorf", evil: true }); |
| 500 |
}); |
| 501 |
|
| 502 |
test("updating a collection returns the updated records", () => { |
| 503 |
let characters = db.contacts.update({ evil: true }); |
| 504 |
expect(characters).toEqual([ |
| 505 |
{ id: "1", name: "Link", evil: true }, |
| 506 |
{ id: "2", name: "Zelda", evil: true }, |
| 507 |
{ id: "123-abc", name: "Epona", evil: true }, |
| 508 |
]); |
| 509 |
}); |
| 510 |
|
| 511 |
test("updating multiple records returns the updated records", () => { |
| 512 |
let characters = db.contacts.update({ evil: false }, { evil: true }); |
| 513 |
expect(characters).toEqual([ |
| 514 |
{ id: "1", name: "Link", evil: true }, |
| 515 |
{ id: "2", name: "Zelda", evil: true }, |
| 516 |
{ id: "123-abc", name: "Epona", evil: true }, |
| 517 |
]); |
| 518 |
}); |
| 519 |
|
| 520 |
test("throws when updating an ID is attempted", () => { |
| 521 |
expect.assertions(1); |
| 522 |
|
| 523 |
expect(function () { |
| 524 |
db.contacts.update(1, { id: 3 }); |
| 525 |
}).toThrow(); |
| 526 |
}); |
| 527 |
}); |
| 528 |
|
| 529 |
describe("Unit | Db #remove", function () { |
| 530 |
beforeEach(function () { |
| 531 |
db = new Db(); |
| 532 |
db.createCollection("contacts"); |
| 533 |
db.contacts.insert([ |
| 534 |
{ name: "Link", evil: false }, |
| 535 |
{ name: "Zelda", evil: false }, |
| 536 |
{ name: "Ganon", evil: true }, |
| 537 |
{ id: "123-abc", name: "Epona", evil: false }, |
| 538 |
]); |
| 539 |
}); |
| 540 |
|
| 541 |
afterEach(function () { |
| 542 |
db.emptyData(); |
| 543 |
}); |
| 544 |
|
| 545 |
test("it can remove an entire collection", () => { |
| 546 |
db.contacts.remove(); |
| 547 |
|
| 548 |
expect(db.contacts).toHaveLength(0); |
| 549 |
}); |
| 550 |
|
| 551 |
test("it can remove a single record by id", () => { |
| 552 |
db.contacts.remove(1); |
| 553 |
|
| 554 |
expect(db.contacts).toContainEqual({ id: "2", name: "Zelda", evil: false }); |
| 555 |
expect(db.contacts).toContainEqual({ id: "3", name: "Ganon", evil: true }); |
| 556 |
expect(db.contacts).toContainEqual({ |
| 557 |
id: "123-abc", |
| 558 |
name: "Epona", |
| 559 |
evil: false, |
| 560 |
}); |
| 561 |
}); |
| 562 |
|
| 563 |
test("it can remove a single record when the id is a string", () => { |
| 564 |
db.contacts.remove("123-abc"); |
| 565 |
|
| 566 |
expect(db.contacts).toHaveLength(3); |
| 567 |
expect(db.contacts).toContainEqual({ id: "1", name: "Link", evil: false }); |
| 568 |
expect(db.contacts).toContainEqual({ id: "2", name: "Zelda", evil: false }); |
| 569 |
expect(db.contacts).toContainEqual({ id: "3", name: "Ganon", evil: true }); |
| 570 |
}); |
| 571 |
|
| 572 |
test("it can remove multiple records by ids", () => { |
| 573 |
db.contacts.remove([1, 2]); |
| 574 |
|
| 575 |
expect(db.contacts).toHaveLength(2); |
| 576 |
expect(db.contacts).toContainEqual({ id: "3", name: "Ganon", evil: true }); |
| 577 |
expect(db.contacts).toContainEqual({ |
| 578 |
id: "123-abc", |
| 579 |
name: "Epona", |
| 580 |
evil: false, |
| 581 |
}); |
| 582 |
}); |
| 583 |
|
| 584 |
test("it can remove multiple records by query", () => { |
| 585 |
db.contacts.remove({ evil: false }); |
| 586 |
|
| 587 |
expect(db.contacts).toHaveLength(1); |
| 588 |
expect(db.contacts).toContainEqual({ id: "3", name: "Ganon", evil: true }); |
| 589 |
}); |
| 590 |
|
| 591 |
test("it can add a record after removing all records", () => { |
| 592 |
db.contacts.remove(); |
| 593 |
db.contacts.insert({ name: "Foo" }); |
| 594 |
|
| 595 |
expect(db.contacts).toHaveLength(1); |
| 596 |
expect(db.contacts).toContainEqual({ id: "1", name: "Foo" }); |
| 597 |
}); |
| 598 |
}); |
| 599 |
|
| 600 |
describe("Unit | Db #firstOrCreate", function () { |
| 601 |
beforeEach(function () { |
| 602 |
db = new Db(); |
| 603 |
db.createCollection("contacts"); |
| 604 |
db.contacts.insert([ |
| 605 |
{ id: 1, name: "Link", evil: false }, |
| 606 |
{ id: 2, name: "Zelda", evil: false }, |
| 607 |
{ id: 3, name: "Ganon", evil: true }, |
| 608 |
]); |
| 609 |
}); |
| 610 |
|
| 611 |
afterEach(function () { |
| 612 |
db.emptyData(); |
| 613 |
}); |
| 614 |
|
| 615 |
test("it can find the first record available from the query", () => { |
| 616 |
let record = db.contacts.firstOrCreate({ name: "Link" }); |
| 617 |
|
| 618 |
expect(record).toEqual({ id: "1", name: "Link", evil: false }); |
| 619 |
}); |
| 620 |
|
| 621 |
test("it creates a new record from query + attrs if none found", () => { |
| 622 |
let record = db.contacts.firstOrCreate({ name: "Mario" }, { evil: false }); |
| 623 |
|
| 624 |
expect(record.name).toEqual("Mario"); |
| 625 |
expect(record.evil).toEqual(false); |
| 626 |
expect(record.id).toBeTruthy(); |
| 627 |
}); |
| 628 |
|
| 629 |
test("does not require attrs", () => { |
| 630 |
let record = db.contacts.firstOrCreate({ name: "Luigi" }); |
| 631 |
|
| 632 |
expect(record.name).toEqual("Luigi"); |
| 633 |
expect(record.id).toBeTruthy(); |
| 634 |
}); |
| 635 |
}); |
| 636 |
|
| 637 |
describe("Unit | Db #registerIdentityManagers and #identityManagerFor", function () { |
| 638 |
test("identityManagerFor returns default IdentityManager if there aren't any custom ones", () => { |
| 639 |
let db = new Db(); |
| 640 |
expect(db.identityManagerFor("foo")).toEqual(DefaultIdentityManager); |
| 641 |
}); |
| 642 |
|
| 643 |
test("it can register identity managers per db collection and for application", () => { |
| 644 |
let FooIdentityManager = class {}; |
| 645 |
let ApplicationIdentityManager = class {}; |
| 646 |
|
| 647 |
let db = new Db(); |
| 648 |
db.registerIdentityManagers({ |
| 649 |
foo: FooIdentityManager, |
| 650 |
application: ApplicationIdentityManager, |
| 651 |
}); |
| 652 |
|
| 653 |
expect(db.identityManagerFor("foo")).toEqual(FooIdentityManager); |
| 654 |
expect(db.identityManagerFor("bar")).toEqual(ApplicationIdentityManager); |
| 655 |
}); |
| 656 |
|
| 657 |
test("it can register idenitity managers on instantiation", () => { |
| 658 |
let CustomIdentityManager = class {}; |
| 659 |
let db = new Db(undefined, { |
| 660 |
foo: CustomIdentityManager, |
| 661 |
}); |
| 662 |
expect(db.identityManagerFor("foo")).toEqual(CustomIdentityManager); |
| 663 |
expect(db.identityManagerFor("bar")).toEqual(DefaultIdentityManager); |
| 664 |
}); |
| 665 |
}); |