0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00
ghost/test/unit/adapters/scheduling/post-scheduling/post-scheduler.test.js
Hannah Wolfe f08a55c21f
Renamed tests to .test.js & updated commands
refs: https://github.com/TryGhost/Team/issues/856
refs: https://github.com/TryGhost/Team/issues/756

- The .test.js extension is better than _spec.js as it's more obvious that it's an extension
- It also meaans we can use the --extension parameter in mocha, which should result in a better default behaviour for `yarn test`
- It also highlights that some of our tests were named incorrectly and were not (and still will not be) run (see https://github.com/TryGhost/Team/issues/856)
- Note: even with this change, `yarn test` is throwing errors, I believe because of this issue https://github.com/TryGhost/Team/issues/756
2021-07-06 20:45:01 +01:00

84 lines
3.1 KiB
JavaScript

const errors = require('@tryghost/errors');
const should = require('should');
const sinon = require('sinon');
const Promise = require('bluebird');
const moment = require('moment');
const testUtils = require('../../../../utils');
const models = require('../../../../../core/server/models');
const events = require('../../../../../core/server/lib/common/events');
const schedulingUtils = require('../../../../../core/server/adapters/scheduling/utils');
const SchedulingDefault = require('../../../../../core/server/adapters/scheduling/SchedulingDefault');
const urlUtils = require('../../../../../core/shared/url-utils');
const PostScheduler = require('../../../../../core/server/adapters/scheduling/post-scheduling/post-scheduler');
describe('Scheduling: Post Scheduler', function () {
let adapter;
before(function () {
models.init();
});
beforeEach(function () {
adapter = new SchedulingDefault();
sinon.stub(schedulingUtils, 'createAdapter').returns(Promise.resolve(adapter));
sinon.spy(adapter, 'schedule');
sinon.spy(adapter, 'unschedule');
});
afterEach(function () {
sinon.restore();
});
describe('fn:constructor', function () {
describe('success', function () {
it('will be scheduled', async function () {
const post = models.Post.forge(testUtils.DataGenerator.forKnex.createPost({
id: 1337,
mobiledoc: testUtils.DataGenerator.markdownToMobiledoc('something')
}));
new PostScheduler({
apiUrl: 'localhost:1111/',
integration: {
api_keys: [{
id: 'integrationUniqueId',
secret: 'super-secret'
}]
},
adapter,
scheduledResources: {
posts: []
},
events
});
events.emit('post.scheduled', post);
// let the events bubble up
await Promise.delay(100);
adapter.schedule.called.should.eql(true);
adapter.schedule.calledOnce.should.eql(true);
adapter.schedule.args[0][0].time.should.equal(moment(post.get('published_at')).valueOf());
adapter.schedule.args[0][0].url.should.startWith(urlUtils.urlJoin('localhost:1111/', 'schedules', 'posts', post.get('id'), '?token='));
adapter.schedule.args[0][0].extra.httpMethod.should.eql('PUT');
should.equal(null, adapter.schedule.args[0][0].extra.oldTime);
});
});
describe('error', function () {
it('no apiUrl parameter passed', function () {
try {
new PostScheduler();
throw new Error('should have thrown');
} catch (err) {
(err instanceof errors.IncorrectUsageError).should.eql(true);
}
});
});
});
});