Skip to content

Commit

Permalink
ic: New tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed May 8, 2017
1 parent 96f3981 commit e8e1dd8
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 16 deletions.
16 changes: 8 additions & 8 deletions src/cli/cms/data/source.js
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
}
Expand Down
5 changes: 2 additions & 3 deletions src/index.js
Expand Up @@ -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')
)
})
Expand All @@ -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()
})

Expand Down
223 changes: 218 additions & 5 deletions tests/unit/cms/data/source.js
Expand Up @@ -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')})
Expand All @@ -25,18 +27,132 @@ 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:{}}
cmsData.source.valueList(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");


obj = {key:'titles'}
json = {abe_source:{}}
Expand All @@ -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()
});
});
13 changes: 13 additions & 0 deletions tests/unit/cms/media/image.js
Expand Up @@ -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()
Expand Down

0 comments on commit e8e1dd8

Please sign in to comment.