diff --git a/package.json b/package.json index 9a8b3fef..4b261835 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ }, "scripts": { "lint": "eslint src --fix", - "test": "mocha --compilers js:babel-register --require babel-polyfill", + "test": "mocha --recursive --compilers js:babel-register --require babel-polyfill", "test-cov": "istanbul cover _mocha -- --compilers js:babel-register --require babel-polyfill", "test-coveralls": "istanbul cover _mocha --report lcovonly -- --compilers js:babel-register --require babel-polyfill -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", "start": "node --debug --harmony ./dist/server/index.js", diff --git a/src/cli/cms/editor/handlebars/sourceAttr.js b/src/cli/cms/editor/handlebars/sourceAttr.js index 89dba572..741ed955 100755 --- a/src/cli/cms/editor/handlebars/sourceAttr.js +++ b/src/cli/cms/editor/handlebars/sourceAttr.js @@ -1,4 +1,4 @@ -function isSelected(currentValue, values) { +export function isSelected(currentValue, values) { var isEqual = false if(typeof currentValue === 'object' && Object.prototype.toString.call(currentValue) === '[object Object]') { Array.prototype.forEach.call(values, (value) => { @@ -46,7 +46,7 @@ export default function sourceAttr(obj, params) { * @param {string} path the path to object (dot notation) * @return {[type]} the object containing the path object or undefined */ -function get(obj, path) { +export function get(obj, path) { return path.split('.').reduce(function(prev, curr) { return prev ? prev[curr] : undefined }, obj || self) @@ -59,7 +59,7 @@ function get(obj, path) { * @param {string} str the string * @return {string} the string with values */ -function prepareDisplay(obj, str) { +export function prepareDisplay(obj, str) { var keys = getKeys(str) Array.prototype.forEach.call(keys, (key) => { var val = get(obj, key) @@ -75,7 +75,7 @@ function prepareDisplay(obj, str) { * @param {string} str the string containing variables * @return {Array} the array of variables */ -function getKeys(str){ +export function getKeys(str){ var regex = /\{\{(.*?)\}\}/g var variables = [] var match diff --git a/test/cms/editor/handlebars/sourceAttr.js b/test/cms/editor/handlebars/sourceAttr.js new file mode 100644 index 00000000..b248f627 --- /dev/null +++ b/test/cms/editor/handlebars/sourceAttr.js @@ -0,0 +1,81 @@ +var chai = require('chai'); +var path = require('path'); +var fse = require('fs-extra'); + +var sourceAttr = require('../../../../src/cli/cms/editor/handlebars/sourceAttr') + +describe('cmsEditor.handlebars.sourceAttr', function() { + before( function() { + this.fixture = { + gmapsElement: fse.readJsonSync(path.join(__dirname, '../../../fixtures/editor/gmaps-element.json'), 'utf8') + } + }); + + it('sourceAttr', function() { + var obj = this.fixture.gmapsElement + var params = { + display:'{{formatted_address}}', + value: 'State of Pará, Brazil' + } + var json = sourceAttr.default(obj, params) + chai.expect(json.selected).to.equal('selected') + chai.expect(json.val).to.equal('State of Pará, Brazil') + }); + + it('get path', function() { + var obj = this.fixture.gmapsElement + var path = 'geometry.location.lat' + var str = sourceAttr.get(obj, path) + chai.expect(str).to.equal(-1.9981271) + }); + + it('get unknown path', function() { + var obj = this.fixture.gmapsElement + var path = 'errored.path' + var str = sourceAttr.get(obj, path) + chai.expect(str).to.be.undefined; + }); + + it('prepareDisplay value', function() { + var obj = this.fixture.gmapsElement + var str = 'formatted_address' + str = sourceAttr.prepareDisplay(obj, str) + chai.expect(str).to.equal('State of Pará, Brazil') + }); + + it('prepareDisplay variable', function() { + var obj = this.fixture.gmapsElement + var str = '{{formatted_address}}' + str = sourceAttr.prepareDisplay(obj, str) + chai.expect(str).to.equal('State of Pará, Brazil') + }); + + it('prepareDisplay 2 variables', function() { + var obj = this.fixture.gmapsElement + var str = '{{formatted_address}} - {{geometry.location.lat}}' + str = sourceAttr.prepareDisplay(obj, str) + chai.expect(str).to.equal('State of Pará, Brazil - -1.9981271') + }); + + it('prepareDisplay unknown', function() { + var obj = this.fixture.gmapsElement + var str = 'unknownkey' + str = sourceAttr.prepareDisplay(obj, str) + chai.expect(str).to.equal(str) + }); + + it('getKeys key', function() { + var ar = sourceAttr.getKeys('key') + chai.expect(ar.length).to.equal(1) + }); + + it('getKeys {{key}}', function() { + var ar = sourceAttr.getKeys('{{key}}') + chai.expect(ar.length).to.equal(1) + }); + + it('getKeys {{key}}-nokey-{{key2}}', function() { + var ar = sourceAttr.getKeys('{{key}}-nokey-{{key2}}') + chai.expect(ar.length).to.equal(2) + }); +}); \ No newline at end of file diff --git a/test/fixtures/editor/gmaps-element.json b/test/fixtures/editor/gmaps-element.json new file mode 100644 index 00000000..c25dca49 --- /dev/null +++ b/test/fixtures/editor/gmaps-element.json @@ -0,0 +1,53 @@ +{ + "address_components": [ + { + "long_name": "State of Pará", + "short_name": "PA", + "types": [ + "administrative_area_level_1", + "political" + ] + }, + { + "long_name": "Brazil", + "short_name": "BR", + "types": [ + "country", + "political" + ] + } + ], + "formatted_address": "State of Pará, Brazil", + "geometry": { + "bounds": { + "northeast": { + "lat": 2.5910246, + "lng": -46.0643195 + }, + "southwest": { + "lat": -9.8411565, + "lng": -58.89827700000001 + } + }, + "location": { + "lat": -1.9981271, + "lng": -54.9306152 + }, + "location_type": "APPROXIMATE", + "viewport": { + "northeast": { + "lat": 2.5910246, + "lng": -46.0643195 + }, + "southwest": { + "lat": -9.8411565, + "lng": -58.89827700000001 + } + } + }, + "place_id": "ChIJlwksrJn5iJIRCvNV5z7IXjE", + "types": [ + "administrative_area_level_1", + "political" + ] +} \ No newline at end of file diff --git a/test/fixtures/node_modules/test/process/test.js b/test/fixtures/node_modules/test/process/test.js index a243684d..68ab636f 100644 --- a/test/fixtures/node_modules/test/process/test.js +++ b/test/fixtures/node_modules/test/process/test.js @@ -1 +1,2 @@ -console.log('test') \ No newline at end of file +//console.log('test') +var i = 0 \ No newline at end of file diff --git a/test/fixtures/scripts/test-script/process/test.js b/test/fixtures/scripts/test-script/process/test.js index a243684d..68ab636f 100644 --- a/test/fixtures/scripts/test-script/process/test.js +++ b/test/fixtures/scripts/test-script/process/test.js @@ -1 +1,2 @@ -console.log('test') \ No newline at end of file +//console.log('test') +var i = 0 \ No newline at end of file diff --git a/test/process.js b/test/process.js index 6c828f29..b6b4cb99 100644 --- a/test/process.js +++ b/test/process.js @@ -17,20 +17,11 @@ describe('Process', function() { }.bind(this)) }); - /** - * abeExtend.plugins.instance.getProcess - * - */ it('abeExtend.plugins.instance.getProcess()', function() { var file = abeExtend.plugins.instance.getProcess('test') chai.expect(file).to.not.be.null; }); - - /** - * abeExtend.process - * - */ it('abeExtend.process', function() { var res = abeExtend.process('test', []) // chai.assert.equal(res, 'test', 'Hook test failed !')