Skip to content

Commit

Permalink
✨ Required properties cannot be empty
Browse files Browse the repository at this point in the history
  • Loading branch information
thiamsantos committed Apr 2, 2017
1 parent 2378868 commit aafaa9c
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 12 deletions.
14 changes: 11 additions & 3 deletions lib/seotopper.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
const utils = require('./utils')

const checkMissingKeys = utils.checkMissingKeys
const createErrorMessage = utils.createErrorMessage
const requiredProperties = utils.requiredProperties
const checkMissingKeys = utils.checkMissingKeys
const createMissingKeysErrorMessage = utils.createMissingKeysErrorMessage
const checkEmptyKeys = utils.checkEmptyKeys
const createEmptyKeysErrorMessage = utils.createEmptyKeysErrorMessage

function seotopper(args) {
const keys = Object.keys(args)
const missingKeys = checkMissingKeys(requiredProperties, keys)

if (missingKeys.length > 0) {
throw new Error(createErrorMessage(missingKeys))
throw new Error(createMissingKeysErrorMessage(missingKeys))
}

const emptyKeys = checkEmptyKeys(requiredProperties, keys, args)

if (emptyKeys.length > 0) {
throw new Error(createEmptyKeysErrorMessage(emptyKeys))
}

return `<title>${args.title}</title>
Expand Down
18 changes: 15 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ const requiredProperties = [
const checkMissingKeys = (requiredProperties, keys) => requiredProperties
.filter(property => keys.indexOf(property) === -1)

const createErrorMessage = missingKeys => String(
const createMissingKeysErrorMessage = missingKeys => String(
'The following ' +
(missingKeys.length > 1 ? 'properties are' : 'property is') +
' required: ' +
missingKeys.join(', ')
missingKeys.sort().join(', ')
)

const checkEmptyKeys = (requiredProperties, keys, args) => keys
.filter(key => requiredProperties.indexOf(key) !== -1)
.filter(key => args[key] === '')

const createEmptyKeysErrorMessage = emptyKeys => String(
'The following ' +
(emptyKeys.length > 1 ? 'properties' : 'property') +
' cannot be empty: ' +
emptyKeys.sort().join(', ')
)
module.exports = {
checkMissingKeys,
checkEmptyKeys,
requiredProperties,
createErrorMessage
createMissingKeysErrorMessage,
createEmptyKeysErrorMessage
}
91 changes: 91 additions & 0 deletions test/seotopper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,94 @@ test('should give feedback for more than one required property', t => {
})
}, 'The following properties are required: author, description, title')
})

test('title cannot be empty', t => {
t.throws(() => {
seotopper({
title: '',
description: 'Descrição da minha página',
author: 'Eu',
canonical: 'https://sua-url.com.br',
robots: 'index/follow',
image: 'https://sua-url.com.br/images/intro.jpg'
})
}, 'The following property cannot be empty: title')
})

test('description cannot be empty', t => {
t.throws(() => {
seotopper({
title: 'Título da minha página',
description: '',
author: 'Eu',
canonical: 'https://sua-url.com.br',
robots: 'index/follow',
image: 'https://sua-url.com.br/images/intro.jpg'
})
}, 'The following property cannot be empty: description')
})

test('author cannot be empty', t => {
t.throws(() => {
seotopper({
title: 'Título da minha página',
description: 'Descrição da minha página',
author: '',
canonical: 'https://sua-url.com.br',
robots: 'index/follow',
image: 'https://sua-url.com.br/images/intro.jpg'
})
}, 'The following property cannot be empty: author')
})

test('canonical cannot be empty', t => {
t.throws(() => {
seotopper({
title: 'Título da minha página',
author: 'Eu',
description: 'Descrição da minha página',
canonical: '',
robots: 'index/follow',
image: 'https://sua-url.com.br/images/intro.jpg'
})
}, 'The following property cannot be empty: canonical')
})

test('robots cannot be empty', t => {
t.throws(() => {
seotopper({
title: 'Título da minha página',
author: 'Eu',
description: 'Descrição da minha página',
canonical: 'https://sua-url.com.br',
robots: '',
image: 'https://sua-url.com.br/images/intro.jpg'
})
}, 'The following property cannot be empty: robots')
})

test('image cannot be empty', t => {
t.throws(() => {
seotopper({
title: 'Título da minha página',
author: 'Eu',
description: 'Descrição da minha página',
canonical: 'https://sua-url.com.br',
robots: 'index/follow',
image: ''
})
}, 'The following property cannot be empty: image')
})

test('should give feedback for more than one empty property', t => {
t.throws(() => {
seotopper({
title: '',
description: '',
author: '',
canonical: 'https://sua-url.com.br',
robots: 'index/follow',
image: 'https://sua-url.com.br/images/intro.jpg'
})
}, 'The following properties cannot be empty: author, description, title')
})
42 changes: 36 additions & 6 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const test = require('ava')
const {
requiredProperties,
checkMissingKeys,
createErrorMessage
createMissingKeysErrorMessage,
checkEmptyKeys,
createEmptyKeysErrorMessage
} = require('../lib/utils')

test('requiredProperties should be an array', t => {
Expand All @@ -18,16 +20,44 @@ test('checkMissingKeys', t => {
t.deepEqual(actual, expected, 'should return the missing keys')
})

test('createErrorMessage missing one key', t => {
const actual = createErrorMessage(['title'])
test('createMissingKeysErrorMessage missing one key', t => {
const actual = createMissingKeysErrorMessage(['title'])
const expected = 'The following property is required: title'

t.is(actual, expected)
})

test('createErrorMessage missign more than one key', t => {
const actual = createErrorMessage(['title', 'author'])
const expected = 'The following properties are required: title, author'
test('createMissingKeysErrorMessage missing more than one key', t => {
const actual = createMissingKeysErrorMessage(['title', 'author'])
const expected = 'The following properties are required: author, title'

t.is(actual, expected)
})

test('checkEmptyKeys', t => {
const actual = checkEmptyKeys(
['title', 'author'],
['title', 'author'],
{
title: '',
author: 'me'
}
)
const expected = ['title']

t.deepEqual(actual, expected)
})

test('createEmptyKeysErrorMessage with one empty key', t => {
const actual = createEmptyKeysErrorMessage(['title'])
const expected = 'The following property cannot be empty: title'

t.is(actual, expected)
})

test('createEmptyKeysErrorMessage with more than one empty key', t => {
const actual = createEmptyKeysErrorMessage(['title', 'author'])
const expected = 'The following properties cannot be empty: author, title'

t.is(actual, expected)
})

0 comments on commit aafaa9c

Please sign in to comment.