Saturday, December 9, 2017

How may I have .querySelector in a Jasmine unit test select the nth DOM element that matches and not merely the first match?

The answer is to use .querySelectorAll instead. Something like this...

expect(element.querySelector('li').textContent).toContain('foo');

 
 

...may become something like this...

expect(element.querySelectorAll('li')[0].textContent).toContain('foo');

 
 

...which then affords something different altogether like this...

expect(element.querySelectorAll('li')[1].textContent).toContain('bar');

No comments:

Post a Comment