Added unit tests for "parser.js" and "util.js"

This commit is contained in:
euvl
2017-08-07 16:07:57 +01:00
parent 72f5222e4b
commit 0089717a8c
10 changed files with 186 additions and 7 deletions

43
test/unit/karma.conf.js Normal file
View File

@@ -0,0 +1,43 @@
// Karma configuration
var webpackConfig = require('../../build/webpack.test.config.js')
module.exports = function(config) {
config.set({
frameworks: [
'mocha'
],
files: [
'./tests.webpack.js'
],
browsers: [
'Chrome'
],
preprocessors: {
'./tests.webpack.js': ['webpack', 'sourcemap']
},
reporters: ['spec'],
plugins: [
// Launchers
'karma-chrome-launcher',
// Test Libraries
'karma-mocha',
'karma-sinon-chai',
// Preprocessors
'karma-webpack',
'karma-sourcemap-loader',
// Reporters
'karma-spec-reporter',
'karma-coverage'
],
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
},
logLevel: config.LOG_INFO,
singleRun: true,
concurrency: Infinity,
})
}

69
test/unit/parser.spec.js Normal file
View File

@@ -0,0 +1,69 @@
var parser = require('../../src/parser')
var { expect } = require('chai')
describe('parser.js', () => {
describe('#parse', () => {
describe('Correct types', () => {
it('Should parse numbers', () => {
let object = parser.parse(10)
expect(object.value).to.be.a('number')
expect(object.type).to.be.a('string')
expect(object.value).to.equal(10)
expect(object.type).to.equal('px')
})
it('Should parse strings', () => {
let object = parser.parse('10')
expect(object.value).to.be.a('number')
expect(object.type).to.be.a('string')
expect(object.value).to.equal(10)
expect(object.type).to.equal('px')
})
it ('Should parse "auto" string, auto => {type: "auto", value: 0}', () => {
let object = parser.parse('auto')
expect(object.value).to.equal(0)
expect(object.type).to.equal('auto')
})
it ('Should parse wrong types', () => {
let nullValue = parser.parse(null)
let booleanValue = parser.parse(false)
expect(nullValue.value).to.equal(null)
expect(nullValue.type).to.equal('')
expect(booleanValue.value).to.equal(false)
expect(booleanValue.type).to.equal('')
})
})
describe('Parsing suffixed string', () => {
it ('Should parse "px"', () => {
let object = parser.parse('10px')
expect(object.value).to.equal(10)
expect(object.type).to.equal('px')
})
it ('Should parse "%"', () => {
let object = parser.parse('10%')
expect(object.value).to.equal(10)
expect(object.type).to.equal('%')
})
it ('Should not parse "%px"', () => {
let object = parser.parse('10%px')
expect(object.value).to.be.a('string')
expect(object.type).to.equal('')
})
})
})
})

View File

@@ -0,0 +1,2 @@
var testsContext = require.context('.', true, /\.spec$/)
testsContext.keys().forEach(testsContext)

26
test/unit/util.spec.js Normal file
View File

@@ -0,0 +1,26 @@
var util = require('../../src/util')
var { expect } = require('chai')
describe('util.js', () => {
describe('#inRange', () => {
it('Should return the source value (in the range)', () => {
let value = util.inRange(-10, 10, 0)
expect(value).to.equal(0)
})
it('Should not be equal to the source value (outside the range)', () => {
let value = util.inRange(-10, 10, -100)
expect(value).not.to.equal(-100)
})
it('Should replace source value with upper limit', () => {
let value = util.inRange(0, 10, 30)
expect(value).to.equal(10)
})
it('Should replace source value with lower limit', () => {
let value = util.inRange(1, 10, -1)
expect(value).to.equal(1)
})
})
})