2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const testUtils = require('../../../../utils');
|
|
|
|
const api = require('../../../../../core/server/api');
|
|
|
|
const themeService = require('../../../../../core/frontend/services/themes');
|
|
|
|
const helpers = require('../../../../../core/frontend/services/routing/helpers');
|
|
|
|
const controllers = require('../../../../../core/frontend/services/routing/controllers');
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
function failTest(done) {
|
|
|
|
return function (err) {
|
|
|
|
should.exist(err);
|
|
|
|
done(err);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Unit - services/routing/controllers/static', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let req;
|
|
|
|
let res;
|
|
|
|
let secureStub;
|
|
|
|
let renderStub;
|
|
|
|
let handleErrorStub;
|
|
|
|
let formatResponseStub;
|
|
|
|
let postsPerPage;
|
|
|
|
let tagsReadStub;
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
postsPerPage = 5;
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
secureStub = sinon.stub();
|
|
|
|
renderStub = sinon.stub();
|
|
|
|
handleErrorStub = sinon.stub();
|
|
|
|
formatResponseStub = sinon.stub();
|
|
|
|
formatResponseStub.entries = sinon.stub();
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2019-09-10 12:41:42 +03:00
|
|
|
tagsReadStub = sinon.stub().resolves();
|
|
|
|
sinon.stub(api.v2, 'tagsPublic').get(() => {
|
|
|
|
return {
|
|
|
|
read: tagsReadStub
|
|
|
|
};
|
|
|
|
});
|
2018-06-24 01:32:08 +03:00
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(helpers, 'secure').get(function () {
|
2018-06-24 01:32:08 +03:00
|
|
|
return secureStub;
|
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(helpers, 'handleError').get(function () {
|
2018-06-24 01:32:08 +03:00
|
|
|
return handleErrorStub;
|
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(themeService, 'getActive').returns({
|
2018-06-24 01:32:08 +03:00
|
|
|
config: function (key) {
|
2018-10-17 10:23:59 +03:00
|
|
|
if (key === 'posts_per_page') {
|
|
|
|
return postsPerPage;
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(helpers, 'renderer').get(function () {
|
2018-06-24 01:32:08 +03:00
|
|
|
return renderStub;
|
|
|
|
});
|
|
|
|
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.stub(helpers, 'formatResponse').get(function () {
|
2018-06-24 01:32:08 +03:00
|
|
|
return formatResponseStub;
|
|
|
|
});
|
|
|
|
|
|
|
|
req = {
|
|
|
|
path: '/',
|
|
|
|
params: {},
|
|
|
|
route: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
res = {
|
2018-06-26 02:12:50 +03:00
|
|
|
routerOptions: {},
|
2018-06-24 01:32:08 +03:00
|
|
|
render: sinon.spy(),
|
2018-10-17 10:23:59 +03:00
|
|
|
redirect: sinon.spy(),
|
|
|
|
locals: {
|
2019-09-10 12:41:42 +03:00
|
|
|
apiVersion: 'v2'
|
2018-10-17 10:23:59 +03:00
|
|
|
}
|
2018-06-24 01:32:08 +03:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2018-06-24 01:32:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('no extra data to fetch', function (done) {
|
|
|
|
helpers.renderer.callsFake(function () {
|
2019-02-25 19:03:27 +03:00
|
|
|
helpers.formatResponse.entries.calledOnce.should.be.true();
|
2019-09-10 12:41:42 +03:00
|
|
|
tagsReadStub.called.should.be.false();
|
2018-06-24 01:32:08 +03:00
|
|
|
helpers.secure.called.should.be.false();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
controllers.static(req, res, failTest(done));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('extra data to fetch', function (done) {
|
2018-06-26 02:12:50 +03:00
|
|
|
res.routerOptions.data = {
|
2018-06-24 01:32:08 +03:00
|
|
|
tag: {
|
2019-09-10 12:41:42 +03:00
|
|
|
controller: 'tagsPublic',
|
2018-06-24 01:32:08 +03:00
|
|
|
resource: 'tags',
|
|
|
|
type: 'read',
|
|
|
|
options: {
|
|
|
|
slug: 'bacon'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-10 12:41:42 +03:00
|
|
|
tagsReadStub = sinon.stub().resolves({tags: [{slug: 'bacon'}]});
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
helpers.renderer.callsFake(function () {
|
2019-09-10 12:41:42 +03:00
|
|
|
tagsReadStub.called.should.be.true();
|
2019-02-25 19:03:27 +03:00
|
|
|
helpers.formatResponse.entries.calledOnce.should.be.true();
|
2018-06-24 01:32:08 +03:00
|
|
|
helpers.secure.calledOnce.should.be.true();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
controllers.static(req, res, failTest(done));
|
|
|
|
});
|
|
|
|
});
|