var chai = require('chai'),
expect = chai.expect,
sinon = require('sinon'),
sinonChai = require('sinon-chai'),
nock = require('nock'),
rewire = require('rewire'),
path = require('path'),
Amperize = rewire('../lib/amperize'),
amperize;
chai.use(sinonChai);
chai.config.includeStack = true;
describe('Amperize', function () {
beforeEach(function () {
amperize = new Amperize();
});
afterEach(function () {
amperize = undefined;
nock.cleanAll();
sinon.restore();
});
describe('is a module', function () {
it('which has a constructor', function () {
expect(Amperize).to.be.a('function');
});
it('which has default options', function () {
expect(amperize).to.have.property('config');
expect(amperize.config).to.be.eql({
'amp-img': {
layout: 'responsive',
width: 600,
height: 400
},
'amp-anim': {
layout: 'responsive',
width: 600,
height: 400
},
'amp-iframe': {
layout: 'responsive',
width: 600,
height: 400,
sandbox: 'allow-scripts allow-same-origin'
},
'amp-youtube': {
layout: 'responsive',
width: 600,
height: 400
},
request_timeout: 3000
});
});
it('which can be configured', function () {
var configurable = new Amperize({some: 'options'});
expect(configurable).to.have.property('config');
expect(configurable.config.some).to.be.equal('options');
});
it('which has htmlParser', function () {
expect(amperize).to.have.property('htmlParser');
expect(amperize.htmlParser).to.be.a('object');
});
it('which has #parse', function () {
expect(amperize).to.have.property('parse');
expect(amperize.parse).to.be.a('function');
});
it('which has #amperizer', function () {
expect(amperize).to.have.property('amperizer');
expect(amperize.amperizer).to.be.a('function');
});
});
describe('#parse', function () {
var resetProbeImageSize,
imageSizeMock,
probeImageSizeStub;
beforeEach(function () {
// reset rewire so tests are independent
if (resetProbeImageSize) {
resetProbeImageSize();
}
// stubbing the `probe-probe-image-size` lib, so we don't make a request everytime
probeImageSizeStub = sinon.stub();
});
it('throws an error if no callback provided', function () {
function err() {
amperize.parse('', null);
}
expect(err).throws('No callback provided');
});
it('transforms small into with full image dimensions and fixed layout', function (done) {
imageSizeMock = nock('http://static.wixstatic.com')
.get('/media/355241_d31358572a2542c5a44738ddcb59e7ea.jpg_256')
.reply(200, {
body: ''
});
probeImageSizeStub.returns(Promise.resolve({width: 50, height: 50, type: 'jpg'}));
resetProbeImageSize = Amperize.__set__('probeImageSize', probeImageSizeStub);
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('transforms big into with full image dimensions and responsive layout', function (done) {
imageSizeMock = nock('http://static.wixstatic.com')
.get('/media/355241_d31358572a2542c5a44738ddcb59e7ea.jpg_256')
.reply(200, {
body: ''
});
probeImageSizeStub.returns(Promise.resolve({width: 350, height: 200, type: 'jpg'}));
resetProbeImageSize = Amperize.__set__('probeImageSize', probeImageSizeStub);
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('transforms into when width and height is set and overwrites it', function (done) {
imageSizeMock = nock('http://somestockwebsite.com')
.get('/image.jpg')
.reply(200, {
body: ''
});
probeImageSizeStub.returns(Promise.resolve({width: 350, height: 200, type: 'jpg'}));
resetProbeImageSize = Amperize.__set__('probeImageSize', probeImageSizeStub);
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('transforms into does not overwrite layout attribute', function (done) {
imageSizeMock = nock('http://somestockwebsite.com')
.get('/image.jpg')
.reply(200, {
body: ''
});
probeImageSizeStub.returns(Promise.resolve({width: 350, height: 200, type: 'jpg'}));
resetProbeImageSize = Amperize.__set__('probeImageSize', probeImageSizeStub);
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('transforms into when no file extension is given', function (done) {
// This test is mocked, but works with this specific example.
// You can comment out the mocks and the test should still pass.
imageSizeMock = nock('https://www.zomato.com')
.matchHeader('User-Agent', /Mozilla\/.*Safari\/.*/)
.get('/logo/18163505/minilogo')
.reply(200, {
body: ''
});
probeImageSizeStub.returns(Promise.resolve({width: 104, height: 15, type: 'png'}));
resetProbeImageSize = Amperize.__set__('probeImageSize', probeImageSizeStub);
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('falls back to image-size for unprobable images', function (done) {
imageSizeMock = nock('https://somewebsite.com')
.get('/favicon.ico')
.replyWithFile(200, path.join(__dirname, 'fixtures/multi-size.ico'));
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('falls back to image-size for unprobable images (uppercase extension)', function (done) {
imageSizeMock = nock('https://somewebsite.com')
.get('/favicon.ICO')
.replyWithFile(200, path.join(__dirname, 'fixtures/multi-size.ico'));
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('falls back to image-size for unprobable images (query param)', function (done) {
imageSizeMock = nock('https://somewebsite.com')
.get('/favicon.ICO?v=1')
.replyWithFile(200, path.join(__dirname, 'fixtures/multi-size.ico'));
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('returns largest image value for .ico files', function (done) {
imageSizeMock = nock('https://somewebsite.com')
.get('/favicon.ico')
.replyWithFile(200, path.join(__dirname, 'fixtures/multi-size.ico'));
probeImageSizeStub.returns(Promise.resolve({
width: 32,
height: 32,
type: 'ico',
images: [
{width: 48, height: 48},
{width: 32, height: 32},
{width: 16, height: 16}
]
}));
resetProbeImageSize = Amperize.__set__('sizeOf', probeImageSizeStub);
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
// TODO: adapt code to not trigger parallel requests for the same image
it.skip('uses cached size rather than extra requests for duplicated images in html', function (done) {
var GIF1x1 = Buffer.from('R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==', 'base64');
var secondImageSizeMock;
imageSizeMock = nock('http://example.com')
.get('/image.jpg')
.reply(200, GIF1x1);
secondImageSizeMock = nock('http://example.com')
.get('/image.jpg')
.reply(200, GIF1x1);
amperize.parse('', function (error, result) {
expect(imageSizeMock.isDone()).to.equal(true);
expect(secondImageSizeMock.isDone()).to.equal(false);
expect(result).to.exist;
expect(result).to.match(/ with only height property into with full dimensions by overriding them', function (done) {
imageSizeMock = nock('https://media.giphy.com')
.get('/media/l46CtzgjhTm29Cbjq/giphy.gif')
.reply(200, {
body: ''
});
probeImageSizeStub.returns(Promise.resolve({width: 800, height: 600, type: 'gif'}));
resetProbeImageSize = Amperize.__set__('probeImageSize', probeImageSizeStub);
amperize.parse('', function (error, result) {
expect(result).to.exist;
expect(result).to.contain('');
done();
});
});
it('transforms