2016-12-08 21:44:28 -05:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-12-08 21:44:28 -05:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-08-22 22:16:25 -04:00
|
|
|
import {ɵgetDOM as getDOM} from '@angular/common';
|
2016-12-08 21:44:28 -05:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {TestBed} from '@angular/core/testing';
|
|
|
|
import {BrowserModule, Meta} from '@angular/platform-browser';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2016-12-08 21:44:28 -05:00
|
|
|
describe('Meta service', () => {
|
2017-08-08 05:17:40 -04:00
|
|
|
let doc: Document;
|
|
|
|
let metaService: Meta;
|
2016-12-08 21:44:28 -05:00
|
|
|
let defaultMeta: HTMLMetaElement;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2017-08-08 05:17:40 -04:00
|
|
|
doc = getDOM().createHtmlDocument();
|
|
|
|
metaService = new Meta(doc);
|
2016-12-08 21:44:28 -05:00
|
|
|
defaultMeta = getDOM().createElement('meta', doc) as HTMLMetaElement;
|
2019-08-30 15:52:48 -04:00
|
|
|
defaultMeta.setAttribute('property', 'fb:app_id');
|
|
|
|
defaultMeta.setAttribute('content', '123456789');
|
|
|
|
doc.getElementsByTagName('head')[0].appendChild(defaultMeta);
|
2016-12-08 21:44:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => getDOM().remove(defaultMeta));
|
|
|
|
|
|
|
|
it('should return meta tag matching selector', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const actual: HTMLMetaElement = metaService.getTag('property="fb:app_id"')!;
|
2016-12-08 21:44:28 -05:00
|
|
|
expect(actual).not.toBeNull();
|
2019-08-30 15:52:48 -04:00
|
|
|
expect(actual.getAttribute('content')).toEqual('123456789');
|
2016-12-08 21:44:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return all meta tags matching selector', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const tag1 = metaService.addTag({name: 'author', content: 'page author'})!;
|
|
|
|
const tag2 = metaService.addTag({name: 'author', content: 'another page author'})!;
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
const actual: HTMLMetaElement[] = metaService.getTags('name=author');
|
|
|
|
expect(actual.length).toEqual(2);
|
2019-08-30 15:52:48 -04:00
|
|
|
expect(actual[0].getAttribute('content')).toEqual('page author');
|
|
|
|
expect(actual[1].getAttribute('content')).toEqual('another page author');
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
metaService.removeTagElement(tag1);
|
|
|
|
metaService.removeTagElement(tag2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should return null if meta tag does not exist', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const actual: HTMLMetaElement = metaService.getTag('fake=fake')!;
|
2016-12-08 21:44:28 -05:00
|
|
|
expect(actual).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove meta tag by the given selector', () => {
|
2017-03-01 14:19:22 -05:00
|
|
|
const selector = 'name=author';
|
|
|
|
expect(metaService.getTag(selector)).toBeNull();
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
metaService.addTag({name: 'author', content: 'page author'});
|
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
expect(metaService.getTag(selector)).not.toBeNull();
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
metaService.removeTag(selector);
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
expect(metaService.getTag(selector)).toBeNull();
|
2016-12-08 21:44:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove meta tag by the given element', () => {
|
2017-03-01 14:19:22 -05:00
|
|
|
const selector = 'name=keywords';
|
|
|
|
expect(metaService.getTag(selector)).toBeNull();
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
metaService.addTags([{name: 'keywords', content: 'meta test'}]);
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
const meta = metaService.getTag(selector)!;
|
2016-12-08 21:44:28 -05:00
|
|
|
expect(meta).not.toBeNull();
|
|
|
|
|
|
|
|
metaService.removeTagElement(meta);
|
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
expect(metaService.getTag(selector)).toBeNull();
|
2016-12-08 21:44:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should update meta tag matching the given selector', () => {
|
2017-03-01 14:19:22 -05:00
|
|
|
const selector = 'property="fb:app_id"';
|
|
|
|
metaService.updateTag({content: '4321'}, selector);
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
const actual = metaService.getTag(selector);
|
2016-12-08 21:44:28 -05:00
|
|
|
expect(actual).not.toBeNull();
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(actual!.getAttribute('content')).toEqual('4321');
|
2016-12-08 21:44:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should extract selector from the tag definition', () => {
|
2017-03-01 14:19:22 -05:00
|
|
|
const selector = 'property="fb:app_id"';
|
2016-12-08 21:44:28 -05:00
|
|
|
metaService.updateTag({property: 'fb:app_id', content: '666'});
|
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
const actual = metaService.getTag(selector);
|
2016-12-08 21:44:28 -05:00
|
|
|
expect(actual).not.toBeNull();
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(actual!.getAttribute('content')).toEqual('666');
|
2016-12-08 21:44:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create meta tag if it does not exist', () => {
|
2017-03-01 14:19:22 -05:00
|
|
|
const selector = 'name="twitter:title"';
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
metaService.updateTag({name: 'twitter:title', content: 'Content Title'}, selector);
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
const actual = metaService.getTag(selector)!;
|
2016-12-08 21:44:28 -05:00
|
|
|
expect(actual).not.toBeNull();
|
2019-08-30 15:52:48 -04:00
|
|
|
expect(actual.getAttribute('content')).toEqual('Content Title');
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
metaService.removeTagElement(actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add new meta tag', () => {
|
2017-03-01 14:19:22 -05:00
|
|
|
const selector = 'name="og:title"';
|
|
|
|
expect(metaService.getTag(selector)).toBeNull();
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
metaService.addTag({name: 'og:title', content: 'Content Title'});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
const actual = metaService.getTag(selector)!;
|
2016-12-08 21:44:28 -05:00
|
|
|
expect(actual).not.toBeNull();
|
2019-08-30 15:52:48 -04:00
|
|
|
expect(actual.getAttribute('content')).toEqual('Content Title');
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
metaService.removeTagElement(actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should add multiple new meta tags', () => {
|
2017-03-01 14:19:22 -05:00
|
|
|
const nameSelector = 'name="twitter:title"';
|
|
|
|
const propertySelector = 'property="og:title"';
|
|
|
|
expect(metaService.getTag(nameSelector)).toBeNull();
|
|
|
|
expect(metaService.getTag(propertySelector)).toBeNull();
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
metaService.addTags([
|
|
|
|
{name: 'twitter:title', content: 'Content Title'},
|
|
|
|
{property: 'og:title', content: 'Content Title'}
|
|
|
|
]);
|
2020-04-13 19:40:21 -04:00
|
|
|
const twitterMeta = metaService.getTag(nameSelector)!;
|
|
|
|
const fbMeta = metaService.getTag(propertySelector)!;
|
2016-12-08 21:44:28 -05: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 14:19:22 -05:00
|
|
|
const selector = 'property="fb:app_id"';
|
|
|
|
expect(metaService.getTags(selector).length).toEqual(1);
|
2016-12-08 21:44:28 -05:00
|
|
|
|
|
|
|
metaService.addTag({property: 'fb:app_id', content: '123456789'});
|
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
expect(metaService.getTags(selector).length).toEqual(1);
|
2016-12-08 21:44:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should add meta tag if it is already present on the page and but has different attr',
|
|
|
|
() => {
|
2017-03-01 14:19:22 -05:00
|
|
|
const selector = 'property="fb:app_id"';
|
|
|
|
expect(metaService.getTags(selector).length).toEqual(1);
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
const meta = metaService.addTag({property: 'fb:app_id', content: '666'})!;
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
expect(metaService.getTags(selector).length).toEqual(2);
|
2016-12-08 21:44:28 -05: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 14:19:22 -05:00
|
|
|
const selector = 'property="fb:app_id"';
|
|
|
|
expect(metaService.getTags(selector).length).toEqual(1);
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
const meta = metaService.addTag({property: 'fb:app_id', content: '123456789'}, true)!;
|
2016-12-08 21:44:28 -05:00
|
|
|
|
2017-03-01 14:19:22 -05:00
|
|
|
expect(metaService.getTags(selector).length).toEqual(2);
|
2016-12-08 21:44:28 -05: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',
|
2019-08-28 19:22:36 -04:00
|
|
|
() => expect(TestBed.inject(DependsOnMeta).meta).toBeAnInstanceOf(Meta));
|
2016-12-08 21:44:28 -05:00
|
|
|
});
|
|
|
|
}
|