From e8e1dd8372467bc3297517cb0924a0a81927ce3e Mon Sep 17 00:00:00 2001 From: gregorybesson Date: Mon, 8 May 2017 04:54:36 +0200 Subject: [PATCH] ic: New tests --- src/cli/cms/data/source.js | 16 +-- src/index.js | 5 +- tests/unit/cms/data/source.js | 223 +++++++++++++++++++++++++++++++++- tests/unit/cms/media/image.js | 13 ++ 4 files changed, 241 insertions(+), 16 deletions(-) diff --git a/src/cli/cms/data/source.js b/src/cli/cms/data/source.js index 47d3348b..9d351c8a 100644 --- a/src/cli/cms/data/source.js +++ b/src/cli/cms/data/source.js @@ -19,17 +19,17 @@ export function requestList(obj, tplPath, match, jsonPage) { if (!obj.editable) { if (obj['max-length']) { jsonPage[obj.key] = data.slice(0, obj['max-length']) - }else { + } else { jsonPage[obj.key] = data } } else if (jsonPage[obj.key] == null && obj.prefill) { if (obj['prefill-quantity'] && obj['max-length']) { jsonPage[obj.key] = data.slice(0, (obj['prefill-quantity'] > obj['max-length']) ? obj['max-length'] : obj['prefill-quantity']) - }else if (obj['prefill-quantity']) { + } else if (obj['prefill-quantity']) { jsonPage[obj.key] = data.slice(0, obj['prefill-quantity']) - }else if (obj['max-length']) { + } else if (obj['max-length']) { jsonPage[obj.key] = data.slice(0, obj['max-length']) - }else { + } else { jsonPage[obj.key] = data } } @@ -106,12 +106,12 @@ export function urlList(obj, tplPath, match, jsonPage) { var parsedBody = JSON.parse(body) if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Array]') { jsonPage['abe_source'][obj.key] = parsedBody - }else if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Object]') { + } else if(typeof parsedBody === 'object' && Object.prototype.toString.call(parsedBody) === '[object Object]') { jsonPage['abe_source'][obj.key] = [parsedBody] } - }else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Array]') { + } else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Array]') { jsonPage['abe_source'][obj.key] = body - }else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Object]') { + } else if(typeof body === 'object' && Object.prototype.toString.call(body) === '[object Object]') { jsonPage['abe_source'][obj.key] = body } } catch(e) { @@ -129,7 +129,7 @@ export function urlList(obj, tplPath, match, jsonPage) { localReq.write('') localReq.end() - }else { + } else { jsonPage['abe_source'][obj.key] = obj.sourceString resolve() } diff --git a/src/index.js b/src/index.js index 46013b61..93da363f 100755 --- a/src/index.js +++ b/src/index.js @@ -48,7 +48,7 @@ program }) } console.log( - clc.green('Yeahhh ! Your Abe site ' + answers.name + ' is ready. to launch it:'), + clc.green('Yeahhh ! Your Abe site ' + answers.name + ' is ready to launch!'), clc.cyan('\ncd ' + answers.name + '\nabe serve -i') ) }) @@ -59,8 +59,7 @@ program }).on('--help', function() { console.log(' Examples:') console.log() - console.log(' $ abe create') - console.log(' $ abe create [destination]') + console.log(' $ abe init') console.log() }) diff --git a/tests/unit/cms/data/source.js b/tests/unit/cms/data/source.js index 28ac1ba3..411a88af 100644 --- a/tests/unit/cms/data/source.js +++ b/tests/unit/cms/data/source.js @@ -5,6 +5,8 @@ chai.use(sinonChai) var sinon = require('sinon'); var path = require('path'); var fse = require('fs-extra'); +var http = require('http') +var PassThrough = require('stream').PassThrough; var config = require('../../../../src/cli').config config.set({root: path.join(process.cwd(), 'tests', 'unit', 'fixtures')}) @@ -25,10 +27,125 @@ describe('Source', function() { }.bind(this)) }); - /** - * - * - */ + it('cmsData.source.requestList', function(done) { + var obj = {key:'titles'} + var json = {abe_source:{}} + var data = [{"a":"1", "b":"1"},{"a":"2", "b":"2"}] + this.sinon = sinon.sandbox.create(); + this.sinon.stub(cmsData.sql, 'executeQuery', function(tplPath, match, jsonPage) { + return Promise.resolve(data) + }.bind(this)); + cmsData.source.requestList(obj, 'tplPath', 'match', json) + .then((jsonPage) => { + chai.expect(jsonPage.abe_source.titles.length).to.be.equal(2) + chai.expect(jsonPage.abe_source.titles[0].a).to.be.equal("1") + + chai.expect(jsonPage.titles.length).to.be.equal(2) + chai.expect(jsonPage.titles[0].a).to.be.equal("1") + this.sinon.restore() + done() + }) + }); + + it('cmsData.source.requestList max-length', function(done) { + var obj = {key:'titles', "max-length":1} + var json = {abe_source:{}} + var data = [{"a":"1", "b":"1"},{"a":"2", "b":"2"}] + this.sinon = sinon.sandbox.create(); + this.sinon.stub(cmsData.sql, 'executeQuery', function(tplPath, match, jsonPage) { + return Promise.resolve(data) + }.bind(this)); + cmsData.source.requestList(obj, 'tplPath', 'match', json) + .then((jsonPage) => { + chai.expect(jsonPage.abe_source.titles.length).to.be.equal(2) + chai.expect(jsonPage.abe_source.titles[0].a).to.be.equal("1") + + chai.expect(jsonPage.titles.length).to.be.equal(1) + chai.expect(jsonPage.titles[0].a).to.be.equal("1") + this.sinon.restore() + done() + }) + }); + + it('cmsData.source.requestList prefill', function(done) { + var obj = {key:'titles', editable:true, prefill:true, "prefill-quantity":1} + var json = {} + var data = [{"a":"1", "b":"1"},{"a":"2", "b":"2"}] + this.sinon = sinon.sandbox.create(); + this.sinon.stub(cmsData.sql, 'executeQuery', function(tplPath, match, jsonPage) { + return Promise.resolve(data) + }.bind(this)); + cmsData.source.requestList(obj, 'tplPath', 'match', json) + .then((jsonPage) => { + chai.expect(jsonPage.abe_source.titles.length).to.be.equal(2) + chai.expect(jsonPage.abe_source.titles[0].a).to.be.equal("1") + + chai.expect(jsonPage.titles.length).to.be.equal(1) + chai.expect(jsonPage.titles[0].a).to.be.equal("1") + this.sinon.restore() + done() + }) + }); + + it('cmsData.source.requestList editable no prefill', function(done) { + var obj = {key:'titles', editable:true} + var json = {} + var data = [{"a":"1", "b":"1"},{"a":"2", "b":"2"}] + this.sinon = sinon.sandbox.create(); + this.sinon.stub(cmsData.sql, 'executeQuery', function(tplPath, match, jsonPage) { + return Promise.resolve(data) + }.bind(this)); + cmsData.source.requestList(obj, 'tplPath', 'match', json) + .then((jsonPage) => { + chai.expect(jsonPage.abe_source.titles.length).to.be.equal(2) + chai.expect(jsonPage.abe_source.titles[0].a).to.be.equal("1") + + chai.expect(jsonPage.titles).to.be.undefined + this.sinon.restore() + done() + }) + }); + + it('cmsData.source.requestList editable max-length', function(done) { + var obj = {key:'titles', editable:true, prefill:true, "max-length":1} + var json = {} + var data = [{"a":"1", "b":"1"},{"a":"2", "b":"2"}] + this.sinon = sinon.sandbox.create(); + this.sinon.stub(cmsData.sql, 'executeQuery', function(tplPath, match, jsonPage) { + return Promise.resolve(data) + }.bind(this)); + cmsData.source.requestList(obj, 'tplPath', 'match', json) + .then((jsonPage) => { + chai.expect(jsonPage.abe_source.titles.length).to.be.equal(2) + chai.expect(jsonPage.abe_source.titles[0].a).to.be.equal("1") + + chai.expect(jsonPage.titles.length).to.be.equal(1) + chai.expect(jsonPage.titles[0].a).to.be.equal("1") + this.sinon.restore() + done() + }) + }); + + it('cmsData.source.requestList editable max-length prefill-qty', function(done) { + var obj = {key:'titles', editable:true, prefill:true, "max-length":1, "prefill-quantity":2} + var json = {} + var data = [{"a":"1", "b":"1"},{"a":"2", "b":"2"},{"a":"3", "b":"3"}] + this.sinon = sinon.sandbox.create(); + this.sinon.stub(cmsData.sql, 'executeQuery', function(tplPath, match, jsonPage) { + return Promise.resolve(data) + }.bind(this)); + cmsData.source.requestList(obj, 'tplPath', 'match', json) + .then((jsonPage) => { + chai.expect(jsonPage.abe_source.titles.length).to.be.equal(3) + chai.expect(jsonPage.abe_source.titles[0].a).to.be.equal("1") + + chai.expect(jsonPage.titles.length).to.be.equal(1) + chai.expect(jsonPage.titles[0].a).to.be.equal("1") + this.sinon.restore() + done() + }) + }); + it('cmsData.source.valueList', function(done) { var obj = {key:'titles'} var json = {abe_source:{}} @@ -36,7 +153,6 @@ describe('Source', function() { .then(() => { chai.expect(json.abe_source.titles.length).to.be.equal(3); chai.expect(json.abe_source.titles[0].title).to.be.equal("rouge"); - obj = {key:'titles'} json = {abe_source:{}} @@ -47,4 +163,101 @@ describe('Source', function() { }) }) }); + + it('cmsData.source.urlList', function(done) { + var obj = {key:'web', sourceString:'http://www.rest.endpoint/', autocomplete:true} + var json = {abe_source:{}} + cmsData.source.urlList(obj, 'tplPath', 'match', json) + .then(() => { + chai.expect(json.abe_source.web).to.be.equal("http://www.rest.endpoint/"); + done() + }) + }); + + it('cmsData.source.urlList response string', function(done) { + var obj = {key:'web', sourceString:'http://www.rest.endpoint/', autocomplete:false} + var json = {abe_source:{}} + this.sinon = sinon.sandbox.create(); + this.request = this.sinon.stub(http, 'request') + + var expected = {a: "1"} + var response = new PassThrough() + response.write(JSON.stringify(expected)) + response.end() + var request = new PassThrough() + this.request.callsArgWith(1, response) + .returns(request); + + cmsData.source.urlList(obj, 'tplPath', 'match', json) + .then(() => { + chai.expect(json.abe_source.web.length).to.be.equal(1); + this.sinon.restore() + done() + }) + }); + + it('cmsData.source.urlList response array string', function(done) { + var obj = {key:'web', sourceString:'http://www.rest.endpoint/', autocomplete:false} + var json = {abe_source:{}} + this.sinon = sinon.sandbox.create(); + this.request = this.sinon.stub(http, 'request') + + var expected = [{a: "1"}] + var response = new PassThrough() + response.write(JSON.stringify(expected)) + response.end() + var request = new PassThrough() + this.request.callsArgWith(1, response) + .returns(request); + + cmsData.source.urlList(obj, 'tplPath', 'match', json) + .then(() => { + chai.expect(json.abe_source.web.length).to.be.equal(1); + this.sinon.restore() + done() + }) + }); + + it('cmsData.source.fileList', function(done) { + var obj = {key:'file', sourceString:'data/article-1.json'} + var json = {abe_source:{}} + cmsData.source.fileList(obj, 'tplpath', 'match', json) + .then(() => { + chai.expect(json.abe_source.file.title).to.be.equal("article") + + done() + }) + }); + + // it('cmsData.source.getDataList', function(done) { + // // var obj = {key:'titles'} + // // var json = {abe_source:{}} + // // cmsData.source.getDataList(obj, this.fixture.articleJsoninline, json) + // // .then(() => { + // // chai.expect(json.abe_source.titles.length).to.be.equal(3); + // // chai.expect(json.abe_source.titles[0].title).to.be.equal("rouge"); + // // }) + // }); + + it('cmsData.source.removeDataList', function(done) { + var text = "a{{abe type='data' source='fake'}}b" + var result = cmsData.source.removeDataList(text) + console.log(result) + chai.expect(result).to.be.equal("ab") + done() + }); + + it('cmsData.source.removeNonEditableDataList', function(done) { + var text = "a{{abe type='data' editable='false' source='fake'}}b" + var result = cmsData.source.removeNonEditableDataList(text) + chai.expect(result).to.be.equal("ab") + done() + }); + + it('cmsData.source.removeNonEachDataList', function(done) { + var text = "{{#each test}} a{{abe type='data' editable='false' source='fake'}}b {{/each}}a{{abe type='data' source='fake'}}b" + var result = cmsData.source.removeNonEachDataList(text) + chai.expect(result).to.be.equal("{{#each test}} a{{abe type=\'data\' editable=\'false\' source=\'fake\'}}b {{/each}}ab") + done() + }); }); diff --git a/tests/unit/cms/media/image.js b/tests/unit/cms/media/image.js index 6c17a9b7..0ac90052 100644 --- a/tests/unit/cms/media/image.js +++ b/tests/unit/cms/media/image.js @@ -167,6 +167,19 @@ describe('image', function() { coreUtils.random.generateUniqueIdentifier.restore() }); + it('cmsMedia.image.getMediaType()', function() { + var result = cmsMedia.image.getMediaType('.jpg') + chai.expect(result).to.equal('image') + result = cmsMedia.image.getMediaType('.svg') + chai.expect(result).to.equal('image') + result = cmsMedia.image.getMediaType('.mp4') + chai.expect(result).to.equal('video') + result = cmsMedia.image.getMediaType('.mp3') + chai.expect(result).to.equal('sound') + result = cmsMedia.image.getMediaType('.pdf') + chai.expect(result).to.equal('document') + }); + after(function(done) { fse.remove(path.join(config.root, config.publish.url)) done()