Skip to content
This repository has been archived by the owner on Oct 5, 2020. It is now read-only.

Commit

Permalink
Add tests for mapModel()
Browse files Browse the repository at this point in the history
  • Loading branch information
luludotdev committed Aug 14, 2018
1 parent 223438e commit 19ce967
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/helpers/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ const analyzeErrors = {
}

const generateErrors = {
invalidInput: new Error(`argument 'model' must be a Token[]`),
}

const mapModelErrors = {
invalidInput: new Error(`argument 'model' must be an Object literal`),
}

module.exports = { analyzeErrors, generateErrors }
module.exports = { analyzeErrors, generateErrors, mapModelErrors }
23 changes: 23 additions & 0 deletions src/helpers/mapModel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
const { mapModelErrors: errors } = require('./errors.js')

/**
* @typedef {Object} Token
* @property {string} token
* @property {number} count
* @property {boolean} start
* @property {Word[]} next
*/

/**
* @typedef {Object} Word
* @property {string} token
* @property {number} weight
*/

/**
* @param {Object} model Raw Model
* @returns {Token[]}
*/
const mapModel = model => {
// Input Validation
if (model.constructor !== Object) throw errors.invalidInput

let output = Object.entries(model)
.map(x => {
let [key, value] = x
Expand Down
32 changes: 31 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { expect } = require('chai')

// Local Package
const { analyze, generate, mapModel } = require('../src/index.js')
const { analyzeErrors, generateErrors } = require('../src/helpers/errors.js')
const { analyzeErrors, generateErrors, mapModelErrors } = require('../src/helpers/errors.js')

// Test Data
const { testInput, testModel, testMapped } = require('./data.js')
Expand Down Expand Up @@ -53,6 +53,36 @@ describe('analyze()', () => {
})
})

describe('mapModel()', () => {
it('should be a function', () => {
expect(mapModel).to.be.a('function')
})

describe('input types', () => {
it('should accept an Object literal', () => {
expect(() => { mapModel(testModel) }).to.not.throw()
})

it('should throw when given a string', () => {
expect(() => { mapModel('string') }).to.throw(mapModelErrors.invalidInput)
})

it('should throw when given a number', () => {
expect(() => { mapModel(5) }).to.throw(mapModelErrors.invalidInput)
})

it('should throw when given an Array', () => {
expect(() => { mapModel([]) }).to.throw(mapModelErrors.invalidInput)
})
})

describe('return types', () => {
it('should return an Object', () => {
expect(mapModel(testModel)).to.be.an('array')
})
})
})

describe('generate()', () => {
it('should be a function', () => {
expect(generate).to.be.a('function')
Expand Down

0 comments on commit 19ce967

Please sign in to comment.