-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.spec.js
120 lines (102 loc) · 3.15 KB
/
Search.spec.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const sandbox = require("sinon").createSandbox();
const Search = require("./Search");
describe("Search", () => {
let Model;
beforeEach(() => {
Model = {
modelName: "TestModel",
disableRemoteMethodByName: sandbox.spy(),
remoteMethod: sandbox.spy(),
count: sandbox.fake.returns(100),
find: sandbox.fake.returns([{ test: true }])
};
});
afterEach(() => sandbox.restore());
it("should disable default find endpoint", () => {
Search(Model, {});
sandbox.assert.calledOnce(Model.disableRemoteMethodByName);
sandbox.assert.calledWith(Model.disableRemoteMethodByName, "find");
});
it("should set up new remote method", () => {
Search(Model, {});
sandbox.assert.calledOnce(Model.remoteMethod);
sandbox.assert.calledWith(
Model.remoteMethod,
"search",
sandbox.match({
description: sandbox.match.string,
accepts: [
{
arg: sandbox.match.string,
type: sandbox.match.string,
description: sandbox.match.string
}
],
http: { path: "/search", verb: "get" },
returns: {
root: true,
type: {
total: "number",
limit: "number",
skip: "number",
data: [Model.modelName]
}
}
})
);
});
it("should create member function setDefaultFilter", () => {
Search(Model, {});
Model.setDefaultFilter.should.be.a.Function();
});
describe("setDefaultFilter", () => {
beforeEach(() => Search(Model, {}));
it("should create filter if non exists", () => {
const filter = Model.setDefaultFilter();
filter.should.have.keys("limit", "offset");
});
it("should add default limit to filter if does not exist", () => {
const filter = Model.setDefaultFilter({ offset: 5 });
filter.limit.should.equal(10);
});
it("should add default offset to filter if does not exist", () => {
const filter = Model.setDefaultFilter({ limit: 5 });
filter.offset.should.equal(0);
});
it("should add not override offset or limit if they exist in the filter", () => {
const filter = Model.setDefaultFilter({ limit: 100, offset: 100 });
filter.offset.should.equal(100);
filter.limit.should.equal(100);
});
it("should override default mixin limit", () => {
Search(Model, { limit: 20 });
const filter = Model.setDefaultFilter();
filter.limit.should.equal(20);
});
it("should override default mixin offset", () => {
Search(Model, { offset: 10 });
const filter = Model.setDefaultFilter();
filter.offset.should.equal(10);
});
});
describe("search", () => {
let filterSpy;
beforeEach(() => {
Search(Model, {});
filterSpy = sandbox.spy(Model, "setDefaultFilter");
});
it("should set default filter", () => {
Model.search();
sandbox.assert.calledOnce(filterSpy);
});
it("should return payload", async () => {
const res = await Model.search();
res.should.deepEqual({
total: 100,
limit: 10,
offset: 0,
data: [{ test: true }]
});
});
});
});