How do you select an option in an angular directive test?
var options = elem.find('#test-code-select option');
expect(angular.element(options[0]).text()).to.equal('234');
expect(angular.element(options[1]).text()).to.equal('236');
These work fine, but how do I manually force the selection of an option?
angular.element(options[1]).click(); //nope
angular.element(options[1]).trigger('click'); //nope
angular.element(options[1]).attr('selected', true); //nope
EDIT:
The directive template includes a select with an internal ng-model, I suspect this is the cause of the problem:
<select id='test-code-select' ng-options='code as code for code in codeList' ng-model='code'>
fixture is a wrapper for our component's environment so we can control things like change detection. To trigger change detection we call the function fixture.detectChanges() , now we can update our test spec to: Copy it('login button hidden when the user is authenticated', () => { expect(el. nativeElement. textContent.
What is meant by directives in Angular? Directives are classes that add new behavior or modify the existing behavior to the elements in the template. Basically directives are used to manipulate the DOM, for example adding/removing the element from DOM or changing the appearance of the DOM elements.
You can create a component fixture with TestBed. createComponent . Fixtures have access to a debugElement , which will give you access to the internals of the component fixture. Change detection isn't done automatically, so you'll call detectChanges on a fixture to tell Angular to run change detection.
When it comes to the Angular world, Jasmine is the recommended testing framework. Angular CLI comes by default with Jasmine and Karma as the test runner. It generates test files at component creation, collects unit tests, runs karma, and displays results on a web page.
This works for me:
var select = elem.find('#test-code-select');
select.val('236').change();
expect(scope.code).toEqual('236');
Note the call to change()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With