Skip to content

Instantly share code, notes, and snippets.

@franzwong
Created November 26, 2018 13:25
Show Gist options
  • Save franzwong/9f06a5cb5f5e73753d6f50b43cf7ee50 to your computer and use it in GitHub Desktop.
Save franzwong/9f06a5cb5f5e73753d6f50b43cf7ee50 to your computer and use it in GitHub Desktop.
Js unit test
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
}
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