Skip to content

Commit

Permalink
Add Environment unit tests
Browse files Browse the repository at this point in the history
- Refs #338
  • Loading branch information
pascalduez committed Jan 28, 2015
1 parent f86e6b3 commit 0b2b44a
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 6 deletions.
5 changes: 0 additions & 5 deletions .sassdocrc

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion test/annotations/defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
173 changes: 173 additions & 0 deletions test/env/environment.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

});
7 changes: 7 additions & 0 deletions test/fixture/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"display": {
"access": ["public", "private"],
"alias": false,
"watermark": true
}
}
59 changes: 59 additions & 0 deletions test/mock.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 0b2b44a

Please sign in to comment.