Ghost/test/utils/urlUtils.js
Hannah Wolfe 7f1d3ebc07
Move tests from core to root (#11700)
- move all test files from core/test to test/
- updated all imports and other references
- all code inside of core/ is then application code
- tests are correctly at the root level
- consistent with other repos/projects

Co-authored-by: Kevin Ansfield <kevin@lookingsideways.co.uk>
2020-03-30 16:26:47 +01:00

63 lines
1.9 KiB
JavaScript

const _ = require('lodash');
const sinon = require('sinon');
const UrlUtils = require('@tryghost/url-utils');
const config = require('../../core/server/config');
const urlUtils = require('../../core/server/lib/url-utils');
const defaultSandbox = sinon.createSandbox();
const getInstance = (options) => {
const opts = {
url: options.url,
adminUrl: options.adminUrl,
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];
});
}
});
};
// Method for regressions tests must be used with restore method
const stubUrlUtilsFromConfig = () => {
const options = {
url: config.get('url'),
adminUrl: config.get('admin:url'),
apiVersions: config.get('api:versions'),
defaultApiVersion: 'v3',
slugs: config.get('slugs').protected,
redirectCacheMaxAge: config.get('caching:301:maxAge'),
baseApiPath: '/ghost/api'
};
stubUrlUtils(options, defaultSandbox);
};
const restore = () => {
defaultSandbox.restore();
};
module.exports.stubUrlUtils = stubUrlUtils;
module.exports.stubUrlUtilsFromConfig = stubUrlUtilsFromConfig;
module.exports.restore = restore;
module.exports.getInstance = getInstance;