From 0b2b44a0d6152c48ff843e591bfbaf2f269c0d10 Mon Sep 17 00:00:00 2001 From: Pascal Duez Date: Wed, 28 Jan 2015 23:30:31 +0100 Subject: [PATCH] Add `Environment` unit tests - Refs #338 --- .sassdocrc | 5 - package.json | 1 + test/annotations/defaults.test.js | 2 +- test/env/environment.test.js | 173 ++++++++++++++++++++++++++++++ test/fixture/config.json | 7 ++ test/mock.js | 59 ++++++++++ 6 files changed, 241 insertions(+), 6 deletions(-) delete mode 100644 .sassdocrc create mode 100644 test/env/environment.test.js create mode 100644 test/fixture/config.json create mode 100644 test/mock.js diff --git a/.sassdocrc b/.sassdocrc deleted file mode 100644 index ac3c476f..00000000 --- a/.sassdocrc +++ /dev/null @@ -1,5 +0,0 @@ -display: - access: - - public - - private - alias: true diff --git a/package.json b/package.json index 94a0df3d..0e90438d 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,7 @@ "jsesc": "^0.5.0", "jshint": "^2.5.10", "mocha": "^2.0.1", + "sinon": "^1.12.2", "vinyl": "^0.4.6" }, "engines": { diff --git a/test/annotations/defaults.test.js b/test/annotations/defaults.test.js index 56345c08..3971de8b 100644 --- a/test/annotations/defaults.test.js +++ b/test/annotations/defaults.test.js @@ -24,7 +24,7 @@ describe('#defaults', function () { }); }); - it('should assign the proper default values', function () { + it('should assign proper default values', function () { assert.deepEqual(['undefined'], dummy.group); assert.strictEqual('public', dummy.access); }); diff --git a/test/env/environment.test.js b/test/env/environment.test.js new file mode 100644 index 00000000..047ca93f --- /dev/null +++ b/test/env/environment.test.js @@ -0,0 +1,173 @@ +'use strict'; + +var fs = require('fs'); +var path = require('path'); +var assert = require('assert'); +var sinon = require('sinon'); +var rimraf = require('rimraf'); +var mock = require('../mock'); +var Environment = require('../../dist/environment'); + +describe('#environment', function () { + var warnings = []; + var logger; + var env; + + beforeEach(function () { + logger = new mock.Logger(true); + env = new Environment(logger, false); + warnings = logger.output; + }); + + /** + * Passed in config file. + */ + describe('#config', function () { + var configPath = path.join(__dirname, '../fixture/config.json'); + var configFile = require(configPath); + + beforeEach(function () { + env.load(configPath); + env.postProcess(); + }); + + it('should properly process a passed in config file', function () { + assert.ok(path.basename(env.file) === 'config.json'); + assert.deepEqual(env.display, configFile.display); + }); + }); + + /** + * Passed in wrong type config file. + */ + describe('#config-fail', function () { + var spy = sinon.spy(); + + beforeEach(function () { + env.on('error', spy); + env.load(123); // @TOO [] pass + env.postProcess(); + }); + + it('should error if config is of a wrong format', function () { + assert.ok(spy.called); + }); + }); + + /** + * Passed in undefined config file. + */ + describe('#config-fail', function () { + beforeEach(function () { + env.load('fail.json'); + env.postProcess(); + }); + + it('should warn if config file is not found', function () { + assert.ok(path.basename(env.file) === '.sassdocrc'); + assert.ok(warnings[0].includes('Config file `fail.json` not found')); + assert.ok(warnings[1].includes('Falling back to `.sassdocrc')); + }); + }); + + /** + * Default .sassdocrc process. + */ + describe('#sassdocrc', function () { + var sdrc; + + var config = { + display: { + access: ['public', 'private'], + alias: true, + watermark: true + } + }; + + beforeEach(function () { + sdrc = new mock.SassDocRc(config); + + return sdrc.dump().then(function () { + env.load(); + env.postProcess(); + }); + }); + + it('should default to .sassdocrc', function () { + assert.ok(warnings.length === 0); + assert.ok(path.basename(env.file) === '.sassdocrc'); + assert.deepEqual(env.display, config.display); + }); + + after(function () { + return sdrc.clean(); + }); + }); + + /** + * A config.package is passed but fails. + */ + describe('#package-fail', function () { + var spy = sinon.spy(); + + beforeEach(function () { + env.on('warning', spy); + env.load({ package: 'should/fail.json' }); + env.postProcess(); + }); + + it('should warn if package file is not found and load CWD package.json', function () { + assert.ok(spy.called); + assert.ok(env.package.name === 'sassdoc'); + assert.ok(warnings[0].includes('should/fail.json` not found')); + assert.ok(warnings[1].includes('Falling back to `package.json`')); + }); + }); + + /** + * Render default theme. + */ + describe('#theme-default', function () { + beforeEach(function () { + env.load(); + env.postProcess(); + env.data = []; + return env.theme('.sassdoc', env); + }); + + it('should render the default theme', function () { + assert.ok(env.themeName === 'default'); + assert.ok(fs.existsSync('.sassdoc/index.html')); + assert.ok(fs.existsSync('.sassdoc/assets')); + }); + + after(function (done) { + rimraf('.sassdoc', done); + }); + }); + + /** + * A config.theme is passed but fails. + */ + describe('#theme-fail', function () { + beforeEach(function () { + env.load({ theme: 'fail' }); + env.postProcess(); + env.data = []; + return env.theme('.sassdoc', env); + }); + + it('should warn and render the default theme', function () { + assert.ok(warnings[0].includes('Theme `fail` not found')); + assert.ok(warnings[1].includes('Falling back to default theme')); + // assert.ok(env.themeName === 'default'); // @TODO ?? + assert.ok(fs.existsSync('.sassdoc/index.html')); + assert.ok(fs.existsSync('.sassdoc/assets')); + }); + + after(function (done) { + rimraf('.sassdoc', done); + }); + }); + +}); diff --git a/test/fixture/config.json b/test/fixture/config.json new file mode 100644 index 00000000..74511877 --- /dev/null +++ b/test/fixture/config.json @@ -0,0 +1,7 @@ +{ + "display": { + "access": ["public", "private"], + "alias": false, + "watermark": true + } +} diff --git a/test/mock.js b/test/mock.js new file mode 100644 index 00000000..d4651c6b --- /dev/null +++ b/test/mock.js @@ -0,0 +1,59 @@ +'use strict'; + +require('./init'); + +var path = require('path'); +var fs = require('fs'); +var inherits = require('util').inherits; +var Writable = require('stream').Writable; +var Logger = require('../dist/logger'); +var utils = require('../dist/utils'); +var writeFile = utils.denodeify(fs.writeFile); +var unlink = utils.denodeify(fs.unlink); +var is = utils.is; + +function SassDocRc(config) { + this._filePath = path.join(process.cwd(), '.sassdocrc'); + this._contents = config || {}; +} + +SassDocRc.prototype.dump = function () { + return writeFile(this._filePath, JSON.stringify(this._contents)); +}; + +SassDocRc.prototype.clean = function () { + return unlink(this._filePath); +}; + +Object.defineProperty(SassDocRc.prototype, 'contents', { + get: function () { + return this._contents; + }, + set: function (config) { + if (!is.plainObject(config)) { + throw new Error('SassDocRc.contents can only be an Object.'); + } + this._contents = config; + } +}); + +module.exports.SassDocRc = SassDocRc; + +function MockLogger() { + Logger.call(this, arguments); + this._output = []; + this._stderr = new Writable(); + this._stderr._write = function (chunk, enc, cb) { + this._output.push(chunk.toString()); + cb(); + }.bind(this); +} +inherits(MockLogger, Logger); + +Object.defineProperty(MockLogger.prototype, 'output', { + get: function () { + return this._output; + } +}); + +module.exports.Logger = MockLogger;