Skip to content

Commit 0b2b44a

Browse files
committed
Add Environment unit tests
- Refs #338
1 parent f86e6b3 commit 0b2b44a

File tree

6 files changed

+241
-6
lines changed

6 files changed

+241
-6
lines changed

.sassdocrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"jsesc": "^0.5.0",
110110
"jshint": "^2.5.10",
111111
"mocha": "^2.0.1",
112+
"sinon": "^1.12.2",
112113
"vinyl": "^0.4.6"
113114
},
114115
"engines": {

test/annotations/defaults.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('#defaults', function () {
2424
});
2525
});
2626

27-
it('should assign the proper default values', function () {
27+
it('should assign proper default values', function () {
2828
assert.deepEqual(['undefined'], dummy.group);
2929
assert.strictEqual('public', dummy.access);
3030
});

test/env/environment.test.js

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
var assert = require('assert');
6+
var sinon = require('sinon');
7+
var rimraf = require('rimraf');
8+
var mock = require('../mock');
9+
var Environment = require('../../dist/environment');
10+
11+
describe('#environment', function () {
12+
var warnings = [];
13+
var logger;
14+
var env;
15+
16+
beforeEach(function () {
17+
logger = new mock.Logger(true);
18+
env = new Environment(logger, false);
19+
warnings = logger.output;
20+
});
21+
22+
/**
23+
* Passed in config file.
24+
*/
25+
describe('#config', function () {
26+
var configPath = path.join(__dirname, '../fixture/config.json');
27+
var configFile = require(configPath);
28+
29+
beforeEach(function () {
30+
env.load(configPath);
31+
env.postProcess();
32+
});
33+
34+
it('should properly process a passed in config file', function () {
35+
assert.ok(path.basename(env.file) === 'config.json');
36+
assert.deepEqual(env.display, configFile.display);
37+
});
38+
});
39+
40+
/**
41+
* Passed in wrong type config file.
42+
*/
43+
describe('#config-fail', function () {
44+
var spy = sinon.spy();
45+
46+
beforeEach(function () {
47+
env.on('error', spy);
48+
env.load(123); // @TOO [] pass
49+
env.postProcess();
50+
});
51+
52+
it('should error if config is of a wrong format', function () {
53+
assert.ok(spy.called);
54+
});
55+
});
56+
57+
/**
58+
* Passed in undefined config file.
59+
*/
60+
describe('#config-fail', function () {
61+
beforeEach(function () {
62+
env.load('fail.json');
63+
env.postProcess();
64+
});
65+
66+
it('should warn if config file is not found', function () {
67+
assert.ok(path.basename(env.file) === '.sassdocrc');
68+
assert.ok(warnings[0].includes('Config file `fail.json` not found'));
69+
assert.ok(warnings[1].includes('Falling back to `.sassdocrc'));
70+
});
71+
});
72+
73+
/**
74+
* Default .sassdocrc process.
75+
*/
76+
describe('#sassdocrc', function () {
77+
var sdrc;
78+
79+
var config = {
80+
display: {
81+
access: ['public', 'private'],
82+
alias: true,
83+
watermark: true
84+
}
85+
};
86+
87+
beforeEach(function () {
88+
sdrc = new mock.SassDocRc(config);
89+
90+
return sdrc.dump().then(function () {
91+
env.load();
92+
env.postProcess();
93+
});
94+
});
95+
96+
it('should default to .sassdocrc', function () {
97+
assert.ok(warnings.length === 0);
98+
assert.ok(path.basename(env.file) === '.sassdocrc');
99+
assert.deepEqual(env.display, config.display);
100+
});
101+
102+
after(function () {
103+
return sdrc.clean();
104+
});
105+
});
106+
107+
/**
108+
* A config.package is passed but fails.
109+
*/
110+
describe('#package-fail', function () {
111+
var spy = sinon.spy();
112+
113+
beforeEach(function () {
114+
env.on('warning', spy);
115+
env.load({ package: 'should/fail.json' });
116+
env.postProcess();
117+
});
118+
119+
it('should warn if package file is not found and load CWD package.json', function () {
120+
assert.ok(spy.called);
121+
assert.ok(env.package.name === 'sassdoc');
122+
assert.ok(warnings[0].includes('should/fail.json` not found'));
123+
assert.ok(warnings[1].includes('Falling back to `package.json`'));
124+
});
125+
});
126+
127+
/**
128+
* Render default theme.
129+
*/
130+
describe('#theme-default', function () {
131+
beforeEach(function () {
132+
env.load();
133+
env.postProcess();
134+
env.data = [];
135+
return env.theme('.sassdoc', env);
136+
});
137+
138+
it('should render the default theme', function () {
139+
assert.ok(env.themeName === 'default');
140+
assert.ok(fs.existsSync('.sassdoc/index.html'));
141+
assert.ok(fs.existsSync('.sassdoc/assets'));
142+
});
143+
144+
after(function (done) {
145+
rimraf('.sassdoc', done);
146+
});
147+
});
148+
149+
/**
150+
* A config.theme is passed but fails.
151+
*/
152+
describe('#theme-fail', function () {
153+
beforeEach(function () {
154+
env.load({ theme: 'fail' });
155+
env.postProcess();
156+
env.data = [];
157+
return env.theme('.sassdoc', env);
158+
});
159+
160+
it('should warn and render the default theme', function () {
161+
assert.ok(warnings[0].includes('Theme `fail` not found'));
162+
assert.ok(warnings[1].includes('Falling back to default theme'));
163+
// assert.ok(env.themeName === 'default'); // @TODO ??
164+
assert.ok(fs.existsSync('.sassdoc/index.html'));
165+
assert.ok(fs.existsSync('.sassdoc/assets'));
166+
});
167+
168+
after(function (done) {
169+
rimraf('.sassdoc', done);
170+
});
171+
});
172+
173+
});

test/fixture/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"display": {
3+
"access": ["public", "private"],
4+
"alias": false,
5+
"watermark": true
6+
}
7+
}

test/mock.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
require('./init');
4+
5+
var path = require('path');
6+
var fs = require('fs');
7+
var inherits = require('util').inherits;
8+
var Writable = require('stream').Writable;
9+
var Logger = require('../dist/logger');
10+
var utils = require('../dist/utils');
11+
var writeFile = utils.denodeify(fs.writeFile);
12+
var unlink = utils.denodeify(fs.unlink);
13+
var is = utils.is;
14+
15+
function SassDocRc(config) {
16+
this._filePath = path.join(process.cwd(), '.sassdocrc');
17+
this._contents = config || {};
18+
}
19+
20+
SassDocRc.prototype.dump = function () {
21+
return writeFile(this._filePath, JSON.stringify(this._contents));
22+
};
23+
24+
SassDocRc.prototype.clean = function () {
25+
return unlink(this._filePath);
26+
};
27+
28+
Object.defineProperty(SassDocRc.prototype, 'contents', {
29+
get: function () {
30+
return this._contents;
31+
},
32+
set: function (config) {
33+
if (!is.plainObject(config)) {
34+
throw new Error('SassDocRc.contents can only be an Object.');
35+
}
36+
this._contents = config;
37+
}
38+
});
39+
40+
module.exports.SassDocRc = SassDocRc;
41+
42+
function MockLogger() {
43+
Logger.call(this, arguments);
44+
this._output = [];
45+
this._stderr = new Writable();
46+
this._stderr._write = function (chunk, enc, cb) {
47+
this._output.push(chunk.toString());
48+
cb();
49+
}.bind(this);
50+
}
51+
inherits(MockLogger, Logger);
52+
53+
Object.defineProperty(MockLogger.prototype, 'output', {
54+
get: function () {
55+
return this._output;
56+
}
57+
});
58+
59+
module.exports.Logger = MockLogger;

0 commit comments

Comments
 (0)