2020-05-25 03:49:38 -05:00
|
|
|
const errors = require('@tryghost/errors');
|
2018-10-05 00:50:45 +02:00
|
|
|
const should = require('should');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const sinon = require('sinon');
|
2020-03-30 16:26:47 +01:00
|
|
|
const shared = require('../../../../../core/server/api/shared');
|
2018-10-05 00:50:45 +02:00
|
|
|
|
|
|
|
describe('Unit: api/shared/serializers/handle', function () {
|
|
|
|
beforeEach(function () {
|
2019-01-21 17:53:44 +01:00
|
|
|
sinon.restore();
|
2018-10-05 00:50:45 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('input', function () {
|
|
|
|
it('no api config passed', function () {
|
|
|
|
return shared.serializers.handle.input()
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-25 03:49:38 -05:00
|
|
|
(err instanceof errors.IncorrectUsageError).should.be.true();
|
2018-10-05 00:50:45 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no api serializers passed', function () {
|
|
|
|
return shared.serializers.handle.input({})
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-25 03:49:38 -05:00
|
|
|
(err instanceof errors.IncorrectUsageError).should.be.true();
|
2018-10-05 00:50:45 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-16 16:51:50 +07:00
|
|
|
it('ensure serializers are called with apiConfig and frame', function () {
|
2019-01-21 17:53:44 +01:00
|
|
|
const allStub = sinon.stub();
|
|
|
|
sinon.stub(shared.serializers.input.all, 'all').get(() => allStub);
|
2018-10-05 00:50:45 +02:00
|
|
|
|
|
|
|
const apiSerializers = {
|
2019-01-21 17:53:44 +01:00
|
|
|
all: sinon.stub().resolves(),
|
2018-10-05 00:50:45 +02:00
|
|
|
posts: {
|
2019-01-21 17:53:44 +01:00
|
|
|
all: sinon.stub().resolves(),
|
2019-03-04 17:00:07 +01:00
|
|
|
browse: sinon.stub().resolves()
|
2018-10-05 00:50:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-04 17:00:07 +01:00
|
|
|
const apiConfig = {docName: 'posts', method: 'browse'};
|
2018-10-16 16:51:50 +07:00
|
|
|
const frame = {};
|
|
|
|
|
|
|
|
const stubsToCheck = [
|
|
|
|
allStub,
|
|
|
|
apiSerializers.all,
|
|
|
|
apiSerializers.posts.all,
|
2019-03-04 17:00:07 +01:00
|
|
|
apiSerializers.posts.browse
|
2018-10-16 16:51:50 +07:00
|
|
|
];
|
|
|
|
|
|
|
|
return shared.serializers.handle.input(apiConfig, apiSerializers, frame)
|
2018-10-05 00:50:45 +02:00
|
|
|
.then(() => {
|
2018-10-16 16:51:50 +07:00
|
|
|
stubsToCheck.forEach((stub) => {
|
|
|
|
stub.calledOnce.should.be.true();
|
|
|
|
should.equal(stub.args[0][0], apiConfig);
|
|
|
|
should.equal(stub.args[0][1], frame);
|
|
|
|
});
|
2018-10-05 00:50:45 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('output', function () {
|
|
|
|
it('no models passed', function () {
|
|
|
|
return shared.serializers.handle.output(null, {}, {}, {});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no api config passed', function () {
|
|
|
|
return shared.serializers.handle.output([])
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-25 03:49:38 -05:00
|
|
|
(err instanceof errors.IncorrectUsageError).should.be.true();
|
2018-10-05 00:50:45 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('no api serializers passed', function () {
|
|
|
|
return shared.serializers.handle.output([], {})
|
|
|
|
.then(Promise.reject)
|
|
|
|
.catch((err) => {
|
2020-05-25 03:49:38 -05:00
|
|
|
(err instanceof errors.IncorrectUsageError).should.be.true();
|
2018-10-05 00:50:45 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('ensure serializers are called', function () {
|
|
|
|
const apiSerializers = {
|
|
|
|
posts: {
|
2019-01-21 17:53:44 +01:00
|
|
|
add: sinon.stub().resolves()
|
2018-10-05 00:50:45 +02:00
|
|
|
},
|
|
|
|
users: {
|
2019-01-21 17:53:44 +01:00
|
|
|
add: sinon.stub().resolves()
|
2018-10-05 00:50:45 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return shared.serializers.handle.output([], {docName: 'posts', method: 'add'}, apiSerializers, {})
|
|
|
|
.then(() => {
|
|
|
|
apiSerializers.posts.add.calledOnce.should.be.true();
|
|
|
|
apiSerializers.users.add.called.should.be.false();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|