UNPKG

12.3 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __asyncValues = (this && this.__asyncValues) || function (o) {
16 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
17 var m = o[Symbol.asyncIterator], i;
18 return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
19 function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
20 function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
21};
22var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
23var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
24 if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
25 var g = generator.apply(thisArg, _arguments || []), i, q = [];
26 return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
27 function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
28 function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
29 function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
30 function fulfill(value) { resume("next", value); }
31 function reject(value) { resume("throw", value); }
32 function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
33};
34Object.defineProperty(exports, "__esModule", { value: true });
35const chai_1 = require("chai");
36const path = require("path");
37const vfs = require("vinyl-fs");
38const polymer_project_1 = require("../polymer-project");
39const streams_1 = require("../streams");
40/**
41 * A utility stream to check all files that pass through it for a file that
42 * matches the given push manifest file path. For that file, the stream asserts
43 * that it matches the expected push manifest contents. It will emit
44 * "match-success" & "match-failure" events for each test to listen to.
45 */
46class CheckPushManifest extends streams_1.AsyncTransformStream {
47 constructor(filePath, expectedManifest) {
48 super({ objectMode: true });
49 this.filePath = filePath;
50 this.expectedManifest = expectedManifest;
51 }
52 _transformIter(files) {
53 return __asyncGenerator(this, arguments, function* _transformIter_1() {
54 var e_1, _a;
55 let didAssert = false;
56 try {
57 for (var files_1 = __asyncValues(files), files_1_1; files_1_1 = yield __await(files_1.next()), !files_1_1.done;) {
58 const file = files_1_1.value;
59 if (this.filePath !== file.path) {
60 yield yield __await(file);
61 continue;
62 }
63 try {
64 const pushManifestContents = file.contents.toString();
65 const pushManifestJson = JSON.parse(pushManifestContents);
66 chai_1.assert.deepEqual(pushManifestJson, this.expectedManifest);
67 this.emit('match-success');
68 }
69 catch (err) {
70 this.emit('match-failure', err);
71 }
72 didAssert = true;
73 yield yield __await(file);
74 }
75 }
76 catch (e_1_1) { e_1 = { error: e_1_1 }; }
77 finally {
78 try {
79 if (files_1_1 && !files_1_1.done && (_a = files_1.return)) yield __await(_a.call(files_1));
80 }
81 finally { if (e_1) throw e_1.error; }
82 }
83 if (!didAssert) {
84 throw new Error(`never saw file ${this.filePath}`);
85 }
86 });
87 }
88}
89/**
90 * Utility function to set up the boilerplate for testing the
91 * project.addPushManifest() transform stream.
92 */
93function testPushManifest(project, manifestRelativePath, prefix, expectedManifest, done) {
94 const expectedManifestAbsolutePath = path.join(project.config.root, manifestRelativePath || 'push-manifest.json');
95 const pushManifestChecker = new CheckPushManifest(expectedManifestAbsolutePath, expectedManifest);
96 vfs.src(path.join(project.config.root, '**'))
97 .on('error', done)
98 .pipe(project.addPushManifest(manifestRelativePath, prefix))
99 .on('error', done)
100 .pipe(pushManifestChecker)
101 .on('data', () => { })
102 .on('match-success', done)
103 .on('match-failure', done)
104 .on('error', done);
105}
106suite('AddPushManifest', () => {
107 const testProjectRoot = path.resolve('test-fixtures/push-manifest-data');
108 test('with entrypoint-only config options', (done) => {
109 const project = new polymer_project_1.PolymerProject({
110 root: testProjectRoot,
111 entrypoint: 'entrypoint-only.html',
112 });
113 const expectedPushManifest = {
114 'entrypoint-only.html': {
115 'framework.html': {
116 type: 'document',
117 weight: 1,
118 },
119 },
120 };
121 testPushManifest(project, undefined, undefined, expectedPushManifest, done);
122 });
123 test('with entrypoint and fragments config options', (done) => {
124 const project = new polymer_project_1.PolymerProject({
125 root: testProjectRoot,
126 entrypoint: 'entrypoint-only.html',
127 fragments: ['entrypoint-b.html', 'entrypoint-c.html'],
128 sources: [
129 'framework.html',
130 'shell.html',
131 'entrypoint-a.html',
132 'entrypoint-b.html',
133 'entrypoint-c.html',
134 'common-dependency.html',
135 ],
136 });
137 const expectedPushManifest = {
138 'entrypoint-only.html': {
139 'framework.html': {
140 type: 'document',
141 weight: 1,
142 },
143 },
144 'common-dependency.html': {
145 'example-script.js': {
146 'type': 'script',
147 'weight': 1,
148 },
149 'example-style.css': {
150 'type': 'style',
151 'weight': 1,
152 }
153 },
154 'entrypoint-b.html': {
155 'common-dependency.html': {
156 type: 'document',
157 weight: 1,
158 },
159 'example-script.js': {
160 type: 'script',
161 weight: 1,
162 },
163 'example-style.css': {
164 type: 'style',
165 weight: 1,
166 },
167 },
168 'entrypoint-c.html': {},
169 };
170 testPushManifest(project, undefined, undefined, expectedPushManifest, done);
171 });
172 test('with full app-shell config options', (done) => {
173 const project = new polymer_project_1.PolymerProject({
174 root: testProjectRoot,
175 entrypoint: 'entrypoint-a.html',
176 shell: 'shell.html',
177 fragments: ['entrypoint-b.html', 'entrypoint-c.html'],
178 sources: [
179 'framework.html',
180 'shell.html',
181 'entrypoint-a.html',
182 'entrypoint-b.html',
183 'entrypoint-c.html',
184 'common-dependency.html',
185 ],
186 });
187 const expectedPushManifest = {
188 'shell.html': {
189 'framework.html': {
190 type: 'document',
191 weight: 1,
192 }
193 },
194 'common-dependency.html': {
195 'example-script.js': {
196 'type': 'script',
197 'weight': 1,
198 },
199 'example-style.css': {
200 'type': 'style',
201 'weight': 1,
202 }
203 },
204 'entrypoint-b.html': {
205 'common-dependency.html': {
206 type: 'document',
207 weight: 1,
208 },
209 'example-script.js': {
210 type: 'script',
211 weight: 1,
212 },
213 'example-style.css': {
214 type: 'style',
215 weight: 1,
216 },
217 },
218 'entrypoint-c.html': {}
219 };
220 testPushManifest(project, undefined, undefined, expectedPushManifest, done);
221 });
222 test('with custom file path', (done) => {
223 const project = new polymer_project_1.PolymerProject({
224 root: testProjectRoot,
225 entrypoint: 'entrypoint-a.html',
226 shell: 'shell.html',
227 fragments: ['entrypoint-b.html', 'entrypoint-c.html'],
228 sources: [
229 'framework.html',
230 'shell.html',
231 'entrypoint-a.html',
232 'entrypoint-b.html',
233 'entrypoint-c.html',
234 'common-dependency.html',
235 ],
236 });
237 const pushManifestRelativePath = 'custom/push-manifest/path.json';
238 const expectedPushManifest = {
239 'shell.html': {
240 'framework.html': {
241 type: 'document',
242 weight: 1,
243 }
244 },
245 'common-dependency.html': {
246 'example-script.js': {
247 'type': 'script',
248 'weight': 1,
249 },
250 'example-style.css': {
251 'type': 'style',
252 'weight': 1,
253 }
254 },
255 'entrypoint-b.html': {
256 'common-dependency.html': {
257 type: 'document',
258 weight: 1,
259 },
260 'example-script.js': {
261 type: 'script',
262 weight: 1,
263 },
264 'example-style.css': {
265 type: 'style',
266 weight: 1,
267 },
268 },
269 'entrypoint-c.html': {}
270 };
271 testPushManifest(project, pushManifestRelativePath, undefined, expectedPushManifest, done);
272 });
273 test('with prefix', (done) => {
274 const project = new polymer_project_1.PolymerProject({
275 root: testProjectRoot,
276 entrypoint: 'entrypoint-a.html',
277 shell: 'shell.html',
278 fragments: ['entrypoint-b.html'],
279 sources: [
280 'framework.html',
281 'common-dependency.html',
282 ],
283 });
284 const expectedPushManifest = {
285 'foo/shell.html': {
286 'foo/framework.html': {
287 type: 'document',
288 weight: 1,
289 }
290 },
291 'foo/entrypoint-b.html': {
292 'foo/common-dependency.html': {
293 type: 'document',
294 weight: 1,
295 },
296 'foo/example-script.js': {
297 type: 'script',
298 weight: 1,
299 },
300 'foo/example-style.css': {
301 type: 'style',
302 weight: 1,
303 },
304 },
305 };
306 testPushManifest(project, undefined, '/foo/', expectedPushManifest, done);
307 });
308});
309//# sourceMappingURL=push-manifest_test.js.map
\No newline at end of file