freeCodeCamp/tools/scripts/createRedirects.test.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-02-04 12:34:44 +00:00
/* global describe expect it */
const { createRedirects } = require('./createRedirects');
const testLocations = {
api: 'https://api.example.com',
home: 'https://home.example.com',
forum: 'https://forum.example.com',
};
describe('createRedirects', () => {
it('is a function', () => {
expect(typeof createRedirects).toEqual('function');
});
it('returns a string', () => {
expect(typeof createRedirects(testLocations)).toEqual('string');
});
it('replaces instances of `#{{...}}` with the locations provided', () => {
2019-02-04 12:34:44 +00:00
expect.assertions(7);
const apiPlaceholderRE = /#\{\{API\}\}/;
const homePlaceholderRE = /#\{\{HOME\}\}/;
const forumPlacehilderRE = /#\{\{FORUM\}\}/;
const forumProxyPlaceholderRE = /#\{\{FORUM_PROXY\}\}/;
const redirects = createRedirects(testLocations);
const hasApiPlaceholder = apiPlaceholderRE.test(redirects);
const hasHomePlaceholder = homePlaceholderRE.test(redirects);
const hasForumPlaceholder = forumPlacehilderRE.test(redirects);
const hasForumProxyPlaceholder = forumProxyPlaceholderRE.test(redirects);
expect(hasApiPlaceholder).toBe(false);
expect(hasHomePlaceholder).toBe(false);
expect(hasForumPlaceholder).toBe(false);
expect(hasForumProxyPlaceholder).toBe(false);
2019-02-04 12:34:44 +00:00
const { api, home, forum } = testLocations;
expect(redirects.includes(`${api}/internal/:splat`)).toBe(true);
expect(
redirects.includes(
`${home}/forum/t/free-code-camp-privacy-policy/19545 301`
)
).toBe(true);
2019-02-04 12:34:44 +00:00
expect(redirects.includes(`${forum}`)).toBe(true);
});
it('throws when any location is missing', () => {
2019-02-04 12:34:44 +00:00
expect.assertions(3);
const api = 'api';
const home = 'home';
const forum = 'forum';
2019-02-04 12:34:44 +00:00
const noApi = { forum, home };
const noHome = { api, forum };
const noForum = { api, home };
expect(() => createRedirects(noApi)).toThrow();
expect(() => createRedirects(noHome)).toThrow();
expect(() => createRedirects(noForum)).toThrow();
});
it('matches the snapshot', () =>
expect(createRedirects(testLocations)).toMatchSnapshot());
});