From e781d6d2b1705f44fcdd950f5541917fde41323f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Wed, 27 Apr 2016 17:58:35 +0200 Subject: [PATCH] Add test for assets inliner --- docs/README.md | 2 ++ jest/customMatchers.js | 24 ++++++++++++++ lib/models/output.js | 31 ++++++++++++------- lib/output/ebook/index.js | 12 ++++--- lib/output/ebook/onFinish.js | 19 ++++++++++++ lib/output/ebook/onInit.js | 21 +++++++++++++ lib/output/ebook/onPage.js | 8 ++--- .../modifiers/__tests__/fetchRemoteImages.js | 25 +++++++++++++++ lib/output/modifiers/__tests__/svgToImg.js | 25 +++++++++++++++ lib/output/website/createTemplateEngine.js | 1 - lib/output/website/onPage.js | 2 +- package.json | 10 +++--- 12 files changed, 155 insertions(+), 25 deletions(-) create mode 100644 jest/customMatchers.js create mode 100644 lib/output/ebook/onFinish.js create mode 100644 lib/output/ebook/onInit.js create mode 100644 lib/output/modifiers/__tests__/fetchRemoteImages.js create mode 100644 lib/output/modifiers/__tests__/svgToImg.js diff --git a/docs/README.md b/docs/README.md index d62623173..16917dda9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,5 +1,7 @@ # GitBook Toolchain Documentation +![image](https://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg) + This document aims to be a comprehensive guide to GitBook. It contains the full documentation for version **{{ book.version }}**. Help for GitBook.com specific questions can be found at [help.gitbook.com](https://help.gitbook.com). ### What is GitBook? diff --git a/jest/customMatchers.js b/jest/customMatchers.js new file mode 100644 index 000000000..a0dd7f96b --- /dev/null +++ b/jest/customMatchers.js @@ -0,0 +1,24 @@ +var path = require('path'); +var fs = require('fs'); + +var matchers = { + /** + Verify that a file exists in a directory + */ + toHaveFile: function () { + return { + compare: function (actual, expected) { + var filePath = path.join(actual, expected); + var exists = fs.existsSync(filePath); + + return { + pass: exists + }; + } + }; + } +}; + +jasmine.getEnv().beforeEach(function () { + jasmine.addMatchers(matchers); +}); \ No newline at end of file diff --git a/lib/models/output.js b/lib/models/output.js index c11017e12..43e36f8d6 100644 --- a/lib/models/output.js +++ b/lib/models/output.js @@ -61,6 +61,26 @@ Output.prototype.getRoot = function() { return this.getOptions().get('root'); }; +/** + Update state of output + + @param {Map} newState + @return {Output} +*/ +Output.prototype.setState = function(newState) { + return this.set('state', newState); +}; + +/** + Update options + + @param {Map} newOptions + @return {Output} +*/ +Output.prototype.setOptions = function(newOptions) { + return this.set('options', newOptions); +}; + /** Return logegr for this output (same as book) @@ -70,15 +90,4 @@ Output.prototype.getLogger = function() { return this.getBook().getLogger(); }; -/** - Update state of output - - @param {Output} output - @param {Map} newState - @return {Output} -*/ -Output.updateState = function(output, newState) { - return output.set('state', newState); -}; - module.exports = Output; diff --git a/lib/output/ebook/index.js b/lib/output/ebook/index.js index 46a94e3f1..3aaa7e778 100644 --- a/lib/output/ebook/index.js +++ b/lib/output/ebook/index.js @@ -1,5 +1,9 @@ +var extend = require('extend'); +var websiteGenerator = require('../website'); -module.exports = { - - -}; +module.exports = extend({}, websiteGenerator, { + name: 'ebook', + onInit: require('./onInit'), + onPage: require('./onPage'), + onFinish: require('./onFinish') +}); diff --git a/lib/output/ebook/onFinish.js b/lib/output/ebook/onFinish.js new file mode 100644 index 000000000..5ea514838 --- /dev/null +++ b/lib/output/ebook/onFinish.js @@ -0,0 +1,19 @@ +var websiteGenerator = require('../website'); + +/** + Finish the generation, generate the ebook file using ebook-convert + + @param {Output} + @return {Output} +*/ +function onFinish(output) { + return websiteGenerator.onFinish(output) + .then(function(resultOutput) { + + // todo + + return resultOutput; + }); +} + +module.exports = onFinish; diff --git a/lib/output/ebook/onInit.js b/lib/output/ebook/onInit.js new file mode 100644 index 000000000..9cee141a4 --- /dev/null +++ b/lib/output/ebook/onInit.js @@ -0,0 +1,21 @@ +var websiteGenerator = require('../website'); + +/** + Initialize the generator + + @param {Output} + @return {Output} +*/ +function onInit(output) { + return websiteGenerator.onInit(output) + .then(function(resultOutput) { + var options = resultOutput.getOptions(); + + options = options.set('directoryIndex', false); + options = options.set('prefix', 'ebook'); + + return resultOutput.setOptions(options); + }); +} + +module.exports = onInit; diff --git a/lib/output/ebook/onPage.js b/lib/output/ebook/onPage.js index ab1513365..13edf173f 100644 --- a/lib/output/ebook/onPage.js +++ b/lib/output/ebook/onPage.js @@ -1,18 +1,18 @@ var website = require('../website'); -var Modifier = require('../modifier'); +var Modifiers = require('../modifiers'); /** Write a page for ebook output @param {Output} output - @param {Page} page + @param {Output} */ function onPage(output, page) { var options = output.getOptions(); // Inline assets - return Modifier.modifyHTML(page, [ - Modifier.inlineAssets(options.get('root')) + return Modifiers.modifyHTML(page, [ + Modifiers.inlineAssets(options.get('root')) ]) // Write page using website generator diff --git a/lib/output/modifiers/__tests__/fetchRemoteImages.js b/lib/output/modifiers/__tests__/fetchRemoteImages.js new file mode 100644 index 000000000..543aca09c --- /dev/null +++ b/lib/output/modifiers/__tests__/fetchRemoteImages.js @@ -0,0 +1,25 @@ +var cheerio = require('cheerio'); +var tmp = require('tmp'); + +describe('fetchRemoteImages', function() { + var dir; + var fetchRemoteImages = require('../fetchRemoteImages'); + + beforeEach(function() { + dir = tmp.dirSync(); + }); + + pit('should download image file', function() { + var $ = cheerio.load(''); + + return fetchRemoteImages(dir.name, $) + .then(function() { + var $img = $('img'); + var src = '.' + $img.attr('src'); + + expect(dir.name).toHaveFile(src); + }); + }); +}); + + diff --git a/lib/output/modifiers/__tests__/svgToImg.js b/lib/output/modifiers/__tests__/svgToImg.js new file mode 100644 index 000000000..762a02e3b --- /dev/null +++ b/lib/output/modifiers/__tests__/svgToImg.js @@ -0,0 +1,25 @@ +var cheerio = require('cheerio'); +var tmp = require('tmp'); + +describe('svgToImg', function() { + var dir; + var svgToImg = require('../svgToImg'); + + beforeEach(function() { + dir = tmp.dirSync(); + }); + + pit('should write svg as a file', function() { + var $ = cheerio.load(''); + + return svgToImg(dir.name, $) + .then(function() { + var $img = $('img'); + var src = '.' + $img.attr('src'); + + expect(dir.name).toHaveFile(src); + }); + }); +}); + + diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js index 7d8724804..24990b651 100644 --- a/lib/output/website/createTemplateEngine.js +++ b/lib/output/website/createTemplateEngine.js @@ -35,7 +35,6 @@ function createTemplateEngine(output, currentFile) { var i18n = state.getI18n(); var config = book.getConfig(); var summary = book.getSummary(); - var pages = output.getPages(); var outputFolder = output.getRoot(); // Search paths for templates diff --git a/lib/output/website/onPage.js b/lib/output/website/onPage.js index ddae986a4..751a4307e 100644 --- a/lib/output/website/onPage.js +++ b/lib/output/website/onPage.js @@ -53,7 +53,7 @@ function onPage(output, page) { gitbook: context.gitbook, basePath: basePath, book: { - language: null // context.book.language + language: book.getLanguage() } }; } diff --git a/package.json b/package.json index c35c7f98f..7840e8133 100644 --- a/package.json +++ b/package.json @@ -63,9 +63,7 @@ }, "devDependencies": { "eslint": "2.7.0", - "jest-cli": "^11.0.2", - "mocha": "2.4.5", - "should": "8.3.0" + "jest-cli": "^11.0.2" }, "scripts": { "test": "node_modules/.bin/jest --bail", @@ -97,5 +95,9 @@ "name": "Samy Pessé", "email": "samy@gitbook.com" } - ] + ], + "jest": { + "automock": false, + "setupTestFrameworkScriptFile": "/jest/customMatchers.js" + } }