-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn.test.ts
59 lines (58 loc) · 2.03 KB
/
fn.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
import { testSuite, expect } from "manten";
import { nrml, testCollection } from "../../../common";
export default testSuite(async ({ describe }) => {
describe("$fn", ({ test }) => {
test("works", () => {
const collection = testCollection();
collection.insert([
{ a: 2 },
{ a: 4 },
{ a: 5 },
{ a: 6 },
]);
const isEven = (x: number) => x % 2 === 0;
const found = nrml(collection.find({ a: { $fn: isEven } }));
expect(found).toEqual([{ a: 2 }, { a: 4 }, { a: 6 }]);
});
test("nested", () => {
const collection = testCollection();
collection.insert([
{ a: { b: { c: 2 } } },
{ a: { b: { c: 4 } } },
{ a: { b: { c: 5 } } },
{ a: { b: { c: 6 } } },
]);
const isEven = (x: number) => x % 2 === 0;
const found = nrml(collection.find({ c: { $fn: isEven } }));
expect(found).toEqual([ { a: { b: { c: 2 } } }, { a: { b: { c: 4 } } }, { a: { b: { c: 6 } } } ]);
});
test("nested, using dot notation", () => {
const collection = testCollection();
collection.insert([
{ a: { b: { c: 2 } } },
{ a: { b: { c: 4 } } },
{ a: { b: { c: 5 } } },
{ a: { b: { c: 6 } } },
]);
const isEven = (x: number) => x % 2 === 0;
const found = nrml(collection.find({ "a.b.c": { $fn: isEven } }));
expect(found).toEqual([ { a: { b: { c: 2 } } }, { a: { b: { c: 4 } } }, { a: { b: { c: 6 } } } ]);
});
test("multiple functions", () => {
// When using multiple functions, the source value must be true
// for each function in order to be considered a query match.
const collection = testCollection();
collection.insert([
{ a: 2 },
{ a: 3 },
{ a: 4 },
{ a: 5 },
{ a: 6 },
]);
const isOdd = (x: number) => x % 2 !== 0;
const isThree = (x: number) => x === 3;
const found = nrml(collection.find({ a: { $fn: [isThree, isOdd] } }));
expect(found).toEqual([{ a: 3 }]);
});
});
});