| 
									
										
										
										
											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', | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |   news: 'https://news.example.com', | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |   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-05 18:08:40 +00:00
										 |  |  |     expect.assertions(6); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const apiPlaceholderRE = /#\{\{API\}\}/; | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |     const newsPlaceholderRE = /#\{\{NEWS\}\}/; | 
					
						
							|  |  |  |     const forumPlaceholderRE = /#\{\{FORUM\}\}/; | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |     const redirects = createRedirects(testLocations); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const hasApiPlaceholder = apiPlaceholderRE.test(redirects); | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |     const hasNewsPlaceholder = newsPlaceholderRE.test(redirects); | 
					
						
							|  |  |  |     const hasForumPlaceholder = forumPlaceholderRE.test(redirects); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(hasApiPlaceholder).toBe(false); | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |     expect(hasNewsPlaceholder).toBe(false); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |     expect(hasForumPlaceholder).toBe(false); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |     const { api, forum } = testLocations; | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |     expect(redirects.includes(`${api}/internal/:splat`)).toBe(true); | 
					
						
							|  |  |  |     expect( | 
					
						
							|  |  |  |       redirects.includes( | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |         `${forum}/t/free-code-camp-privacy-policy/19545 301` | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |       ) | 
					
						
							|  |  |  |     ).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'; | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |     const news = 'news'; | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |     const forum = 'forum'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |     const noApi = { forum, news }; | 
					
						
							|  |  |  |     const noNews = { api, forum }; | 
					
						
							|  |  |  |     const noForum = { api, news }; | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     expect(() => createRedirects(noApi)).toThrow(); | 
					
						
							| 
									
										
										
										
											2019-02-05 18:08:40 +00:00
										 |  |  |     expect(() => createRedirects(noNews)).toThrow(); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  |     expect(() => createRedirects(noForum)).toThrow(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it('matches the snapshot', () => | 
					
						
							|  |  |  |     expect(createRedirects(testLocations)).toMatchSnapshot()); | 
					
						
							|  |  |  | }); |