-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush.test.ts
73 lines (62 loc) · 2.92 KB
/
push.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { expect, testSuite } from "manten";
import { nrml, testCollection } from "../../../common";
export default testSuite(async ({ describe }) => {
describe("$push", ({ test }) => {
test("works", () => {
const collection = testCollection();
collection.insert({ a: 1, b: [1] });
collection.update({ a: 1 }, { $push: { b: 2 } });
const found = nrml(collection.find({ a: 1 }));
expect(found).toEqual([{ a: 1, b: [1, 2] }]);
});
test("push more than one value", () => {
const collection = testCollection();
collection.insert({ a: 1, b: [1] });
collection.update({ a: 1 }, { $push: { b: [2, 3] } });
const found = nrml(collection.find({ a: 1 }));
expect(found).toEqual([{ a: 1, b: [1, 2, 3] }]);
});
test("push with dot notation", () => {
const collection = testCollection();
collection.insert({ a: 1, b: { c: 1, d: [1, 2]} });
collection.update({ c: 1 }, { $push: { "b.d": [3, 4] } });
const found = nrml(collection.find({ a: 1 }));
expect(found).toEqual([{ a: 1, b: { c: 1, d: [1, 2, 3, 4] } }]);
});
test("push with dot notation, multiple pushes", () => {
const collection = testCollection();
collection.insert({ a: 1, b: { c: 1, d: [1, 2]}, e: { c: 1, d: [1, 2] } });
collection.update({ c: 1 }, { $push: { "b.d": [3, 4], "e.d": [3, 4] } });
const found = nrml(collection.find({ a: 1 }));
expect(found).toEqual([{ a: 1, b: { c: 1, d: [1, 2, 3, 4] }, e: { c: 1, d: [1, 2, 3, 4] } }]);
});
test("push an object to an array of objects", () => {
const collection = testCollection();
collection.insert({ a: 1, b: [{ name: "a" }] });
collection.update({ a: 1 }, { $push: { b: { name: "b" } } });
const found = nrml(collection.find({ a: 1 }));
expect(found).toEqual([{ a: 1, b: [{ name: "a" }, { name: "b" }] }]);
});
test("push with dot notation, an object to an array of objects", () => {
const collection = testCollection();
collection.insert({ a: 1, b: { c: 1, d: [{ name: "a" }] } });
collection.update({ c: 1 }, { $push: { "b.d": { name: "b" } } });
const found = nrml(collection.find({ a: 1 }));
expect(found).toEqual([{ a: 1, b: { c: 1, d: [{ name: "a" }, { name: "b" }] } }]);
});
test("push does not create the target array if it doesn't exist", () => {
const collection = testCollection();
collection.insert({ a: 1 });
collection.update({ a: 1 }, { $push: { b: 1 } });
const found = nrml(collection.find({ a: 1 }));
expect(found).toEqual([{ a: 1 }]);
});
test("push with dot notation does not create the target array if it does not exist", () => {
const collection = testCollection();
collection.insert({ a: 1 });
collection.update({ a: 1 }, { $push: { "b.c": 1 } });
const found = nrml(collection.find({ a: 1 }));
expect(found).toEqual([{ a: 1 }]);
});
});
});