2
\$\begingroup\$

I've given an array of elements and need to check that every single text is a number greater than 30. Sample input data:

<ul class="data-list">
    <li class="data-item">40</li>
    <li class="data-item">31</li>
    <li class="data-item">32</li>
    <li class="data-item">101</li>
</ul>

Currently, I'm using .each() function to process every element one by one and then parseInt() and toBeGreaterThan() jasmine matcher:

var dataItems = element.all(by.css("ul.data-list > li.data-item"));
dataItems.each(function (elm) {
     elm.getText().then(function (text) {
         expect(parseInt(text)).toBeGreaterThan(30);
     });
});

Is there a simpler or better way to address the problem? Would it be appropriate to first map() into an array of integers and then apply the matcher to every element of an array?

What I am a bit concerned about in using the current approach is that it would fail at the very first element that is less than or equal to 30 preventing us to see what other values of an array are.

\$\endgroup\$
  • \$\begingroup\$ if you need every item t be >30 then why would you want to continue evaluating when an element is <=30? \$\endgroup\$ – I wrestled a bear once. Dec 25 '17 at 4:59
  • \$\begingroup\$ @Iwrestledabearonce. good question. Overall, that might help in terms of debugging and understanding if there are more than one potential failures. \$\endgroup\$ – alecxe Dec 25 '17 at 5:05
1
\$\begingroup\$

Would it be appropriate to first map()

I don't think so. This would only serve to make the computer iterate over each value twice. I don't see how that would make it simpler.

it would fail at the very first element that is less than or equal to 30 preventing us to see what other values of an array are.

If I understand correctly, each test can only return a single result, therefore if you want to evaluate all list items you would have to make a test for each one. You could also create a separate test for all of them.

Disclaimer: This code may or may not actually run, I've never used Jasmine, but it should hopefully at least get the point across

describe("All the stuff should be >30", function() {
    var allItemsSafe = true;

    var dataItems = element.all(by.css("ul.data-list > li.data-item"));
    dataItems.each(function (elm) {
        elm.getText().then(function (text) {

            var isSafe = parseInt(text) > 30;
            if(!isSafe) allItemsSafe = false;

            it(text+" should be greater than 30", function() {
                expect(isSafe).toBe(true);
            });

         });
    });

    it("ALL ITEMS should be greater than 30", function() {
        expect(allItemsSafe).toBe(true);
    });

});

Sidenote: I'm not sure if you really need parseInt, if the string is numeric it should be cast to a number implicitly.

\$\endgroup\$
1
\$\begingroup\$

Array::some can be used to reverse the test logic and return earlier.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.