| 
									
										
										
										
											2019-02-04 12:34:44 +00:00
										 |  |  | /* global describe expect it */ | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-13 18:48:24 +05:30
										 |  |  | const { createRedirects } = require('./create-redirects'); | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const testLocations = { | 
					
						
							|  |  |  |   api: 'https://api.example.com', | 
					
						
							| 
									
										
										
										
											2019-10-07 16:42:07 -07:00
										 |  |  |   newsProxy: 'https://news.example.com', | 
					
						
							|  |  |  |   forumProxy: 'https://forum.example.com' | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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', () => { | 
					
						
							| 
									
										
										
										
											2020-03-06 17:51:58 +01:00
										 |  |  |     expect.assertions(4); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 17:51:58 +01:00
										 |  |  |     const { forumProxy } = testLocations; | 
					
						
							| 
									
										
										
										
											2019-10-07 16:42:07 -07:00
										 |  |  |     expect(redirects.includes(`${forumProxy}`)).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-10-07 16:42:07 -07:00
										 |  |  |     const newsProxy = 'newsProxy'; | 
					
						
							|  |  |  |     const forumProxy = 'forumProxy'; | 
					
						
							| 
									
										
										
										
											2018-10-08 00:00:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-07 16:42:07 -07:00
										 |  |  |     const noApi = { forumProxy, newsProxy }; | 
					
						
							|  |  |  |     const noNews = { api, forumProxy }; | 
					
						
							|  |  |  |     const noForum = { api, newsProxy }; | 
					
						
							| 
									
										
										
										
											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()); | 
					
						
							|  |  |  | }); |