Security update to Drupal 8.4.6
[yaffs-website] / node_modules / phridge / test / Page.test.js
1 "use strict";
2
3 /* global pages, config */
4
5 var chai = require("chai");
6 var Page = require("../lib/Page.js");
7 var expect = chai.expect;
8 var phridge = require("../lib/main.js");
9 var slow = require("./helpers/slow.js");
10
11 require("./helpers/setup.js");
12
13 describe("Page", function () {
14
15     describe(".prototype", function () {
16         var phantom;
17         var page;
18
19         function createPage() {
20             page = phantom.createPage();
21         }
22
23         function disposePage() {
24             page.dispose();
25         }
26
27         before(slow(function () {
28             return phridge.spawn({}).then(function (newPhantom) {
29                 phantom = newPhantom;
30             });
31         }));
32
33         after(slow(function () {
34             return phantom.dispose();
35         }));
36
37         describe(".constructor(phantom, id)", function () {
38
39             it("should return an instance of Page with the given arguments applied", function () {
40                 page = new Page(phantom, 1);
41                 expect(page).to.be.an.instanceOf(Page);
42                 expect(page.phantom).to.equal(phantom);
43                 expect(page._id).to.equal(1);
44             });
45
46         });
47
48         describe(".phantom", function () {
49
50             it("should be null by default", function () {
51                 expect(Page.prototype.phantom).to.equal(null);
52             });
53
54         });
55
56         describe(".run(arg1, arg2, arg3, fn)", function () {
57
58             before(createPage);
59             after(disposePage);
60
61             describe("with fn being an asynchronous function", function () {
62
63                 it("should provide a resolve function", function () {
64                     return expect(page.run(function (resolve) {
65                         resolve("everything ok");
66                     })).to.eventually.equal("everything ok");
67                 });
68
69                 it("should provide the possibility to resolve with any stringify-able data", function () {
70                     return Promise.all([
71                         expect(page.run(function (resolve) {
72                             resolve();
73                         })).to.eventually.equal(undefined),
74                         expect(page.run(function (resolve) {
75                             resolve(true);
76                         })).to.eventually.equal(true),
77                         expect(page.run(function (resolve) {
78                             resolve(2);
79                         })).to.eventually.equal(2),
80                         expect(page.run(function (resolve) {
81                             resolve(null);
82                         })).to.eventually.equal(null),
83                         expect(page.run(function (resolve) {
84                             resolve([1, 2, 3]);
85                         })).to.eventually.deep.equal([1, 2, 3]),
86                         expect(page.run(function (resolve) {
87                             resolve({
88                                 someArr: [1, 2, 3],
89                                 otherObj: {}
90                             });
91                         })).to.eventually.deep.equal({
92                             someArr: [1, 2, 3],
93                             otherObj: {}
94                         })
95                     ]);
96                 });
97
98                 it("should provide a reject function", function () {
99                     return page.run(function (resolve, reject) {
100                         reject(new Error("not ok"));
101                     }).catch(function (err) {
102                         expect(err.message).to.equal("not ok");
103                     });
104                 });
105
106             });
107
108             describe("with fn being a synchronous function", function () {
109
110                 it("should resolve to the returned value", function () {
111                     return expect(page.run(function () {
112                         return "everything ok";
113                     })).to.eventually.equal("everything ok");
114                 });
115
116                 it("should provide the possibility to resolve with any stringify-able data", function () {
117                     return Promise.all([
118                         expect(page.run(function () {
119                             // returns undefined
120                         })).to.eventually.equal(undefined),
121                         expect(page.run(function () {
122                             return true;
123                         })).to.eventually.equal(true),
124                         expect(page.run(function () {
125                             return 2;
126                         })).to.eventually.equal(2),
127                         expect(page.run(function () {
128                             return null;
129                         })).to.eventually.equal(null),
130                         expect(page.run(function () {
131                             return [1, 2, 3];
132                         })).to.eventually.deep.equal([1, 2, 3]),
133                         expect(page.run(function () {
134                             return {
135                                 someArr: [1, 2, 3],
136                                 otherObj: {}
137                             };
138                         })).to.eventually.deep.equal({
139                             someArr: [1, 2, 3],
140                             otherObj: {}
141                         })
142                     ]);
143                 });
144
145                 it("should reject the promise if fn throws an error", function () {
146                     return page.run(function () {
147                         throw new Error("not ok");
148                     }).catch(function (err) {
149                         expect(err.message).to.equal("not ok");
150                     });
151                 });
152
153             });
154
155             it("should provide all phantomjs default modules as convenience", function () {
156                 return expect(page.run(function () {
157                     return Boolean(webpage && system && fs && webserver && child_process); // eslint-disable-line
158                 })).to.eventually.equal(true);
159             });
160
161             it("should provide the config object to store all kind of configuration", function () {
162                 return expect(page.run(function () {
163                     return config;
164                 })).to.eventually.deep.equal({});
165             });
166
167             it("should provide the possibility to pass params", function () {
168                 var params = {
169                     some: ["param"],
170                     withSome: "crazy",
171                     values: {
172                         number1: 1
173                     }
174                 };
175
176                 return expect(page.run(params, params, params, function (params1, params2, params3) {
177                     return [params1, params2, params3];
178                 })).to.eventually.deep.equal([params, params, params]);
179             });
180
181             it("should report errors", function () {
182                 return expect(page.run(function () {
183                     undefinedVariable; // eslint-disable-line
184                 })).to.be.rejectedWith("Can't find variable: undefinedVariable");
185             });
186
187             it("should preserve all error details like stack traces", function () {
188                 return Promise.all([
189                     phantom
190                         .run(function brokenFunction() {
191                             undefinedVariable; // eslint-disable-line
192                         }).catch(function (err) {
193                             expect(err).to.have.property("message", "Can't find variable: undefinedVariable");
194                             expect(err).to.have.property("stack");
195                             //console.log(err.stack);
196                         }),
197                     phantom
198                         .run(function (resolve, reject) {
199                             reject(new Error("Custom Error"));
200                         })
201                         .catch(function (err) {
202                             expect(err).to.have.property("message", "Custom Error");
203                             expect(err).to.have.property("stack");
204                             //console.log(err.stack);
205                         })
206                 ]);
207             });
208
209             it("should run the function with the page as context", function () {
210                 return page.run(/** @this WebPage */function () {
211                     if (!this.clipRect) {
212                         throw new Error("The function's context is not the web page");
213                     }
214                 });
215             });
216
217         });
218
219         describe(".dispose()", function () {
220
221             beforeEach(createPage);
222
223             it("should remove the page from the pages-object", function () {
224                 var pageId = page._id;
225
226                 function checkForPage(pageId) {
227                     if (pages[pageId]) {
228                         throw new Error("page is still present in the page-object");
229                     }
230                 }
231
232                 return page.dispose().then(function () {
233                     return phantom.run(pageId, checkForPage);
234                 });
235             });
236
237             it("should remove the phantom reference", function () {
238                 return page.dispose().then(function () {
239                     expect(page.phantom).to.equal(null);
240                 });
241             });
242
243         });
244
245     });
246
247 });
248