| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {Injectable} from '@angular/core'; | 
					
						
							|  |  |  | import {TestBed} from '@angular/core/testing'; | 
					
						
							|  |  |  | import {BrowserModule, Meta} from '@angular/platform-browser'; | 
					
						
							|  |  |  | import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; | 
					
						
							|  |  |  | import {expect} from '@angular/platform-browser/testing/matchers'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('Meta service', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |     const doc = getDOM().createHtmlDocument(); | 
					
						
							|  |  |  |     const metaService = new Meta(doc); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |     let defaultMeta: HTMLMetaElement; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       defaultMeta = getDOM().createElement('meta', doc) as HTMLMetaElement; | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       getDOM().setAttribute(defaultMeta, 'property', 'fb:app_id'); | 
					
						
							|  |  |  |       getDOM().setAttribute(defaultMeta, 'content', '123456789'); | 
					
						
							|  |  |  |       getDOM().appendChild(getDOM().getElementsByTagName(doc, 'head')[0], defaultMeta); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     afterEach(() => getDOM().remove(defaultMeta)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return meta tag matching selector', () => { | 
					
						
							|  |  |  |       const actual: HTMLMetaElement = metaService.getTag('property="fb:app_id"'); | 
					
						
							|  |  |  |       expect(actual).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(getDOM().getAttribute(actual, 'content')).toEqual('123456789'); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return all meta tags matching selector', () => { | 
					
						
							|  |  |  |       const tag1 = metaService.addTag({name: 'author', content: 'page author'}); | 
					
						
							|  |  |  |       const tag2 = metaService.addTag({name: 'author', content: 'another page author'}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const actual: HTMLMetaElement[] = metaService.getTags('name=author'); | 
					
						
							|  |  |  |       expect(actual.length).toEqual(2); | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(getDOM().getAttribute(actual[0], 'content')).toEqual('page author'); | 
					
						
							|  |  |  |       expect(getDOM().getAttribute(actual[1], 'content')).toEqual('another page author'); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // clean up
 | 
					
						
							|  |  |  |       metaService.removeTagElement(tag1); | 
					
						
							|  |  |  |       metaService.removeTagElement(tag2); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return null if meta tag does not exist', () => { | 
					
						
							|  |  |  |       const actual: HTMLMetaElement = metaService.getTag('fake=fake'); | 
					
						
							|  |  |  |       expect(actual).toBeNull(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should remove meta tag by the given selector', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const selector = 'name=author'; | 
					
						
							|  |  |  |       expect(metaService.getTag(selector)).toBeNull(); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       metaService.addTag({name: 'author', content: 'page author'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(metaService.getTag(selector)).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       metaService.removeTag(selector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(metaService.getTag(selector)).toBeNull(); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should remove meta tag by the given element', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const selector = 'name=keywords'; | 
					
						
							|  |  |  |       expect(metaService.getTag(selector)).toBeNull(); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       metaService.addTags([{name: 'keywords', content: 'meta test'}]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const meta = metaService.getTag(selector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |       expect(meta).not.toBeNull(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       metaService.removeTagElement(meta); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(metaService.getTag(selector)).toBeNull(); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should update meta tag matching the given selector', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const selector = 'property="fb:app_id"'; | 
					
						
							|  |  |  |       metaService.updateTag({content: '4321'}, selector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const actual = metaService.getTag(selector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |       expect(actual).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(getDOM().getAttribute(actual, 'content')).toEqual('4321'); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should extract selector from the tag definition', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const selector = 'property="fb:app_id"'; | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |       metaService.updateTag({property: 'fb:app_id', content: '666'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const actual = metaService.getTag(selector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |       expect(actual).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(getDOM().getAttribute(actual, 'content')).toEqual('666'); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should create meta tag if it does not exist', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const selector = 'name="twitter:title"'; | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       metaService.updateTag({name: 'twitter:title', content: 'Content Title'}, selector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const actual = metaService.getTag(selector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |       expect(actual).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(getDOM().getAttribute(actual, 'content')).toEqual('Content Title'); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // clean up
 | 
					
						
							|  |  |  |       metaService.removeTagElement(actual); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should add new meta tag', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const selector = 'name="og:title"'; | 
					
						
							|  |  |  |       expect(metaService.getTag(selector)).toBeNull(); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       metaService.addTag({name: 'og:title', content: 'Content Title'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const actual = metaService.getTag(selector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |       expect(actual).not.toBeNull(); | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(getDOM().getAttribute(actual, 'content')).toEqual('Content Title'); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // clean up
 | 
					
						
							|  |  |  |       metaService.removeTagElement(actual); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should add multiple new meta tags', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const nameSelector = 'name="twitter:title"'; | 
					
						
							|  |  |  |       const propertySelector = 'property="og:title"'; | 
					
						
							|  |  |  |       expect(metaService.getTag(nameSelector)).toBeNull(); | 
					
						
							|  |  |  |       expect(metaService.getTag(propertySelector)).toBeNull(); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       metaService.addTags([ | 
					
						
							|  |  |  |         {name: 'twitter:title', content: 'Content Title'}, | 
					
						
							|  |  |  |         {property: 'og:title', content: 'Content Title'} | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const twitterMeta = metaService.getTag(nameSelector); | 
					
						
							|  |  |  |       const fbMeta = metaService.getTag(propertySelector); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |       expect(twitterMeta).not.toBeNull(); | 
					
						
							|  |  |  |       expect(fbMeta).not.toBeNull(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // clean up
 | 
					
						
							|  |  |  |       metaService.removeTagElement(twitterMeta); | 
					
						
							|  |  |  |       metaService.removeTagElement(fbMeta); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should not add meta tag if it is already present on the page and has the same attr', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const selector = 'property="fb:app_id"'; | 
					
						
							|  |  |  |       expect(metaService.getTags(selector).length).toEqual(1); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       metaService.addTag({property: 'fb:app_id', content: '123456789'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(metaService.getTags(selector).length).toEqual(1); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should add meta tag if it is already present on the page and but has different attr', | 
					
						
							|  |  |  |        () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |          const selector = 'property="fb:app_id"'; | 
					
						
							|  |  |  |          expect(metaService.getTags(selector).length).toEqual(1); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |          const meta = metaService.addTag({property: 'fb:app_id', content: '666'}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |          expect(metaService.getTags(selector).length).toEqual(2); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |          // clean up
 | 
					
						
							|  |  |  |          metaService.removeTagElement(meta); | 
					
						
							|  |  |  |        }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should add meta tag if it is already present on the page and force true', () => { | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       const selector = 'property="fb:app_id"'; | 
					
						
							|  |  |  |       expect(metaService.getTags(selector).length).toEqual(1); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const meta = metaService.addTag({property: 'fb:app_id', content: '123456789'}, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 11:19:22 -08:00
										 |  |  |       expect(metaService.getTags(selector).length).toEqual(2); | 
					
						
							| 
									
										
										
										
											2016-12-09 05:44:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // clean up
 | 
					
						
							|  |  |  |       metaService.removeTagElement(meta); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('integration test', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @Injectable() | 
					
						
							|  |  |  |     class DependsOnMeta { | 
					
						
							|  |  |  |       constructor(public meta: Meta) {} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureTestingModule({ | 
					
						
							|  |  |  |         imports: [BrowserModule], | 
					
						
							|  |  |  |         providers: [DependsOnMeta], | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should inject Meta service when using BrowserModule', | 
					
						
							|  |  |  |        () => expect(TestBed.get(DependsOnMeta).meta).toBeAnInstanceOf(Meta)); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |