javascript - Angular 1/Jasmine - Test element if exist inside ng-if -
i want test existence of input inside ng-if, without ng-if test passes not ng-if normal since ng-if removes element dom.
in template have :
<div ng-if="$ctrl.shouldshowair"> <input class="form-control" type="text"> </div>
in component
shouldshow() { this.shouldshowair = (this.parkingtype.labelkey === 'parking_type.air') }
unit test
import angular 'angular' import 'angular-mocks' let scope let rootscope let compile let htmlelement let ctrl fdescribe('projectexteriorparking', () => { beforeeach(() => { angular.mock.module('projectexteriorparkingmodule') angular.mock.module('ui.select') }) beforeeach(inject((_$compile_, _$rootscope_) => { rootscope = _$rootscope_ compile = _$compile_ scope = rootscope.$new() scope.parking = {} htmlelement = compile(`<project-exterior-parking parking="project.realestateprojectproduct.parkings"></project-exterior-parking>`)(scope) rootscope.$digest() })) beforeeach(inject(($componentcontroller) => { let bindings = { parking: {}, projectreferences: {} } ctrl = $componentcontroller('projectexteriorparkingmodule', null, bindings) })) it('should contain 2 input', () => { const inputitems = htmlelement.get(0).queryselectorall('input') expect(inputitems.length).tobe(2) }) })
how can simulate variable shouldshowair
true
you should write 2 tests , mock
this.parkingtype.labelkey
to false in first test , test input null. mock false , test input defined.
or write 1 test, mock labelkey true , run existing test 'should contain 2 input'
Comments
Post a Comment