Skip to content

Commit

Permalink
Add tests for loading head.html
Browse files Browse the repository at this point in the history
  • Loading branch information
karl committed Apr 8, 2016
1 parent 908d405 commit d6794d1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 14 deletions.
25 changes: 25 additions & 0 deletions dist/server/get_head_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = function (configDirPath) {
var headHtmlPath = _path2.default.resolve(configDirPath, 'head.html');
var headHtml = '';
if (_fs2.default.existsSync(headHtmlPath)) {
headHtml = _fs2.default.readFileSync(headHtmlPath, 'utf8');
}

return headHtml;
};

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
4 changes: 2 additions & 2 deletions dist/server/iframe.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = function (extraHtml) {
return "\n <!DOCTYPE html>\n <html>\n <head>\n <title>React Storybook</title>\n " + extraHtml + "\n </head>\n <body>\n <div id=\"root\" />\n <script src=\"/static/preview.bundle.js\"></script>\n </body>\n </html>\n ";
exports.default = function (headHtml) {
return "\n <!DOCTYPE html>\n <html>\n <head>\n <title>React Storybook</title>\n " + headHtml + "\n </head>\n <body>\n <div id=\"root\" />\n <script src=\"/static/preview.bundle.js\"></script>\n </body>\n </html>\n ";
};
11 changes: 5 additions & 6 deletions dist/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _get_head_html = require('./get_head_html');

var _get_head_html2 = _interopRequireDefault(_get_head_html);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

process.env.NODE_ENV = 'production';
Expand Down Expand Up @@ -121,12 +125,6 @@ if (_fs2.default.existsSync(customConfigPath)) {
});
}

var headHtmlPath = _path2.default.resolve(configDirPath, 'head.html');
var headHtml = '';
if (_fs2.default.existsSync(headHtmlPath)) {
headHtml = _fs2.default.readFileSync(headHtmlPath);
}

var compiler = (0, _webpack2.default)(finalConfig);
var devMiddlewareOptions = {
noInfo: true,
Expand All @@ -139,6 +137,7 @@ app.get('/', function (req, res) {
res.send((0, _index2.default)());
});

var headHtml = (0, _get_head_html2.default)(configDirPath);
app.get('/iframe', function (req, res) {
res.send((0, _iframe2.default)(headHtml));
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"eslint-plugin-babel": "^3.1.0",
"eslint-plugin-react": "^4.2.3",
"mocha": "^2.4.5",
"mock-fs": "^3.8.0",
"nodemon": "^1.9.1",
"react": "^0.14.8",
"react-dom": "^0.14.8",
Expand Down
44 changes: 44 additions & 0 deletions src/server/__tests__/get_head_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { describe, it, beforeEach, afterEach } = global;
import { expect } from 'chai';
import getHeadHtml from '../get_head_html';
import mock from 'mock-fs';

const HEAD_HTML_CONTENTS = '<script>console.log("custom script!");</script>';

describe('server.getHeadHtml', () => {
describe('when .storybook/head.html does not exist', () => {
beforeEach(() => {
mock({
config: {},
});
});

afterEach(() => {
mock.restore();
});

it('return an empty string', () => {
const result = getHeadHtml('./config');
expect(result).to.be.equal('');
});
});

describe('when .storybook/head.html exists', () => {
beforeEach(() => {
mock({
config: {
'head.html': HEAD_HTML_CONTENTS,
},
});
});

afterEach(() => {
mock.restore();
});

it('return the contents of the file', () => {
const result = getHeadHtml('./config');
expect(result).to.be.equal(HEAD_HTML_CONTENTS);
});
});
});
12 changes: 12 additions & 0 deletions src/server/get_head_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'path';
import fs from 'fs';

export default function (configDirPath) {
const headHtmlPath = path.resolve(configDirPath, 'head.html');
let headHtml = '';
if (fs.existsSync(headHtmlPath)) {
headHtml = fs.readFileSync(headHtmlPath, 'utf8');
}

return headHtml;
}
8 changes: 2 additions & 6 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import packageJson from '../../package.json';
import config from './webpack.config';
import path from 'path';
import fs from 'fs';
import getHeadHtml from './get_head_html';

const logger = console;

Expand Down Expand Up @@ -94,12 +95,6 @@ if (fs.existsSync(customConfigPath)) {
};
}

const headHtmlPath = path.resolve(configDirPath, 'head.html');
let headHtml = '';
if (fs.existsSync(headHtmlPath)) {
headHtml = fs.readFileSync(headHtmlPath);
}

const compiler = webpack(finalConfig);
const devMiddlewareOptions = {
noInfo: true,
Expand All @@ -112,6 +107,7 @@ app.get('/', function (req, res) {
res.send(getIndexHtml());
});

const headHtml = getHeadHtml(configDirPath);
app.get('/iframe', function (req, res) {
res.send(getIframeHtml(headHtml));
});
Expand Down

0 comments on commit d6794d1

Please sign in to comment.