-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.spec.js
153 lines (126 loc) · 5.58 KB
/
config.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
const { Config } = require('./index');
describe("Config", function() {
describe("Returns Home Collection Slug Given a Domain" , function() {
it("Returns home-collection id provided a hostname", function(){
const config = Config.build({
domains: [{slug: "mydomain", name: "mydomain", "home-collection-id": 1234}]
});
expect(config.getHomeCollectionSlug("mydomain")).toBe(1234);
});
it("returns home-collection as home if the domain is not found", function() {
const config = Config.build({
domains: []
});
expect(config.getHomeCollectionSlug("mydomain")).toBe("home");
expect(config.getHomeCollectionSlug(null)).toBe("home");
expect(config.getHomeCollectionSlug(undefined)).toBe("home");
});
it("still works if domain is missing", function() {
const config = Config.build({
domains: undefined
});
expect(config.getHomeCollectionSlug("mydomain")).toBe("home");
expect(config.getHomeCollectionSlug(null)).toBe("home");
expect(config.getHomeCollectionSlug(undefined)).toBe("home");
});
});
describe("Returns Sections Given a Domain", function() {
let config= Config.build({
sections:[
{
'domain-slug': 'cinema',
name: 'Hollywood',
id: 2,
},
{
'domain-slug': null,
name:'News',
id:3
},
{
'domain-slug': 'cinema',
name: 'Bollywood',
id: 10,
},
]
});
it("gets all sections for a given domain", function() {
expect(config.getDomainSections('cinema').map(s=> s.id)).toEqual([2, 10]);
});
it("returns empty array if no matching sections are found", function() {
expect(config.getDomainSections('business').map(s=> s.id)).toEqual([]);
});
it("return all sections if domain slug is undefined", function() {
expect(config.getDomainSections(undefined).map(s=> s.id)).toEqual([2, 3, 10]);
});
it("return empty list if domain slug is null", function() {
expect(config.getDomainSections(null).map(s=> s.id)).toEqual([3]);
});
});
it("returns domains to the default domain if domain slug is missing", function (){
const config = Config.build({sections: [{id: 2, name: 'hollywood'}]});
expect(config.getDomainSections(null).map(s => s.id)).toEqual([2]);
expect(config.getDomainSections('cinema').map(s => s.id)).toEqual([2]);
expect(config.getDomainSections(undefined).map(s => s.id)).toEqual([2]);
})
describe("memoize", function() {
it("can memoize logic against some key", function () {
const config = Config.build({});
config.memoize("the-key", () => 1)
expect(config.memoize("the-key", () => 2)).toBe(1);
})
it("allows you to memoize an undefined result", function() {
const config = Config.build({});
config.memoize("the-key", () => undefined)
expect(config.memoize("the-key", () => true)).toBe(undefined);
})
})
describe("memoizeAsync", function() {
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
it("shares values with memoize", async function() {
const config = Config.build({});
config.memoize("the-key", () => 1)
expect(await config.memoizeAsync("the-key", () => 2)).toBe(1);
});
it("evaluates only a single time when called parallely", async function() {
const config = Config.build({});
let numberOfTimes = 0;
const c1 = config.memoizeAsync("the-key", async () => { await sleep(1); numberOfTimes = numberOfTimes + 1; return 42; });
const c2 = config.memoizeAsync("the-key", async () => { await sleep(1); numberOfTimes = numberOfTimes + 1; return 42; });
expect(await c1).toBe(42);
expect(await c2).toBe(42);
expect(numberOfTimes).toBe(1);
})
it("evaluates only a single time even when called serially", async function() {
const config = Config.build({});
let numberOfTimes = 0;
const c1 = config.memoizeAsync("the-key", async () => { await sleep(1); numberOfTimes = numberOfTimes + 1; return 42; });
expect(await c1).toBe(42);
const c2 = config.memoizeAsync("the-key", async () => { await sleep(1); numberOfTimes = numberOfTimes + 1; return 42; });
expect(await c2).toBe(42);
expect(numberOfTimes).toBe(1);
})
it("calls things once, but fails when called parallely with a failing function", async function() {
const config = Config.build({});
let numberOfTimes = 0;
const c1 = config.memoizeAsync("the-key", async () => { await sleep(1); numberOfTimes = numberOfTimes + 1; throw new Error("foobar"); });
const c2 = config.memoizeAsync("the-key", async () => { await sleep(1); numberOfTimes = numberOfTimes + 1; throw new Error("foobar"); });
expect(await c1.catch(e => e.message)).toBe("foobar");
expect(await c2.catch(e => e.message)).toBe("foobar");
expect(numberOfTimes).toBe(1);
})
it("calls things twice, but fails when called serially with a failing function", async function () {
const config = Config.build({});
let numberOfTimes = 0;
const c1 = config.memoizeAsync("the-key", async () => { await sleep(1); numberOfTimes = numberOfTimes + 1; throw new Error("foobar"); });
expect(await c1.catch(e => e.message)).toBe("foobar");
const c2 = config.memoizeAsync("the-key", async () => { await sleep(1); numberOfTimes = numberOfTimes + 1; throw new Error("foobar"); });
expect(await c2.catch(e => e.message)).toBe("foobar");
expect(numberOfTimes).toBe(2);
})
})
});