fix: Fix _redirects file

This commit is contained in:
Bouncey 2019-02-05 18:08:40 +00:00 committed by mrugesh mohapatra
parent 9a4e13954a
commit f9ff43d128
3 changed files with 27 additions and 26 deletions

View File

@ -36,9 +36,11 @@ exports[`createRedirects matches the snapshot 1`] = `
/field-guide/* /forum 301 /field-guide/* /forum 301
/learn-to-code /learn 200 /learn-to-code /learn 200
/map /learn 200 /map /learn 200
/news https://news.example.com
/news/* https://news.example.com/:splat
/forum/* https://forum.example.com/:splat /forum/* https://forum.example.com/:splat
/privacy https://home.example.com/forum/t/free-code-camp-privacy-policy/19545 301 /privacy https://forum.example.com/t/free-code-camp-privacy-policy/19545 301
/nonprofit-project-instructions https://home.example.com/forum/t/how-free-code-camps-nonprofits-projects-work/19547 301 /nonprofit-project-instructions https://forum.example.com/t/how-free-code-camps-nonprofits-projects-work/19547 301
/how-nonprofit-projects-work https://medium.freecodecamp.org/open-source-for-good-1a0ea9f32d5a 301 /how-nonprofit-projects-work https://medium.freecodecamp.org/open-source-for-good-1a0ea9f32d5a 301
" "

View File

@ -1,15 +1,15 @@
const apiPlaceholderRE = /#\{\{API\}\}/g; const apiPlaceholderRE = /#\{\{API\}\}/g;
const homePlaceholderRE = /#\{\{HOME\}\}/g; const newsPlaceholderRE = /#\{\{NEWS\}\}/g;
const forumPlacehilderRE = /#\{\{FORUM\}\}/g; const forumPlacehilderRE = /#\{\{FORUM\}\}/g;
exports.createRedirects = function createRedirects(locations) { exports.createRedirects = function createRedirects(locations) {
const { api, home, forum } = locations; const { api, news, forum } = locations;
if (!(api && home && forum )) { if (!(api && news && forum)) {
throw new Error(`One or more locations are missing, all are required. throw new Error(`One or more locations are missing, all are required.
api: ${api} api: ${api}
home: ${home} news: ${news}
forum: ${forum} forum: ${forum}
`); `);
@ -17,7 +17,7 @@ exports.createRedirects = function createRedirects(locations) {
return template return template
.replace(apiPlaceholderRE, api) .replace(apiPlaceholderRE, api)
.replace(homePlaceholderRE, home) .replace(newsPlaceholderRE, news)
.replace(forumPlacehilderRE, forum); .replace(forumPlacehilderRE, forum);
}; };
@ -57,9 +57,11 @@ const template = `#api redirect
/field-guide/* /forum 301 /field-guide/* /forum 301
/learn-to-code /learn 200 /learn-to-code /learn 200
/map /learn 200 /map /learn 200
/news #{{NEWS}}
/news/* #{{NEWS}}/:splat
/forum/* #{{FORUM}}/:splat /forum/* #{{FORUM}}/:splat
/privacy #{{HOME}}/forum/t/free-code-camp-privacy-policy/19545 301 /privacy #{{FORUM}}/t/free-code-camp-privacy-policy/19545 301
/nonprofit-project-instructions #{{HOME}}/forum/t/how-free-code-camps-nonprofits-projects-work/19547 301 /nonprofit-project-instructions #{{FORUM}}/t/how-free-code-camps-nonprofits-projects-work/19547 301
/how-nonprofit-projects-work https://medium.freecodecamp.org/open-source-for-good-1a0ea9f32d5a 301 /how-nonprofit-projects-work https://medium.freecodecamp.org/open-source-for-good-1a0ea9f32d5a 301
`; `;

View File

@ -4,7 +4,7 @@ const { createRedirects } = require('./createRedirects');
const testLocations = { const testLocations = {
api: 'https://api.example.com', api: 'https://api.example.com',
home: 'https://home.example.com', news: 'https://news.example.com',
forum: 'https://forum.example.com', forum: 'https://forum.example.com',
}; };
@ -18,29 +18,26 @@ describe('createRedirects', () => {
}); });
it('replaces instances of `#{{...}}` with the locations provided', () => { it('replaces instances of `#{{...}}` with the locations provided', () => {
expect.assertions(7); expect.assertions(6);
const apiPlaceholderRE = /#\{\{API\}\}/; const apiPlaceholderRE = /#\{\{API\}\}/;
const homePlaceholderRE = /#\{\{HOME\}\}/; const newsPlaceholderRE = /#\{\{NEWS\}\}/;
const forumPlacehilderRE = /#\{\{FORUM\}\}/; const forumPlaceholderRE = /#\{\{FORUM\}\}/;
const forumProxyPlaceholderRE = /#\{\{FORUM_PROXY\}\}/;
const redirects = createRedirects(testLocations); const redirects = createRedirects(testLocations);
const hasApiPlaceholder = apiPlaceholderRE.test(redirects); const hasApiPlaceholder = apiPlaceholderRE.test(redirects);
const hasHomePlaceholder = homePlaceholderRE.test(redirects); const hasNewsPlaceholder = newsPlaceholderRE.test(redirects);
const hasForumPlaceholder = forumPlacehilderRE.test(redirects); const hasForumPlaceholder = forumPlaceholderRE.test(redirects);
const hasForumProxyPlaceholder = forumProxyPlaceholderRE.test(redirects);
expect(hasApiPlaceholder).toBe(false); expect(hasApiPlaceholder).toBe(false);
expect(hasHomePlaceholder).toBe(false); expect(hasNewsPlaceholder).toBe(false);
expect(hasForumPlaceholder).toBe(false); expect(hasForumPlaceholder).toBe(false);
expect(hasForumProxyPlaceholder).toBe(false);
const { api, home, forum } = testLocations; const { api, forum } = testLocations;
expect(redirects.includes(`${api}/internal/:splat`)).toBe(true); expect(redirects.includes(`${api}/internal/:splat`)).toBe(true);
expect( expect(
redirects.includes( redirects.includes(
`${home}/forum/t/free-code-camp-privacy-policy/19545 301` `${forum}/t/free-code-camp-privacy-policy/19545 301`
) )
).toBe(true); ).toBe(true);
expect(redirects.includes(`${forum}`)).toBe(true); expect(redirects.includes(`${forum}`)).toBe(true);
@ -50,15 +47,15 @@ describe('createRedirects', () => {
expect.assertions(3); expect.assertions(3);
const api = 'api'; const api = 'api';
const home = 'home'; const news = 'news';
const forum = 'forum'; const forum = 'forum';
const noApi = { forum, home }; const noApi = { forum, news };
const noHome = { api, forum }; const noNews = { api, forum };
const noForum = { api, home }; const noForum = { api, news };
expect(() => createRedirects(noApi)).toThrow(); expect(() => createRedirects(noApi)).toThrow();
expect(() => createRedirects(noHome)).toThrow(); expect(() => createRedirects(noNews)).toThrow();
expect(() => createRedirects(noForum)).toThrow(); expect(() => createRedirects(noForum)).toThrow();
}); });