JavaScript failing in Jasmine test

I can't figure out why the Jasmine testing framework says my "childElement" elements are undefined when trying to read the classList property of those elements in div #2 - div# 5. Div #1 doesn't throw any errors, but the rest do. So I'm wondering if this can help someone to figure out what the problem is.

Here's the code for one of the tests that doesn't work:

describe('Div #2', function() {

  var tag = 'div';
  var element = document.getElementsByTagName(tag)[1];
  var childElement = element.getElementsByTagName(tag)[1];

  var utilityClass = 'align-self-center';
  var hasUtilityClass = null;

  it('should have the class "' + utilityClass + '".', function() {
    hasUtilityClass = childElement.classList.contains(utilityClass);
    expect(hasUtilityClass).toBe(true);
  });

});

You can see my full code here: http://codepen.io/jeppeschaumburg/pen/zNyLOK

Upvotes: 0

Views: 160

Answers (2)

Rikin
Rikin

Reputation: 5473

You may want to consider your targeting using getElementsByClassName to get all d-flex example-alignSelf class-named containers and then look for your test case to pass.

Upvotes: 0

xszaboj
xszaboj

Reputation: 1085

var element = document.getElementsByTagName(tag)[1]; is:

<div class="align-self-start">Flex item 1</div>

so there is no child element in it. And then it is undefined

Try to use chrome/firefox debug window to step through your code to see where the problem is.

Upvotes: 1

Related Questions