Jasmine tests check if html contains text and return boolean

JavascriptJqueryJasmineKarma Jasmine

Javascript Problem Overview


expect(view.$el.html()).toContain('Admin');

The view does contain the word 'Admin' so I was expecting it to return true. How can I achieve this?

expect(view.$el.html()).toContain('Admin');

This returns undefined. How can I make it return true?

<header class="main">
  <div id="heading">
    <ul class="header-view right">
      <li class="inline"><a href="#link" class="button help-btn tab-btn"><span>  </span>Help</a></li>
      <li class="inline last"><a href="#og" class="button admin-btn tab-btn expand-admin"><span></span>Admin</a></li>
    </ul>
  </div>
</header>

This is what is returned from view.$el.html

Please help.

Javascript Solutions


Solution 1 - Javascript

toContain() now can actually be used for substring in string checks:

expect(view.$el.html()).toContain('Admin');

Solution 2 - Javascript

From the Jasmine docs:

> The 'toContain' matcher is for finding an item in an Array

You are trying to find a string inside a string so I would do something like this:

expect(view.$el.html().indexOf('Admin') !== -1).toBe(true);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSpdexterView Question on Stackoverflow
Solution 1 - JavascriptalecxeView Answer on Stackoverflow
Solution 2 - JavascriptLee BaileyView Answer on Stackoverflow