Mahendran
Mahendran

Reputation: 21

Jasmine test help needed afterEach

I have a logic built using jQuery that uses two each loops. I'm not sure how to write jasmine test for it.

Here is my code/logic:

<div id="mj">
  <div class="commonclass">
    Item 1
  </div>
  <div class="commonclass">
    Item 2
  </div>
  <div class="commonclass">
    Item 3
  </div>
</div>

jQuery:

var filterArray = ['Item', 'Block', 'Item 3'];

$.each($('.commonclass'), function(i, v) {
  var matching = false;
  $.each(filterArray, function(j, w) {
    if ($(v).text().trim() == w)
      matching = true;
  });
  if (matching)
    $(v).remove();
});

Logic:

The text from the div elements should not match with the filterarray values. The element should be removed if it is matched.

This is my jasmine test code. (Added partial)

describe('Delete matching element', function() {
        beforeAll(function() {
            elementsEl = element(SelectorData.selectors.elementSelector);
            elementValueEl = element(SelectorData.selectors.elementTitleSelector);
            bodyDocumentEl = element(SelectorData.selectors.body);
            bodyDocumentValues = SelectorData.values;
        });

        afterEach( function() {

            if (elementValueEl.value == bodyDocumentValues.value) {
                document.body.removeChild(elementsEl);
            }

        });

        it('delete the element', function() {
            expect(elementsEl.isPresent()).toBe(false);

        });
    });

Let me know what am I doing wrong here. JSfiddle: https://jsfiddle.net/dmahendranme/g1L2Lzm1/

Upvotes: 2

Views: 248

Answers (1)

Anfath Hifans
Anfath Hifans

Reputation: 1598

try this,

$(function(){    
    var filterArray = ['Item', 'Block', 'Item 3'];
    $('.commonclass').each(function(k,v){        
        if($.inArray($.trim($(v).text()), filterArray) > 0){
            $(this).remove()
        }
    });
});

Upvotes: 0

Related Questions