diff --git a/lib/seotopper.js b/lib/seotopper.js
index 525f778..61259b7 100644
--- a/lib/seotopper.js
+++ b/lib/seotopper.js
@@ -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 `
${args.title}
diff --git a/lib/utils.js b/lib/utils.js
index 68836d2..689628b 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -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
}
diff --git a/test/seotopper.test.js b/test/seotopper.test.js
index 46657fb..fc26526 100644
--- a/test/seotopper.test.js
+++ b/test/seotopper.test.js
@@ -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')
+})
diff --git a/test/utils.test.js b/test/utils.test.js
index 9107da2..9adc97f 100644
--- a/test/utils.test.js
+++ b/test/utils.test.js
@@ -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 => {
@@ -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)
})