Skip to content

Commit

Permalink
test(project): Refactor due to coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin LONGEARET committed Mar 2, 2016
1 parent cd30152 commit 31d8f8d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 37 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"commit": "git cz",
"test": "mocha tests -w --compilers js:babel-register",
"test:single": "babel-node node_modules/.bin/babel-istanbul cover _mocha -- tests -R spec",
"check-coverage": "istanbul check-coverage --statements 50 --branches 35 --functions 75 --lines 50",
"check-coverage": "istanbul check-coverage --statements 99 --branches 92 --functions 100 --lines 99",
"report-coverage": "cat ./coverage/lcov.info | coveralls",
"prebuild": "rm -Rf lib && mkdir lib",
"build": "babel -d lib/ src/",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"engines": {
"node": ">= 0.8.0"
"node": ">= 4.0.0"
},
"devDependencies": {
"babel-cli": "6.5.1",
Expand Down
41 changes: 7 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import {JsonAdapter} from './adapter/json-adapter.js'

var extractor
(function() {
var _basePath, _log, _utils
var _log, _utils

var Extractor = function (data, option) {
this.data = data

// Grab NODE_ENV to set debug flag!
var debug = process.env.NODE_ENV === 'debug'
_log = new Log(option.log || (debug ? 'debug' : 'info'))
_basePath = option ? (option.basePath || __dirname) : __dirname

this.basePath = (option && option.basePath) || __dirname
_utils = new Utils({
basePath: _basePath
basePath: this.basePath
})

// Check lang parameter
Expand Down Expand Up @@ -227,38 +228,9 @@ var extractor
return currentArray
}

/**
* Recurse feed translation object (utility for namespace)
* INPUT: {"NS1": {"NS2": {"VAL1": "", "VAL2": ""} } }
* OUTPUT: {"NS1": {"NS2": {"VAL1": "NS1.NS2.VAL1", "VAL2": "NS1.NS2.VAL2"} } }
* @param {Object} data
* @param {string?} path
* @private
*/
var _recurseFeedDefaultNamespace = function (data, path) {
var path = path || ''
if (_.isObject(data)) {
for (var key in data) {
if (_.isObject(data)) {
data[ key ] = _recurseFeedDefaultNamespace(data[ key ], path != '' ? path + '.' + key : key)
}
}
return data
} else {
if (data == null && data == "") {
// return default data if empty/null
return path
} else {

return data
}
}
}

/**
* Start extraction of translations
*/

// Check directory exist
try {
fs.statSync(dest)
Expand Down Expand Up @@ -336,16 +308,17 @@ var extractor

switch(adapter) {
case 'pot':
var toPot = new PotAdapter(_log, _basePath)
var toPot = new PotAdapter(_log, this.basePath)
toPot.init(params)
_translation.persist(toPot)
break
default:
var toJson = new JsonAdapter(_log, _basePath)
var toJson = new JsonAdapter(_log, this.basePath)
toJson.init(params)
_translation.persist(toJson)
}

return this
}

module.exports = {
Expand Down
14 changes: 14 additions & 0 deletions tests/adapter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {expect} from 'chai'
import Adapter from './../src/adapter/adapter'

describe('adapter', () => {
it('should throw error when try to call the non-implemented persist method', () => {
let adapter = new Adapter(console, './')
let result = true
try {
adapter.persist()
result = false
} catch(e) {}
expect(result).to.be.true
})
})
47 changes: 46 additions & 1 deletion tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import preset from './preset.conf.js'
import pkg from '../package.json'

describe('Extractor', () => {
let workingDir = path.resolve(__dirname, '..')

describe('Interface', () => {
it('should expose the current version', () => {
expect(AngularTranslateExtractor.VERSION).to.be.a('string')
Expand All @@ -23,8 +25,51 @@ describe('Extractor', () => {
})
})

describe('extract()', () => {

it('should throw an error when no lang in params', () => {
let result = true
try {
AngularTranslateExtractor.extract({}, {
"basePath": workingDir,
"log": false
})
result = false
} catch (e) {}
expect(result).to.be.true
})

it('should run in debug mode', () => {
let result = true
let oldNodeEnv = process.env.NODE_ENV
process.env.NODE_ENV = 'debug'
try {
AngularTranslateExtractor.extract({
lang: ['fr_FR'],
dest: 'tmp'
}, {
"basePath": workingDir,
"log": false
})
} catch(e) {
result = false
}
expect(result).to.be.true
process.env.NODE_ENV = oldNodeEnv
})

it('should set default basePath if not given as input', () => {
let extractor = AngularTranslateExtractor.extract({
lang: ['fr_FR'],
dest: 'tmp'
}, {"log": false})
expect(extractor.basePath).to.be.a('string')
expect(extractor.basePath).equals(path.resolve(path.dirname('./src/index.js')))
})

})

describe('Preset', () => {
let workingDir = path.resolve(__dirname, '..')
let getContent = (name, tmpDirectory = 'tmp/', expectedDirectory = 'tests/expected/') => {
let encoding = {"encoding": 'utf8'}
let tmp = fs.readFileSync(path.resolve(workingDir, tmpDirectory + name), encoding)
Expand Down
14 changes: 14 additions & 0 deletions tests/translations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@ describe('Translations', () => {
"NS.SNS2": ""
})
})

it('should return merged translations (safeMode: false & useDefault:null)', () => {
translations.setTranslations({
"NS.SNS1": "my translations",
"NS.SNS2": ""
})
let result = translations.getMergedTranslations({
"NS.SNS1": "my new translations"
})
expect(result).to.deep.equal({
"NS.SNS1": "my new translations",
"NS.SNS2": ""
})
})
it('should take care about given link (@:)', () => {
translations.setTranslations({
"NS.SNS1": "my translations",
Expand Down

0 comments on commit 31d8f8d

Please sign in to comment.