2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 12:47:54 -04: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-09-14 16:21:23 -04:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {TestBed} from '@angular/core/testing';
|
|
|
|
import {BrowserModule, Title} from '@angular/platform-browser';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
2015-03-09 12:39:18 -04:00
|
|
|
|
2017-12-16 17:42:55 -05:00
|
|
|
{
|
2015-03-09 12:39:18 -04:00
|
|
|
describe('title service', () => {
|
2017-08-08 05:17:40 -04:00
|
|
|
let doc: Document;
|
|
|
|
let initialTitle: string;
|
|
|
|
let titleService: Title;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
doc = getDOM().createHtmlDocument();
|
2019-08-30 14:31:24 -04:00
|
|
|
initialTitle = doc.title;
|
2017-08-08 05:17:40 -04:00
|
|
|
titleService = new Title(doc);
|
|
|
|
});
|
2015-03-09 12:39:18 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
afterEach(() => {
|
|
|
|
doc.title = initialTitle;
|
|
|
|
});
|
2015-03-09 12:39:18 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should allow reading initial title', () => {
|
|
|
|
expect(titleService.getTitle()).toEqual(initialTitle);
|
|
|
|
});
|
2015-03-09 12:39:18 -04:00
|
|
|
|
|
|
|
it('should set a title on the injected document', () => {
|
|
|
|
titleService.setTitle('test title');
|
2019-08-30 14:31:24 -04:00
|
|
|
expect(doc.title).toEqual('test title');
|
2015-03-09 12:39:18 -04:00
|
|
|
expect(titleService.getTitle()).toEqual('test title');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should reset title to empty string if title not provided', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
titleService.setTitle(null!);
|
2019-08-30 14:31:24 -04:00
|
|
|
expect(doc.title).toEqual('');
|
2015-03-09 12:39:18 -04:00
|
|
|
});
|
2016-09-14 16:21:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('integration test', () => {
|
|
|
|
@Injectable()
|
|
|
|
class DependsOnTitle {
|
|
|
|
constructor(public title: Title) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
imports: [BrowserModule],
|
|
|
|
providers: [DependsOnTitle],
|
|
|
|
});
|
|
|
|
});
|
2015-03-09 12:39:18 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should inject Title service when using BrowserModule', () => {
|
|
|
|
expect(TestBed.inject(DependsOnTitle).title).toBeAnInstanceOf(Title);
|
|
|
|
});
|
2015-03-09 12:39:18 -04:00
|
|
|
});
|
|
|
|
}
|