0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/test/unit/lib/promise/pipeline_spec.js
Hannah Wolfe 22e13acd65 Updated var declarations to const/let and no lists
- All var declarations are now const or let as per ES6
- All comma-separated lists / chained declarations are now one declaration per line
- This is for clarity/readability but also made running the var-to-const/let switch smoother
- ESLint rules updated to match

How this was done:

- npm install -g jscodeshift
- git clone https://github.com/cpojer/js-codemod.git
- git clone git@github.com:TryGhost/Ghost.git shallow-ghost
- cd shallow-ghost
- jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2
- jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2
- yarn
- yarn test
- yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode
- grunt test-regression
- sorted!
2020-04-29 16:51:13 +01:00

100 lines
3.4 KiB
JavaScript

const should = require('should');
const sinon = require('sinon');
const Promise = require('bluebird');
// Stuff we are testing
const pipeline = require('../../../../core/server/lib/promise/pipeline');
// These tests are based on the tests in https://github.com/cujojs/when/blob/3.7.4/test/pipeline-test.js
function createTask(y) {
return function (x) {
return x + y;
};
}
describe('Pipeline', function () {
afterEach(function () {
sinon.restore();
});
it('should execute tasks in order', function () {
return pipeline([createTask('b'), createTask('c'), createTask('d')], 'a').then(function (result) {
result.should.eql('abcd');
});
});
it('should resolve to initial args when no tasks supplied', function () {
return pipeline([], 'a', 'b').then(function (result) {
result.should.eql(['a', 'b']);
});
});
it('should resolve to empty array when no tasks and no args supplied', function () {
return pipeline([]).then(function (result) {
result.should.eql([]);
});
});
it('should pass args to initial task', function () {
const expected = [1, 2, 3];
const tasks = [sinon.spy()];
return pipeline(tasks, 1, 2, 3).then(function () {
tasks[0].calledOnce.should.be.true();
tasks[0].firstCall.args.should.eql(expected);
});
});
it('should allow initial args to be promises', function () {
const expected = [1, 2, 3];
const tasks = [sinon.spy()];
const Resolver = Promise.resolve;
return pipeline(tasks, new Resolver(1), new Resolver(2), new Resolver(3)).then(function () {
tasks[0].calledOnce.should.be.true();
tasks[0].firstCall.args.should.eql(expected);
});
});
it('should allow tasks to be promises', function () {
const expected = [1, 2, 3];
const tasks = [
sinon.stub().returns(new Promise.resolve(4)),
sinon.stub().returns(new Promise.resolve(5)),
sinon.stub().returns(new Promise.resolve(6))
];
return pipeline(tasks, 1, 2, 3).then(function (result) {
result.should.eql(6);
tasks[0].calledOnce.should.be.true();
tasks[0].firstCall.args.should.eql(expected);
tasks[1].calledOnce.should.be.true();
tasks[1].firstCall.calledWith(4).should.be.true();
tasks[2].calledOnce.should.be.true();
tasks[2].firstCall.calledWith(5).should.be.true();
});
});
it('should allow tasks and args to be promises', function () {
const expected = [1, 2, 3];
const tasks = [
sinon.stub().returns(new Promise.resolve(4)),
sinon.stub().returns(new Promise.resolve(5)),
sinon.stub().returns(new Promise.resolve(6))
];
const Resolver = Promise.resolve;
return pipeline(tasks, new Resolver(1), new Resolver(2), new Resolver(3)).then(function (result) {
result.should.eql(6);
tasks[0].calledOnce.should.be.true();
tasks[0].firstCall.args.should.eql(expected);
tasks[1].calledOnce.should.be.true();
tasks[1].firstCall.calledWith(4).should.be.true();
tasks[2].calledOnce.should.be.true();
tasks[2].firstCall.calledWith(5).should.be.true();
});
});
});