2019-02-04 12:34:44 +00:00
|
|
|
/* global describe expect it */
|
2018-10-08 00:00:50 +01:00
|
|
|
|
|
|
|
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);
|
2018-10-08 00:00:50 +01:00
|
|
|
|
|
|
|
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;
|
2018-10-08 00:00:50 +01:00
|
|
|
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);
|
2018-10-08 00:00:50 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when any location is missing', () => {
|
2019-02-04 12:34:44 +00:00
|
|
|
expect.assertions(3);
|
2018-10-08 00:00:50 +01:00
|
|
|
|
|
|
|
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 };
|
2018-10-08 00:00:50 +01:00
|
|
|
|
|
|
|
expect(() => createRedirects(noApi)).toThrow();
|
|
|
|
expect(() => createRedirects(noHome)).toThrow();
|
|
|
|
expect(() => createRedirects(noForum)).toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('matches the snapshot', () =>
|
|
|
|
expect(createRedirects(testLocations)).toMatchSnapshot());
|
|
|
|
});
|