Skip to content

Commit 4e4ce79

Browse files
committed
test(staging): add util and staging tests
Adds tests to staging and util areas of the codebase
1 parent 29ffe64 commit 4e4ce79

File tree

6 files changed

+182
-5
lines changed

6 files changed

+182
-5
lines changed

common/util.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,42 @@ function getParsedPackageJsonFromPath(path) {
4242
* Test if the passed argument is an array
4343
*/
4444
function isArray(arr) {
45-
return arr.constructor === Array;
45+
if(typeof arr === "undefined")
46+
{
47+
return false;
48+
} else if (arr === null) {
49+
return false;
50+
} else {
51+
return arr.constructor === Array;
52+
}
4653
}
4754

4855
/**
4956
* Test if the passed argument is a function
5057
*/
5158
function isFunction(functionToCheck) {
52-
var getType = {};
53-
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
59+
if(typeof functionToCheck === "undefined")
60+
{
61+
return false;
62+
} else if (functionToCheck === null) {
63+
return false;
64+
} else {
65+
var getType = {};
66+
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
67+
}
5468
}
5569

5670
/**
5771
* Test if the passed argument is a string
5872
*/
5973
function isString(str) {
60-
return Object.prototype.toString.call(str) == '[object String]';
74+
if(typeof str === "undefined")
75+
{
76+
return false;
77+
} else if (str === null) {
78+
return false;
79+
} else {
80+
return Object.prototype.toString.call(str) == '[object String]';
81+
}
6182
}
6283

src/git/commit.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function commit(sh, repoPath, message, options, done) {
2020

2121
// Write progress to the screen
2222
.on('data',function(data) {
23+
24+
// Ignore this for code coverage since it is only there
25+
// to make our testing suite pretty
26+
/* istanbul ignore if */
2327
if(!options.quiet) {
2428
if(isString(data))
2529
{

test/tests/commit-missing-adapter.js

Whitespace-only changes.

test/tests/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
import './commit';
12
import './init';
2-
import './commit';
3+
import './staging';
4+
import './util';

test/tests/staging.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {expect} from 'chai';
2+
import path from 'path';
3+
import fs from 'fs';
4+
5+
6+
// Bootstrap our tester
7+
import {bootstrap} from '../tester';
8+
9+
// Get our source files
10+
import {init as gitInit, addPath as gitAdd, log} from '../../src/git';
11+
import {init as commitizenInit, staging} from '../../src/commitizen';
12+
13+
// Destructure some things for cleaner tests
14+
let { config, sh, repo, clean, util, files } = bootstrap();
15+
let { writeFilesToPath } = files;
16+
17+
before(function() {
18+
// Creates the temp path
19+
clean.before(sh, config.paths.tmp);
20+
});
21+
22+
beforeEach(function() {
23+
this.timeout(config.maxTimeout); // this could take a while
24+
repo.createEndUser(sh, config.paths.endUserRepo);
25+
});
26+
27+
describe('commit', function() {
28+
29+
it('should determine if a repo is clean', function(done) {
30+
31+
this.timeout(config.maxTimeout); // this could take a while
32+
33+
// SETUP
34+
35+
// Describe a repo and some files to add and commit
36+
let repoConfig = {
37+
path: config.paths.endUserRepo,
38+
files: {
39+
dummyfile: {
40+
contents: `duck-duck-goose`,
41+
filename: `mydummyfile.txt`,
42+
},
43+
gitignore: {
44+
contents: `node_modules/`,
45+
filename: `.gitignore`
46+
}
47+
}
48+
};
49+
50+
gitInit(sh, repoConfig.path);
51+
52+
staging.isClean(repoConfig.path, function(stagingIsClean) {
53+
54+
expect(stagingIsClean).to.be.true;
55+
56+
writeFilesToPath(repoConfig.files, repoConfig.path);
57+
58+
gitAdd(sh, repoConfig.path);
59+
60+
staging.isClean(repoConfig.path, function(afterWriteStagingIsClean) {
61+
expect(afterWriteStagingIsClean).to.be.false;
62+
done();
63+
});
64+
65+
});
66+
67+
});
68+
69+
});
70+
71+
afterEach(function() {
72+
// All this should do is archive the tmp path to the artifacts
73+
clean.afterEach(sh, config.paths.tmp, config.preserve);
74+
});
75+
76+
after(function() {
77+
// Once everything is done, the artifacts should be cleaned up based on
78+
// the preserve setting in the config
79+
clean.after(sh, config.paths.tmp, config.preserve);
80+
});

test/tests/util.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {expect} from 'chai';
2+
import {isArray, isFunction, isString} from '../../common/util';
3+
4+
describe('common util', function() {
5+
6+
it('isArray determines if array is passed', function() {
7+
8+
// Truthies
9+
expect(isArray([])).to.be.true;
10+
expect(isArray([1, 2, 3])).to.be.true;
11+
expect(isArray([1, , 3])).to.be.true;
12+
expect(isArray(new Array())).to.be.true;
13+
14+
// Falsies
15+
expect(isArray(undefined)).to.be.false;
16+
expect(isArray(null)).to.be.false;
17+
expect(isArray(49)).to.be.false;
18+
expect(isArray(function(){})).to.be.false;
19+
expect(isArray({})).to.be.false;
20+
expect(isArray("asdf")).to.be.false;
21+
expect(isArray(true)).to.be.false;
22+
expect(isArray(false)).to.be.false;
23+
expect(isArray(Symbol())).to.be.false;
24+
25+
});
26+
27+
it('isFunction determines if a function is passed', function() {
28+
29+
// Truthies
30+
expect(isFunction(function(){})).to.be.true;
31+
expect(isFunction(new Function())).to.be.true;
32+
33+
// Falsies
34+
expect(isFunction(undefined)).to.be.false;
35+
expect(isFunction(null)).to.be.false;
36+
expect(isFunction(49)).to.be.false;
37+
expect(isFunction([])).to.be.false;
38+
expect(isFunction({})).to.be.false;
39+
expect(isFunction("asdf")).to.be.false;
40+
expect(isFunction(true)).to.be.false;
41+
expect(isFunction(false)).to.be.false;
42+
expect(isFunction(Symbol())).to.be.false;
43+
44+
});
45+
46+
it('isString determines if string is passed', function() {
47+
48+
// Truthies
49+
expect(isString('a single quoted string')).to.be.true;
50+
expect(isString("a double quoted string")).to.be.true;
51+
expect(isString(`
52+
a multi
53+
line
54+
string`
55+
)).to.be.true;
56+
expect(isString(new String())).to.be.true;
57+
58+
// Falsies
59+
expect(isString(function(){})).to.be.false;
60+
expect(isString(undefined)).to.be.false;
61+
expect(isString(null)).to.be.false;
62+
expect(isString(49)).to.be.false;
63+
expect(isString([])).to.be.false;
64+
expect(isString({})).to.be.false;
65+
expect(isString(true)).to.be.false;
66+
expect(isString(false)).to.be.false;
67+
expect(isString(Symbol())).to.be.false;
68+
69+
});
70+
});

0 commit comments

Comments
 (0)