2

I have a <div> element that has a (click) event attached to it which toggles some class variable, like the following:

<div (click)="some-variable-name = !some-variable-name"></div>

For testing the click event first I have to grab the div event by its CSS class, something like this:

let some-var = fixture.debugElement.query(By.css('.css-class-name'));

So is there a way by which I can spy on the class variable some-variable-name and check if it changes on the click event while testing, with something like this:

let switch-some-variable-name = component.some-variable-name;
some-var.nativeElement.click();
fixture.whenStable().then(() => {
expect(component.some-variable-name).toEqual(!switch-some-variable-name);
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Aiguo
  • 3,416
  • 7
  • 27
  • 52
  • Aside from `.not.toEqual(...)`, what's wrong with what you have? Spies are for things that you call. – jonrsharpe Oct 20 '16 at 21:11
  • right, I figured that! So if I can't test it with spy, then how can I test if its getting toggled on the click event? – Aiguo Oct 20 '16 at 21:15
  • 1
    Exactly how you suggest in the question - store the before value, then assert that the after value is different. – jonrsharpe Oct 20 '16 at 21:16
  • I get it now! I don't need to spy on a class variable, I can just compare the variable changes before and after the click events. Thanks! – Aiguo Oct 20 '16 at 21:26

0 Answers0