1

I want to test the existence of an input that is inside an ng-if, without the ng-if the test passes perfectly but not with the ng-if which is normal since ng-if removes the element from the DOM.

Here is the ng-if

<div ng-ig="$ctrl.model.joineryTypes">
  <input type="" name="">
</div>

The test

import angular from 'angular'
import 'angular-mocks'

let scope
let rootScope
let compile
let htmlElement
let ctrl

fdescribe('roomsCommonForm', () => {
  beforeEach(() => {
    angular.mock.module('roomsCommonModule')
  })

  beforeEach(inject((_$compile_, _$rootScope_) => {
    rootScope = _$rootScope_
    compile = _$compile_
    scope = rootScope.$new()
    htmlElement = compile(`<rooms-common-form></rooms-common-form>`)(scope)
    rootScope.$digest()
  }))

  beforeEach(inject(($componentController) => {
    let bindings = {
      model: {}
    }
    ctrl = $componentController('roomsCommonForm', null, bindings)
  }))

  it('should contain one input', () => {
    const inputItem = htmlElement.get(0).querySelectorAll('input')
    expect(inputItem.length).toBe(1)
  })
})

How I can simulate that object $ctrl.model.joineryTypes ?

1 Answer 1

1

You can change your test slightly; first alter the original binding to include the joineryTypes:

let bindings = {
  model: {
    joineryTypes: false
  }
};

So now it's false, you'd just need to alter it in another test:

it('should look inside the ng-if', () => {
  ctrl.model.joineryTypes = true;
});

Then you should be able to reach inside the ng-if block.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.