Skip to content

Instantly share code, notes, and snippets.

@zaguiini
Last active December 6, 2017 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaguiini/3bbeb276e2d4c4569a88f48995508559 to your computer and use it in GitHub Desktop.
Save zaguiini/3bbeb276e2d4c4569a88f48995508559 to your computer and use it in GitHub Desktop.
A test-proved, one-liner function that returns "red" if the input number is below or equal 300, "yellow" if it is below or equal 700, or "green" if it is above 700. From 0 to 1000.
// a test-proved, one-liner function that returns:
// "red" if the input number is below or equal 300,
// "yellow" if it is below or equal 700, or
// "green" if it is above 700.
// from 0 to 1000.
const getScoreColor = s => (s === undefined || s === null || s.toString() !== '0' && !parseInt(s) || +s < 0 || +s > 1000) && new Error('invalid input') || +s <= 300 && 'red' || +s <= 700 && 'yellow' || 'green'
// make sure you have jest installed globally.
test('it should throw an invalid input error', () => {
expect(getScoreColor(true)).toEqual(Error('invalid input'))
})
test('it should throw an invalid input error', () => {
expect(getScoreColor(false)).toEqual(Error('invalid input'))
})
test('it should throw an invalid input error', () => {
expect(getScoreColor(undefined)).toEqual(Error('invalid input'))
})
test('it should throw an invalid input error', () => {
expect(getScoreColor(null)).toEqual(Error('invalid input'))
})
test('it should throw an invalid input error', () => {
expect(getScoreColor(NaN)).toEqual(Error('invalid input'))
})
test('it should throw an invalid input error', () => {
expect(getScoreColor([])).toEqual(Error('invalid input'))
})
test('it should throw an invalid input error', () => {
expect(getScoreColor({})).toEqual(Error('invalid input'))
})
test('it should throw an invalid input error', () => {
expect(getScoreColor(-1)).toEqual(Error('invalid input'))
})
test('it should return red', () => {
expect(getScoreColor(0)).toBe('red')
})
test('it should return red', () => {
expect(getScoreColor(300)).toBe('red')
})
test('it should return yellow', () => {
expect(getScoreColor(301)).toBe('yellow')
})
test('it should return yellow', () => {
expect(getScoreColor(700)).toBe('yellow')
})
test('it should return green', () => {
expect(getScoreColor(701)).toBe('green')
})
test('it should return green', () => {
expect(getScoreColor(1000)).toBe('green')
})
test('it should throw an invalid input error', () => {
expect(getScoreColor(1001)).toEqual(Error('invalid input'))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment