0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/test/unit/frontend/services/routing/controllers/rss.test.js
Hannah Wolfe 95d27e7f58
Moved frontend unit tests into their own folder
- this is a small part of a bit of cleanup of our test files
- the goal is to make the existing tests clearer with a view to making it easier to write more tests
- this makes the test structure follow the codebase structure more closely
- eventually we will colocate the frontend tests with the frontend code
2021-10-06 11:58:29 +01:00

78 lines
2.2 KiB
JavaScript

const sinon = require('sinon');
const testUtils = require('../../../../../utils');
const security = require('@tryghost/security');
const settingsCache = require('../../../../../../core/shared/settings-cache');
const controllers = require('../../../../../../core/frontend/services/routing/controllers');
const helpers = require('../../../../../../core/frontend/services/routing/helpers');
const rssService = require('../../../../../../core/frontend/services/rss');
// Helper function to prevent unit tests
// from failing via timeout when they
// should just immediately fail
function failTest(done) {
return function (err) {
done(err);
};
}
describe('Unit - services/routing/controllers/rss', function () {
let req;
let res;
let next;
let fetchDataStub;
let posts;
beforeEach(function () {
posts = [
testUtils.DataGenerator.forKnex.createPost(),
testUtils.DataGenerator.forKnex.createPost()
];
req = {
params: {},
originalUrl: '/rss/'
};
res = {
routerOptions: {},
locals: {
safeVersion: '0.6'
}
};
next = sinon.stub();
fetchDataStub = sinon.stub();
sinon.stub(helpers, 'fetchData').get(function () {
return fetchDataStub;
});
sinon.stub(security.string, 'safe').returns('safe');
sinon.stub(rssService, 'render');
sinon.stub(settingsCache, 'get');
settingsCache.get.withArgs('title').returns('Ghost');
settingsCache.get.withArgs('description').returns('Ghost is cool!');
});
afterEach(function () {
sinon.restore();
});
it('should fetch data and attempt to send XML', function (done) {
fetchDataStub.withArgs({page: 1, slug: undefined}).resolves({
posts: posts
});
rssService.render.callsFake(function (_res, baseUrl, data) {
baseUrl.should.eql('/rss/');
data.posts.should.eql(posts);
data.title.should.eql('Ghost');
data.description.should.eql('Ghost is cool!');
done();
});
controllers.rss(req, res, failTest(done));
});
});