Ghost/test/utils/urlUtils.js
Hannah Wolfe fc6d60e36d
Updated test UrlUtils to use canary by default
refs: https://github.com/TryGhost/Toolbox/issues/168

- This file was pinning various tests to v3

We're going to be dropping the idea of having multiple versions of the API in each Ghost version.
Because this has not achieved the goal of making it easier to make breaking changes, but it has
created an ordinate amount of technical debt and maintenance overhead.

As we know this is going away in the next major, there is no benefit to us constantly running tests
that check if those versions still work, especially given how long they take.

Instead we're starting work to ensure that all of our test work on canary, and that canary has
excellent test coverage so that we can be sure that our one API version works really well and that
any changes, no matter how subtle are deliberate, tracked and understood.
2022-01-21 15:12:27 +00:00

66 lines
2.0 KiB
JavaScript

const _ = require('lodash');
const sinon = require('sinon');
const UrlUtils = require('@tryghost/url-utils');
const configUtils = require('./configUtils');
const config = require('../../core/shared/config');
const urlUtils = require('../../core/shared/url-utils');
const defaultSandbox = sinon.createSandbox();
const getInstance = (options) => {
const opts = {
getSubdir: config.getSubdir,
getSiteUrl: config.getSiteUrl,
getAdminUrl: config.getAdminUrl,
apiVersions: options.apiVersions,
defaultApiVersion: 'canary',
slugs: options.slugs,
redirectCacheMaxAge: options.redirectCacheMaxAge,
baseApiPath: '/ghost/api'
};
return new UrlUtils(opts);
};
const stubUrlUtils = (options, sandbox) => {
const stubInstance = getInstance(options);
const classPropNames = Object.getOwnPropertyNames(Object.getPrototypeOf(urlUtils))
.filter(name => name !== 'constructor');
classPropNames.forEach((key) => {
if (typeof urlUtils[key] === 'function') {
sandbox.stub(urlUtils, key).callsFake(function () {
return stubInstance[key](...arguments);
});
} else {
sandbox.stub(urlUtils, key).get(function () {
return stubInstance[key];
});
}
});
return stubInstance;
};
// Method for regressions tests must be used with restore method
const stubUrlUtilsFromConfig = () => {
const options = {
apiVersions: config.get('api:versions'),
defaultApiVersion: 'canary',
slugs: config.get('slugs').protected,
redirectCacheMaxAge: config.get('caching:301:maxAge'),
baseApiPath: '/ghost/api'
};
return stubUrlUtils(options, defaultSandbox);
};
const restore = () => {
defaultSandbox.restore();
configUtils.restore();
};
module.exports.stubUrlUtilsFromConfig = stubUrlUtilsFromConfig;
module.exports.restore = restore;
module.exports.getInstance = getInstance;