Created
November 26, 2018 13:25
-
-
Save franzwong/9f06a5cb5f5e73753d6f50b43cf7ee50 to your computer and use it in GitHub Desktop.
Js unit test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function head(x) { | |
if (typeof x === 'string' && x.length > 0) { | |
return x.charAt(0) | |
} else if (Array.isArray(x) && x.length > 0) { | |
return x[0] | |
} | |
return null | |
} | |
module.exports = { | |
head | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const head = require('./head').head | |
const assert = require('chai').assert | |
describe('head', function() { | |
it('returns first item if x is array', function() { | |
assert.equal(1, head([1, 2, 3])) | |
}) | |
it('returns null if x is array but length is 0', function() { | |
assert.isNull(head([])) | |
}) | |
it('returns first character if x is string', function() { | |
assert.equal('a', head('abc')) | |
}) | |
it('returns null if x is string but length is 0', function() { | |
assert.isNull(head('')) | |
}) | |
it('returns null if neither x is null', function() { | |
assert.isNull(head(null)) | |
}) | |
it('returns null if neither x is string nor array', function() { | |
assert.isNull(head(123)) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment