Unit test modal


#1

I’m checking my coverage report and i’m getting some small percentages for not testing the following (opening and closing). I’m using jasmine so is it possible to write a test for this?

// Open the Review modal
$scope.openIt = function () {
    $scope.helloModal.show();
};

// Close the Share modal
$scope.closeIt = function () {
    $scope.helloModal.hide();
};

Thank you!


#2

Hey!

I’m too familiar with jasmine, but if you need some help with testing you can always check out the tests that we use.

https://github.com/driftyco/ionic/tree/master/test


#3

Hi!

My problem was that how can you test the template of your modal. I’ve checked the unit tests of the modal, and found that if you want to test the DOM, you have an interface for this. For example, you can test the classes of your element like this:

expect(yourModalInstance.modalEl.classList.contains('modal')).to.equal(true)

But what about other DOM tests? How can you reach the DOM of your modal? I’ve found that you can get the template through yourModalInstance.modalEl. But can I get the parsed template of the modal straight, without compiling it?

Fortunately you have a helper method, yourModalInstance.$el, which you can get the parsed dom through like this:

expect(yourModalInstance.$el.find('ion-header-bar')).to.have.class('secondary');

So the answer is yourModalInstance.$el, if you’d like to use jquery to test, and yourModalInstance.modalEl, if you want to test the raw template.