Skip to content

Commit

Permalink
feat: refactor and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Dec 1, 2017
1 parent 56e1408 commit cff7036
Show file tree
Hide file tree
Showing 25 changed files with 6,276 additions and 334 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ notifications:
before_script:
- npm install
node_js:
- "0.10"
- 8
53 changes: 0 additions & 53 deletions Gruntfile.js

This file was deleted.

25 changes: 12 additions & 13 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 JS Bin Ltd
Copyright (c) 2017 JS Bin Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions __tests__/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = Object.assign(require('@remy/eslint/jest'), {});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions test/fixtures/simple.html → __tests__/fixtures/simple.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf8">
<meta charset="utf-8">
<title>Just a simple example</title>
</head>

<body>
<p>Nicely done sir.</p>
</body>
</html>

</html>
10 changes: 5 additions & 5 deletions test/fixtures/simple.js → __tests__/fixtures/simple.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var object = {
downloadBin: function (req, res, next) {
downloadBin: function(req, res, next) {
this.protectVisibility(req.session.user, req.bin, function(err, bin) {
var filename = ['jsbin', bin.url, bin.revision, 'html'].join('.');

var data = {
domain: helpers.set('url host'),
permalink: helpers.editUrlForBin(bin, true),
user: undefsafe(bin, 'metadata.name') || false,
year: (new Date()).getYear() + 1900
year: new Date().getYear() + 1900,
};
};
}
};
});
},
};
53 changes: 53 additions & 0 deletions __tests__/full.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';
/*global describe, it, beforeEach */
var assert = require('assert');
var toFile = require('../');
var fs = require('fs');
var path = require('path');
var cheerio = require('cheerio');

describe('full bin insert', function() {
var html = '';

beforeEach(function() {
html = fs.readFileSync(
path.join(__dirname, 'fixtures', 'simple.html'),
'utf8'
);
});

it('should also store source panel content', function() {
var javascript = 'alert("Hello world");';
var css = 'body { background: red; }';
var meta = '<!-- test -->';
var modifiedHTML = html + '<!-- tested -->\n<script>alert("foo");</script>';
var file = toFile({
html: html,
javascript: javascript,
css: css,
meta: meta,
source: {
html: modifiedHTML,
javascript: javascript + '\n// tested',
css: css + '\n/* tested */',
},
});

var $ = cheerio.load(file, { xmlMode: false, decodeEntities: false });
var sourceHTML = $('#jsbin-source-html').text();

assert(
sourceHTML.indexOf('<\\!-- tested -->') !== -1,
'source HTML is present'
);

$ = cheerio.load(
sourceHTML.replace(/<\\!--/, '<!--').replace(/<\\\/script>/i, '</script>')
);

assert(
$.html().trim() === modifiedHTML.trim(),
'pulling HTML back out is correct'
);
});
});
File renamed without changes.
37 changes: 23 additions & 14 deletions test/insert.test.js → __tests__/insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,43 @@ var toFile = require('../');
var fs = require('fs');
var path = require('path');

describe('content insert', function () {
describe('content insert', function() {
var html = '';

beforeEach(function () {
html = fs.readFileSync(path.join(__dirname, 'fixtures', 'simple.html'), 'utf8');
beforeEach(function() {
html = fs.readFileSync(
path.join(__dirname, 'fixtures', 'simple.html'),
'utf8'
);
});

it('should insert JS before the closing body', function () {
it('should insert JS before the closing body', function() {
var javascript = 'alert("Hello world");';
var file = toFile({ html: html, javascript: javascript });
var file = toFile({ html, javascript });

assert(file.indexOf(javascript) !== -1, 'contains the javascript: ' + file);

var lines = file.split('\n');
var pos = lines.indexOf(javascript);
assert(lines[pos + 2].indexOf('</body>') === 0, lines.join('\n'));
var pos = lines.findIndex(line => line.includes(javascript));
assert(lines[pos + 2].indexOf('</body>') === 0);
});

it('should insert CSS before the closing head', function () {
it('should insert CSS before the closing head', function() {
var css = 'body { background: red; }';
var file = toFile({ html: html, css: css });

assert(file.indexOf(css) !== -1, 'contains the css: ' + file);

var lines = file.split('\n');
var pos = lines.indexOf(css);
assert(lines[pos + 2].indexOf('</head>') === 0, lines[pos+2]);
assert(lines[pos + 2].indexOf('</head>') === 0, lines[pos + 2]);
});

it('should load after <title>', function () {
var html = fs.readFileSync(path.join(__dirname, 'fixtures', 'barebones.html'), 'utf8');
it('should load after <title>', function() {
var html = fs.readFileSync(
path.join(__dirname, 'fixtures', 'barebones.html'),
'utf8'
);
var css = 'body { background: red; }';
var file = toFile({ html: html, css: css });

Expand All @@ -47,8 +53,11 @@ describe('content insert', function () {
assert(lines[pos + 2].indexOf('</title>') === 0, lines[pos + 2]);
});

it('should insert JS at end when missing </body>', function () {
var html = fs.readFileSync(path.join(__dirname, 'fixtures', 'barebones.html'), 'utf8');
it('should insert JS at end when missing </body>', function() {
var html = fs.readFileSync(
path.join(__dirname, 'fixtures', 'barebones.html'),
'utf8'
);
var javascript = 'alert("Hello world");';
var file = toFile({ html: html, javascript: javascript });

Expand All @@ -58,4 +67,4 @@ describe('content insert', function () {

assert(lines.slice(-1)[0] === '</script>', '???: ' + lines.slice(-2));
});
});
});
50 changes: 50 additions & 0 deletions __tests__/integrity.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';
/*global describe, it, beforeEach, after */
var w3cjs = require('w3cjs');
var toFile = require('../');
var fs = require('fs');
var path = require('path');

describe('integrity', function() {
var filename = path.join(
__dirname,
'fixtures',
'tmp-' + ((Math.random() * 1000) | 0) + '.html'
);
var html = '';

beforeEach(function() {
html = fs.readFileSync(
path.join(__dirname, 'fixtures', 'simple.html'),
'utf8'
);
});

afterAll(function() {
fs.unlinkSync(filename);
});

it('should create valid document with just HTML, CSS & JS', function(done) {
var javascript = 'alert("Hello world");';
var css = 'body { background-color: red; }';
var meta = '<!-- created during a test -->';
var file = toFile({
html: html,
javascript: javascript,
css: css,
meta: meta,
});

fs.writeFileSync(filename, file, 'utf8');

w3cjs.validate({
file: filename,
callback: function(res) {
if (res.messages && res.messages.length > 0) {
throw { error: 'html errors have been found', results: res };
}
done();
},
});
});
});
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions dist/bin-to-file.min.js

This file was deleted.

Loading

0 comments on commit cff7036

Please sign in to comment.