Created
January 27, 2017 15:35
-
-
Save larsgk/a9b6d1a0c38020d094238918f1cff415 to your computer and use it in GitHub Desktop.
Early addition of Polymer element inner-functional tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @license | |
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved. | |
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | |
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | |
* Code distributed by Google as part of the polymer project is also | |
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | |
*/ | |
'use strict'; | |
const del = require('del'); | |
const gulp = require('gulp'); | |
const gulpif = require('gulp-if'); | |
const imagemin = require('gulp-imagemin'); | |
const mergeStream = require('merge-stream'); | |
const polymerBuild = require('polymer-build'); | |
const swPrecacheConfig = require('./sw-precache-config.js'); | |
const polymerJson = require('./polymer.json'); | |
const polymerProject = new polymerBuild.PolymerProject(polymerJson); | |
const buildDirectory = 'build'; | |
/** | |
* Waits for the given ReadableStream | |
*/ | |
function waitFor(stream) { | |
return new Promise((resolve, reject) => { | |
stream.on('end', resolve); | |
stream.on('error', reject); | |
}); | |
} | |
function build() { | |
return new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars | |
// Okay, so first thing we do is clear the build directory | |
console.log(`Deleting ${buildDirectory} directory...`); | |
del([buildDirectory]) | |
.then(() => { | |
// Okay, now let's get your source files | |
let sourcesStream = polymerProject.sources() | |
// Oh, well do you want to minify stuff? Go for it! | |
// Here's how splitHtml & gulpif work | |
.pipe(polymerProject.splitHtml()) | |
// .pipe(gulpif(/\.js$/, uglify())) | |
// .pipe(gulpif(/\.css$/, cssSlam())) | |
// .pipe(gulpif(/\.html$/, htmlMinifier())) | |
.pipe(gulpif(/\.(png|gif|jpg|svg)$/, imagemin())) | |
.pipe(polymerProject.rejoinHtml()); | |
// Okay, now let's do the same to your dependencies | |
let dependenciesStream = polymerProject.dependencies() | |
.pipe(polymerProject.splitHtml()) | |
// .pipe(gulpif(/\.js$/, uglify())) | |
// .pipe(gulpif(/\.css$/, cssSlam())) | |
// .pipe(gulpif(/\.html$/, htmlMinifier())) | |
.pipe(polymerProject.rejoinHtml()); | |
// Okay, now let's merge them into a single build stream | |
let buildStream = mergeStream(sourcesStream, dependenciesStream) | |
.once('data', () => { | |
console.log('Analyzing build dependencies...'); | |
}); | |
// If you want bundling, pass the stream to polymerProject.bundler. | |
// This will bundle dependencies into your fragments so you can lazy | |
// load them. | |
buildStream = buildStream.pipe(polymerProject.bundler); | |
// Okay, time to pipe to the build directory | |
buildStream = buildStream.pipe(gulp.dest(buildDirectory)); | |
// waitFor the buildStream to complete | |
return waitFor(buildStream); | |
}) | |
.then(() => { | |
// Okay, now let's generate the Service Worker | |
console.log('Generating the Service Worker...'); | |
return polymerBuild.addServiceWorker({ | |
project: polymerProject, | |
buildRoot: buildDirectory, | |
bundled: true, | |
swPrecacheConfig: swPrecacheConfig | |
}); | |
}) | |
.then(() => { | |
// You did it! | |
console.log('Build complete!'); | |
resolve(); | |
}); | |
}); | |
} | |
gulp.task('build', build); | |
// unit testing below (work in progress) | |
const mocha = require('gulp-mocha'); | |
function unittest() { | |
return new Promise((resolve, reject) => { | |
let teststream = gulp.src(['unittest/*.js'], { read: false }) | |
.pipe(mocha({ reporter: 'list' })) | |
.on('end', resolve); | |
}); | |
} | |
gulp.task('unittest', unittest); | |
gulp.task('watch', () => { | |
gulp.watch(['src/**/*.html', 'unittest/*.js'], unittest); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
@license | |
Copyright (c) 2016 The Polymer Project Authors. All rights reserved. | |
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | |
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | |
Code distributed by Google as part of the polymer project is also | |
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | |
--> | |
<link rel="import" href="../bower_components/polymer/polymer.html"> | |
<link rel="import" href="shared-styles.html"> | |
<dom-module id="my-view1"> | |
<template> | |
<style include="shared-styles"> | |
:host { | |
display: block; | |
padding: 10px; | |
} | |
</style> | |
<div class="card"> | |
<div class="circle">1</div> | |
<h1>View One</h1> | |
<p>Ut labores minimum atomorum pro. Laudem tibique ut has.</p> | |
<p>Lorem ipsum dolor sit amet, per in nusquam nominavi periculis, sit elit oportere ea.Lorem ipsum dolor sit amet, per in nusquam nominavi periculis, sit elit oportere ea.Cu mei vide viris gloriatur, at populo eripuit sit.</p> | |
</div> | |
</template> | |
<script> | |
Polymer({ | |
is: 'my-view1', | |
myFunc:function(a,b) { | |
return a + b; | |
} | |
}); | |
</script> | |
</dom-module> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert'); | |
const gulp = require('gulp'); | |
const polymerBuild = require('polymer-build'); | |
const polymerJson = require('../polymer.json'); | |
const polymerProject = new polymerBuild.PolymerProject(polymerJson); | |
const filter = require('gulp-filter'); | |
const intercept = require('gulp-intercept'); | |
const jsFilter = filter('**/*.js'); | |
describe('testing view1', () => { | |
let polyobj; | |
before(done => { | |
let Polymer = data => polyobj = data; | |
gulp.src('./src/my-view1.html') | |
.pipe(polymerProject.splitHtml()) | |
.pipe(jsFilter) | |
.pipe(intercept(file => eval(file.contents.toString())) ) | |
.on('end', done); | |
}); | |
it('Adds correctly', () => { | |
assert(polyobj.myFunc(1,2) == 3); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment