mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-28 05:14:12 +03:00
737d19c78c
- stubUrlUtils is now doing the same thing as our existing configUtils but is slightly harder to use - swapped this out, and made stubUrlUtils an internal-only utility
66 lines
2.0 KiB
JavaScript
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: 'v3',
|
|
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: 'v3',
|
|
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;
|