-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollection.spec.js
173 lines (165 loc) · 5.84 KB
/
collection.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const { Collection } = require("./index");
const get = require("lodash/get");
function getClient({
getCollectionBySlug = slug => {
return Promise.resolve({
slug: "home",
summary: null,
items: [
{
"associated-metadata": { layout: "ArrowFourColTwelveStories" },
type: "collection",
slug: "arrow-collection"
}
]
});
},
getInBulk = request => {
let collectionItem = {};
const requestKeys = Object.keys(request.requests);
if (requestKeys.length > 0) {
requestKeys.map(req => {
let items = [];
const limit = request.requests[req].limit || 40;
for (let i = 0; i < limit; i++) {
items.push({
"associated-metadata": {},
type: "collection",
slug: `football-sports-${i}`
});
}
Object.assign(collectionItem, {
[req]: {
slug: req,
items: items
}
});
});
}
return Promise.resolve({
results: collectionItem
});
}
} = {}) {
const clientObj = {
getCollectionBySlug,
getInBulk
};
return clientObj;
}
function catch404(e, defaultValue) {
const statusCode = get(e, ["response", "status"]);
if (statusCode === 404) return Promise.resolve(defaultValue);
throw e;
}
const clientThatCallsCollectionThatDoesntExist = getClient({
getCollectionBySlug: () => {
try {
throw { response: { status: 404 } };
} catch (e) {
return catch404(e, null);
}
}
});
describe("Collection", function() {
it("Returns null if collection does not exist", async function() {
const homeCollectionData = await Collection.getCollectionBySlug(
clientThatCallsCollectionThatDoesntExist,
"hot-news",
{},
{ depth: 1, defaultNestedLimit: 3 }
);
expect(homeCollectionData).toBe(null);
});
describe("Returns Home Collection based on defaultNestedLimit", function() {
it("Returns home-collection with depth of 1 and defaultNestedLimit of 3", async function() {
const homeCollectionData = await Collection.getCollectionBySlug(
getClient(),
"home",
{},
{ depth: 1, defaultNestedLimit: 3 }
);
expect(homeCollectionData.collection.items[0].items.length).toBe(3);
});
it("Returns home-collection with depth of 2 and defaultNestedLimit of 3", async function() {
const homeCollectionData = await Collection.getCollectionBySlug(
getClient(),
"home",
{},
{ depth: 2, defaultNestedLimit: 4 }
);
expect(homeCollectionData.collection.items[0].items.length).toBe(4);
expect(homeCollectionData.collection.items[0].items[0].items.length).toBe(4);
});
it("Returns home-collection with depth of 2", async function() {
const homeCollectionData = await Collection.getCollectionBySlug(getClient(), "home", {}, { depth: 2 });
expect(homeCollectionData.collection.items[0].items[0].items.length).toBe(40);
});
});
describe("Returns Home Collection based on nestedCollectionLimit", function() {
it("Returns home-collection with depth of 2 ", async function() {
const homeCollectionData = await Collection.getCollectionBySlug(
getClient(),
"home",
{},
{
depth: 2,
defaultNestedLimit: 4,
nestedCollectionLimit: { ArrowFourColTwelveStories: [1, 2, 3, 5] }
}
);
expect(homeCollectionData.collection.items[0].items.length).toBe(4);
expect(homeCollectionData.collection.items[0].items[0].items.length).toBe(1);
expect(homeCollectionData.collection.items[0].items[1].items.length).toBe(2);
expect(homeCollectionData.collection.items[0].items[2].items.length).toBe(3);
expect(homeCollectionData.collection.items[0].items[3].items.length).toBe(5);
});
it("Returns home-collection with depth of 2 and defaultlimit for the last nested collection ", async function() {
const homeCollectionData = await Collection.getCollectionBySlug(
getClient(),
"home",
{},
{
depth: 2,
defaultNestedLimit: 4,
nestedCollectionLimit: { ArrowFourColTwelveStories: [6, 7, 8] }
}
);
expect(homeCollectionData.collection.items[0].items.length).toBe(4);
expect(homeCollectionData.collection.items[0].items[0].items.length).toBe(6);
expect(homeCollectionData.collection.items[0].items[1].items.length).toBe(7);
expect(homeCollectionData.collection.items[0].items[2].items.length).toBe(8);
expect(homeCollectionData.collection.items[0].items[3].items.length).toBe(4);
});
});
describe("Returns Home Collection based on customLayouts", function() {
it("Returns home-collection with custom-layout's storylimit", async function() {
const homeCollectionData = await Collection.getCollectionBySlug(
getClient(),
"home",
{},
{
depth: 1,
customLayouts: [{ layout: "ArrowThreeColGrid", storyLimit: 6 }]
}
);
expect(homeCollectionData.collection.items[0].items.length).toBe(6);
});
it("Returns home-collection with custom-layout's nestedCollectionLimit", async function() {
const homeCollectionData = await Collection.getCollectionBySlug(
getClient(),
"home",
{},
{
depth: 1,
collectionOfCollectionsIndexes: [0],
customLayouts: [{ layout: "ArrowFourColTwelveStories", storyLimit: 4, nestedCollectionLimit: [3, 3, 3] }]
}
);
expect(homeCollectionData.collection.items[0].items.length).toBe(4);
expect(homeCollectionData.collection.items[0].items[0].items.length).toBe(3);
expect(homeCollectionData.collection.items[0].items[1].items.length).toBe(3);
expect(homeCollectionData.collection.items[0].items[2].items.length).toBe(3);
});
});
});