Selecting a specific TemplateRef from Angular component test?@Directive v/s @Component in AngularHow can I select an element in a component template?Angular - Use pipes in services and componentsBinding select element to object in AngularDynamic tabs with user-click chosen componentsAngular/RxJs When should I unsubscribe from `Subscription`Angular 2 + jQuery + Materialize - how to Test?Testing angular service subscribe/unsubscribe from within a componentAngular Unit Test Component Callthrough On SpyObject of ServiceHow to test data loading states in an angular component?

How do we know neutrons have no charge?

Garage door sticks on a bolt

What are examples of EU policies that are beneficial for one EU country, disadvantagious for another?

Top off gas with old oil, is that bad?

Knights and Knaves: What does C say?

Why would an airline put 15 passengers at once on standby?

What in my code changed between MacTeX 2017 and MacTex 2019?

An impressive body of work

Equations with summation ("sum") symbols

Calculate the Ultraradical

Kinematic formula for Euler characteristic

Is population size a parameter, or sample size a statistic?

Sci-fi movie with one survivor and an organism(?) recreating his memories

Concerning a relationship in the team

Installation of ImageMagick software fails with "configure: error: libltdl is required"

Is there a relationship between prime numbers and music?

Mathematica code for bifurcation diagram in 3D

Is the order of words purely based on convention?

Can I exile my opponent's Progenitus/True-Name Nemesis with Teferi, Hero of Dominaria's emblem?

Population of post-Soviet states. Why decreasing?

Windows 10 deletes lots of tiny files super slowly. Anything that can be done to speed it up?

Why does my browser attempt to download pages from http://clhs.lisp.se instead of viewing them normally?

Why isn't there armor to protect from spells in the Potterverse?

Where to find the Arxiv endorsement code?



Selecting a specific TemplateRef from Angular component test?


@Directive v/s @Component in AngularHow can I select an element in a component template?Angular - Use pipes in services and componentsBinding select element to object in AngularDynamic tabs with user-click chosen componentsAngular/RxJs When should I unsubscribe from `Subscription`Angular 2 + jQuery + Materialize - how to Test?Testing angular service subscribe/unsubscribe from within a componentAngular Unit Test Component Callthrough On SpyObject of ServiceHow to test data loading states in an angular component?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have the following component that has links to open a modal, which I can vary the content of by passing a template reference into.



<a id="spec-tos" (click)="openModal($event, termsOfServiceModalTemplate)">Terms of Service</a>
<a id="spec-pp" (click)="openModal($event, privacyPolicyModalTemplate)">Privacy</a>

<ng-template #termsOfServiceModalTemplate>
terms of service text goes here...
</ng-template>

<ng-template #privacyPolicyModalTemplate>
privacy policy text goes here...
</ng-template>


export class FooterComponent BsModalRef;

constructor(private modalService: BsModalService)

openModal($event: Event, template: TemplateRef<NgTemplateOutlet>): void
$event.preventDefault();
this.modalRef = this.modalService.show(template,
class: 'modal-dialog-scrollable'
);




How can I test that the links open the correct template? I need to be able to select the templateRef ID from my test, but I'm not sure how to do this. here's my test



it('should call the openModal() method when clicking on the privacy policy link', () => 
spyOn(component, 'openModal');
const link = debugEl.query(By.css('#spec-pp'));
//This next line is wrong and does not work
const tmpl = debugEl.query(By.directive(TemplateRef));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledWith(new MouseEvent('click'), tmpl);
);


I know that debugEl.query(By.directive(TemplateRef)) does not work... but that's just to give an idea of what I want to do here. How do I select a specific template from the component I'm testing?




Edit: The answer from @neo below is the solution, but I was able to take his solution and package it up into a re-usable helper function



/**
* Used for detecting an `<ng-template>` element reference that was passed to a function, typically for modals
* We don't have access to much except the name of the template and a number that it maps to internally.
* If we can return the number, then we've found the template by name, which is all we really want to do here
* //https://stackoverflow.com/a/55432163/79677
*/
export const templateExists = (template: TemplateRef<NgTemplateOutlet>, templateName: string): boolean =>
// tslint:disable-next-line:no-any -- There is no other way to get this object, it doesn't exist in the typedefs!
const refs = (template as any)._def.references as [templateName: string]: number;
//If we have a number, then we've found the template by name
return !isNaN(refs[templateName]);
;


Use it like this:



it('should call the openModal() method when clicking on the privacy policy link', () => 
const modalSpy = spyOn(component, 'openModal');
const link = debugEl.query(By.css('.footer-nav .spec-privacy'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(templateExists(modalSpy.calls.mostRecent().args[1], 'privacyPolicyModalTemplate')).toEqual(true);
);









share|improve this question


























  • Can you provide the BsModalRef and BsModalService classes, I think I may able to help you.

    – Anuradha Mudalige
    Mar 28 at 19:43











  • those are from ngx-bootstrap valor-software.com/ngx-bootstrap/#/modals - I don't own them

    – CBarr
    Mar 28 at 21:22











  • I’ve added the answer below, can you check it?

    – Anuradha Mudalige
    Mar 31 at 4:14

















0















I have the following component that has links to open a modal, which I can vary the content of by passing a template reference into.



<a id="spec-tos" (click)="openModal($event, termsOfServiceModalTemplate)">Terms of Service</a>
<a id="spec-pp" (click)="openModal($event, privacyPolicyModalTemplate)">Privacy</a>

<ng-template #termsOfServiceModalTemplate>
terms of service text goes here...
</ng-template>

<ng-template #privacyPolicyModalTemplate>
privacy policy text goes here...
</ng-template>


export class FooterComponent BsModalRef;

constructor(private modalService: BsModalService)

openModal($event: Event, template: TemplateRef<NgTemplateOutlet>): void
$event.preventDefault();
this.modalRef = this.modalService.show(template,
class: 'modal-dialog-scrollable'
);




How can I test that the links open the correct template? I need to be able to select the templateRef ID from my test, but I'm not sure how to do this. here's my test



it('should call the openModal() method when clicking on the privacy policy link', () => 
spyOn(component, 'openModal');
const link = debugEl.query(By.css('#spec-pp'));
//This next line is wrong and does not work
const tmpl = debugEl.query(By.directive(TemplateRef));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledWith(new MouseEvent('click'), tmpl);
);


I know that debugEl.query(By.directive(TemplateRef)) does not work... but that's just to give an idea of what I want to do here. How do I select a specific template from the component I'm testing?




Edit: The answer from @neo below is the solution, but I was able to take his solution and package it up into a re-usable helper function



/**
* Used for detecting an `<ng-template>` element reference that was passed to a function, typically for modals
* We don't have access to much except the name of the template and a number that it maps to internally.
* If we can return the number, then we've found the template by name, which is all we really want to do here
* //https://stackoverflow.com/a/55432163/79677
*/
export const templateExists = (template: TemplateRef<NgTemplateOutlet>, templateName: string): boolean =>
// tslint:disable-next-line:no-any -- There is no other way to get this object, it doesn't exist in the typedefs!
const refs = (template as any)._def.references as [templateName: string]: number;
//If we have a number, then we've found the template by name
return !isNaN(refs[templateName]);
;


Use it like this:



it('should call the openModal() method when clicking on the privacy policy link', () => 
const modalSpy = spyOn(component, 'openModal');
const link = debugEl.query(By.css('.footer-nav .spec-privacy'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(templateExists(modalSpy.calls.mostRecent().args[1], 'privacyPolicyModalTemplate')).toEqual(true);
);









share|improve this question


























  • Can you provide the BsModalRef and BsModalService classes, I think I may able to help you.

    – Anuradha Mudalige
    Mar 28 at 19:43











  • those are from ngx-bootstrap valor-software.com/ngx-bootstrap/#/modals - I don't own them

    – CBarr
    Mar 28 at 21:22











  • I’ve added the answer below, can you check it?

    – Anuradha Mudalige
    Mar 31 at 4:14













0












0








0








I have the following component that has links to open a modal, which I can vary the content of by passing a template reference into.



<a id="spec-tos" (click)="openModal($event, termsOfServiceModalTemplate)">Terms of Service</a>
<a id="spec-pp" (click)="openModal($event, privacyPolicyModalTemplate)">Privacy</a>

<ng-template #termsOfServiceModalTemplate>
terms of service text goes here...
</ng-template>

<ng-template #privacyPolicyModalTemplate>
privacy policy text goes here...
</ng-template>


export class FooterComponent BsModalRef;

constructor(private modalService: BsModalService)

openModal($event: Event, template: TemplateRef<NgTemplateOutlet>): void
$event.preventDefault();
this.modalRef = this.modalService.show(template,
class: 'modal-dialog-scrollable'
);




How can I test that the links open the correct template? I need to be able to select the templateRef ID from my test, but I'm not sure how to do this. here's my test



it('should call the openModal() method when clicking on the privacy policy link', () => 
spyOn(component, 'openModal');
const link = debugEl.query(By.css('#spec-pp'));
//This next line is wrong and does not work
const tmpl = debugEl.query(By.directive(TemplateRef));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledWith(new MouseEvent('click'), tmpl);
);


I know that debugEl.query(By.directive(TemplateRef)) does not work... but that's just to give an idea of what I want to do here. How do I select a specific template from the component I'm testing?




Edit: The answer from @neo below is the solution, but I was able to take his solution and package it up into a re-usable helper function



/**
* Used for detecting an `<ng-template>` element reference that was passed to a function, typically for modals
* We don't have access to much except the name of the template and a number that it maps to internally.
* If we can return the number, then we've found the template by name, which is all we really want to do here
* //https://stackoverflow.com/a/55432163/79677
*/
export const templateExists = (template: TemplateRef<NgTemplateOutlet>, templateName: string): boolean =>
// tslint:disable-next-line:no-any -- There is no other way to get this object, it doesn't exist in the typedefs!
const refs = (template as any)._def.references as [templateName: string]: number;
//If we have a number, then we've found the template by name
return !isNaN(refs[templateName]);
;


Use it like this:



it('should call the openModal() method when clicking on the privacy policy link', () => 
const modalSpy = spyOn(component, 'openModal');
const link = debugEl.query(By.css('.footer-nav .spec-privacy'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(templateExists(modalSpy.calls.mostRecent().args[1], 'privacyPolicyModalTemplate')).toEqual(true);
);









share|improve this question
















I have the following component that has links to open a modal, which I can vary the content of by passing a template reference into.



<a id="spec-tos" (click)="openModal($event, termsOfServiceModalTemplate)">Terms of Service</a>
<a id="spec-pp" (click)="openModal($event, privacyPolicyModalTemplate)">Privacy</a>

<ng-template #termsOfServiceModalTemplate>
terms of service text goes here...
</ng-template>

<ng-template #privacyPolicyModalTemplate>
privacy policy text goes here...
</ng-template>


export class FooterComponent BsModalRef;

constructor(private modalService: BsModalService)

openModal($event: Event, template: TemplateRef<NgTemplateOutlet>): void
$event.preventDefault();
this.modalRef = this.modalService.show(template,
class: 'modal-dialog-scrollable'
);




How can I test that the links open the correct template? I need to be able to select the templateRef ID from my test, but I'm not sure how to do this. here's my test



it('should call the openModal() method when clicking on the privacy policy link', () => 
spyOn(component, 'openModal');
const link = debugEl.query(By.css('#spec-pp'));
//This next line is wrong and does not work
const tmpl = debugEl.query(By.directive(TemplateRef));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledWith(new MouseEvent('click'), tmpl);
);


I know that debugEl.query(By.directive(TemplateRef)) does not work... but that's just to give an idea of what I want to do here. How do I select a specific template from the component I'm testing?




Edit: The answer from @neo below is the solution, but I was able to take his solution and package it up into a re-usable helper function



/**
* Used for detecting an `<ng-template>` element reference that was passed to a function, typically for modals
* We don't have access to much except the name of the template and a number that it maps to internally.
* If we can return the number, then we've found the template by name, which is all we really want to do here
* //https://stackoverflow.com/a/55432163/79677
*/
export const templateExists = (template: TemplateRef<NgTemplateOutlet>, templateName: string): boolean =>
// tslint:disable-next-line:no-any -- There is no other way to get this object, it doesn't exist in the typedefs!
const refs = (template as any)._def.references as [templateName: string]: number;
//If we have a number, then we've found the template by name
return !isNaN(refs[templateName]);
;


Use it like this:



it('should call the openModal() method when clicking on the privacy policy link', () => 
const modalSpy = spyOn(component, 'openModal');
const link = debugEl.query(By.css('.footer-nav .spec-privacy'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(templateExists(modalSpy.calls.mostRecent().args[1], 'privacyPolicyModalTemplate')).toEqual(true);
);






angular angular-template angular-test angular-testing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 11 at 16:24







CBarr

















asked Mar 28 at 19:00









CBarrCBarr

13.9k17 gold badges71 silver badges106 bronze badges




13.9k17 gold badges71 silver badges106 bronze badges















  • Can you provide the BsModalRef and BsModalService classes, I think I may able to help you.

    – Anuradha Mudalige
    Mar 28 at 19:43











  • those are from ngx-bootstrap valor-software.com/ngx-bootstrap/#/modals - I don't own them

    – CBarr
    Mar 28 at 21:22











  • I’ve added the answer below, can you check it?

    – Anuradha Mudalige
    Mar 31 at 4:14

















  • Can you provide the BsModalRef and BsModalService classes, I think I may able to help you.

    – Anuradha Mudalige
    Mar 28 at 19:43











  • those are from ngx-bootstrap valor-software.com/ngx-bootstrap/#/modals - I don't own them

    – CBarr
    Mar 28 at 21:22











  • I’ve added the answer below, can you check it?

    – Anuradha Mudalige
    Mar 31 at 4:14
















Can you provide the BsModalRef and BsModalService classes, I think I may able to help you.

– Anuradha Mudalige
Mar 28 at 19:43





Can you provide the BsModalRef and BsModalService classes, I think I may able to help you.

– Anuradha Mudalige
Mar 28 at 19:43













those are from ngx-bootstrap valor-software.com/ngx-bootstrap/#/modals - I don't own them

– CBarr
Mar 28 at 21:22





those are from ngx-bootstrap valor-software.com/ngx-bootstrap/#/modals - I don't own them

– CBarr
Mar 28 at 21:22













I’ve added the answer below, can you check it?

– Anuradha Mudalige
Mar 31 at 4:14





I’ve added the answer below, can you check it?

– Anuradha Mudalige
Mar 31 at 4:14












1 Answer
1






active

oldest

votes


















3
















Sorry for the delay, been busy these days. You should modify your spec.ts as follows.



import TestBed, async, ComponentFixture from '@angular/core/testing';
import FooterComponent from './footer.component';
import BrowserModule, By from '@angular/platform-browser';
import BrowserAnimationsModule from '@angular/platform-browser/animations';
import MatButtonModule from '@angular/material';
import ModalModule from 'ngx-bootstrap';

describe('FooterComponent ', () =>
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [
FooterComponent
],
imports: [
// your imports here
ModalModule.forRoot()
]
).compileComponents();
));

beforeEach(() =>
fixture = TestBed.createComponent(FooterComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
);

it('should create the app', () =>
expect(component).toBeTruthy();
);


it('should call the openModal() method when clicking on the links', () =>
const obj = spyOn(component, 'openModal');
let link = fixture.debugElement.query(By.css('#spec-tos'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(obj.calls.mostRecent().args[1]._def.references.termsOfServiceModalTemplate).toBeDefined();

link = fixture.debugElement.query(By.css('#spec-pp'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledTimes(2);
expect(obj.calls.mostRecent().args[1]._def.references.privacyPolicyModalTemplate).toBeDefined();
);
);



Enjoy!!






share|improve this answer

























  • Very cool, thank you! Too bad these things aren't strongly typed, but at least I can make them testable

    – CBarr
    Apr 1 at 13:16













Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55405056%2fselecting-a-specific-templateref-from-angular-component-test%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3
















Sorry for the delay, been busy these days. You should modify your spec.ts as follows.



import TestBed, async, ComponentFixture from '@angular/core/testing';
import FooterComponent from './footer.component';
import BrowserModule, By from '@angular/platform-browser';
import BrowserAnimationsModule from '@angular/platform-browser/animations';
import MatButtonModule from '@angular/material';
import ModalModule from 'ngx-bootstrap';

describe('FooterComponent ', () =>
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [
FooterComponent
],
imports: [
// your imports here
ModalModule.forRoot()
]
).compileComponents();
));

beforeEach(() =>
fixture = TestBed.createComponent(FooterComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
);

it('should create the app', () =>
expect(component).toBeTruthy();
);


it('should call the openModal() method when clicking on the links', () =>
const obj = spyOn(component, 'openModal');
let link = fixture.debugElement.query(By.css('#spec-tos'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(obj.calls.mostRecent().args[1]._def.references.termsOfServiceModalTemplate).toBeDefined();

link = fixture.debugElement.query(By.css('#spec-pp'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledTimes(2);
expect(obj.calls.mostRecent().args[1]._def.references.privacyPolicyModalTemplate).toBeDefined();
);
);



Enjoy!!






share|improve this answer

























  • Very cool, thank you! Too bad these things aren't strongly typed, but at least I can make them testable

    – CBarr
    Apr 1 at 13:16















3
















Sorry for the delay, been busy these days. You should modify your spec.ts as follows.



import TestBed, async, ComponentFixture from '@angular/core/testing';
import FooterComponent from './footer.component';
import BrowserModule, By from '@angular/platform-browser';
import BrowserAnimationsModule from '@angular/platform-browser/animations';
import MatButtonModule from '@angular/material';
import ModalModule from 'ngx-bootstrap';

describe('FooterComponent ', () =>
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [
FooterComponent
],
imports: [
// your imports here
ModalModule.forRoot()
]
).compileComponents();
));

beforeEach(() =>
fixture = TestBed.createComponent(FooterComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
);

it('should create the app', () =>
expect(component).toBeTruthy();
);


it('should call the openModal() method when clicking on the links', () =>
const obj = spyOn(component, 'openModal');
let link = fixture.debugElement.query(By.css('#spec-tos'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(obj.calls.mostRecent().args[1]._def.references.termsOfServiceModalTemplate).toBeDefined();

link = fixture.debugElement.query(By.css('#spec-pp'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledTimes(2);
expect(obj.calls.mostRecent().args[1]._def.references.privacyPolicyModalTemplate).toBeDefined();
);
);



Enjoy!!






share|improve this answer

























  • Very cool, thank you! Too bad these things aren't strongly typed, but at least I can make them testable

    – CBarr
    Apr 1 at 13:16













3














3










3









Sorry for the delay, been busy these days. You should modify your spec.ts as follows.



import TestBed, async, ComponentFixture from '@angular/core/testing';
import FooterComponent from './footer.component';
import BrowserModule, By from '@angular/platform-browser';
import BrowserAnimationsModule from '@angular/platform-browser/animations';
import MatButtonModule from '@angular/material';
import ModalModule from 'ngx-bootstrap';

describe('FooterComponent ', () =>
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [
FooterComponent
],
imports: [
// your imports here
ModalModule.forRoot()
]
).compileComponents();
));

beforeEach(() =>
fixture = TestBed.createComponent(FooterComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
);

it('should create the app', () =>
expect(component).toBeTruthy();
);


it('should call the openModal() method when clicking on the links', () =>
const obj = spyOn(component, 'openModal');
let link = fixture.debugElement.query(By.css('#spec-tos'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(obj.calls.mostRecent().args[1]._def.references.termsOfServiceModalTemplate).toBeDefined();

link = fixture.debugElement.query(By.css('#spec-pp'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledTimes(2);
expect(obj.calls.mostRecent().args[1]._def.references.privacyPolicyModalTemplate).toBeDefined();
);
);



Enjoy!!






share|improve this answer













Sorry for the delay, been busy these days. You should modify your spec.ts as follows.



import TestBed, async, ComponentFixture from '@angular/core/testing';
import FooterComponent from './footer.component';
import BrowserModule, By from '@angular/platform-browser';
import BrowserAnimationsModule from '@angular/platform-browser/animations';
import MatButtonModule from '@angular/material';
import ModalModule from 'ngx-bootstrap';

describe('FooterComponent ', () =>
let component: FooterComponent;
let fixture: ComponentFixture<FooterComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [
FooterComponent
],
imports: [
// your imports here
ModalModule.forRoot()
]
).compileComponents();
));

beforeEach(() =>
fixture = TestBed.createComponent(FooterComponent);
component = fixture.debugElement.componentInstance;
fixture.detectChanges();
);

it('should create the app', () =>
expect(component).toBeTruthy();
);


it('should call the openModal() method when clicking on the links', () =>
const obj = spyOn(component, 'openModal');
let link = fixture.debugElement.query(By.css('#spec-tos'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalled();
expect(obj.calls.mostRecent().args[1]._def.references.termsOfServiceModalTemplate).toBeDefined();

link = fixture.debugElement.query(By.css('#spec-pp'));
link.triggerEventHandler('click', null);
expect(component.openModal).toHaveBeenCalledTimes(2);
expect(obj.calls.mostRecent().args[1]._def.references.privacyPolicyModalTemplate).toBeDefined();
);
);



Enjoy!!







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 30 at 13:57









Anuradha MudaligeAnuradha Mudalige

4323 silver badges10 bronze badges




4323 silver badges10 bronze badges















  • Very cool, thank you! Too bad these things aren't strongly typed, but at least I can make them testable

    – CBarr
    Apr 1 at 13:16

















  • Very cool, thank you! Too bad these things aren't strongly typed, but at least I can make them testable

    – CBarr
    Apr 1 at 13:16
















Very cool, thank you! Too bad these things aren't strongly typed, but at least I can make them testable

– CBarr
Apr 1 at 13:16





Very cool, thank you! Too bad these things aren't strongly typed, but at least I can make them testable

– CBarr
Apr 1 at 13:16




















draft saved

draft discarded















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55405056%2fselecting-a-specific-templateref-from-angular-component-test%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript