Fordio
Fordio

Reputation: 3820

Jasmine unit testing an element is not :hidden

I have an AngularJS directive that I am writing tests for. I have recreated the problem in this plunk: http://plnkr.co/edit/gHoGwI6TZhsgKVwIb1uI?p=preview

Can anyone tell me why the second test fails and how to fix it?

Code:

var app = angular.module("myApp", []);
app.directive("myDirective", function() {
  return {
    template: "<div>Hello this is my directive!</div>"
  }
  });

describe("Testing hidden", function() {
  var $compile, $scope, $document;

  beforeEach(inject(function(_$compile_, $rootScope, _$document_){
    $compile = _$compile_;
    $scope = $rootScope.$new();
    $document = _$document_;
  }));

  function getElement() {
    var element = angular.element("<div my-directive></div>");
    $compile(element)($scope);
    return element;
  }

  it("passes", function() {
    var element = getElement();
    expect(element.is(":hidden")).toBe(true); // Passes
  });

  it("fails", function() {
    var element = getElement();
    $document.body.append(element);
    element.show();
    expect(element.is(":hidden")).toBe(false); // Fails
  });

  });

Upvotes: 2

Views: 6079

Answers (1)

TestersGonnaTest
TestersGonnaTest

Reputation: 1027

You need to add your element to the DOM;

I've updated your code with a fast solution. Of course you can add it to the DOM in the beforeEach and remove it from the DOM in an afterEach.

http://plnkr.co/edit/wRLhak1rDqnqfHMbKoWs?p=preview

Upvotes: 5

Related Questions