javascript - Nested promise returns undefined -
i'm trying write tests cucumber.js using protractor , chai-as-promised.
in page object have these fragments of code:
var menusonlistelements = element.all(by.repeater('menu in menus').column('menu.name')) this.ismenulisted = function(menu) { return menusonlistelements.each(function(element) { return element.gettext().then(function (name) { if (menu.name === name) { return true; //this executed } }); }); };
and in step definition code do:
var menu = {}; menu.name = 'abc'; expect(new menuspage().ismenulisted(menu)).to.eventually.be.true.notify(done);
when run test get
expected undefined true
which means ismenulisted method returned undefined instead of true. however, debugged , can see 'return true;' statement executed.
am missing how promises work in case?
alternatively, can apply reduce()
here:
this.ismenulisted = function(menu) { return menusonlistelements.reduce(function(acc, element) { return element.gettext().then(function (name) { return acc || menu.name === name; }, false); }); };
the drawback here go through every single element in menusonlistelements
, don't stop if find matching menu. aside that, reduce()
resolve here in true
or false
defines if menu listed or not.
Comments
Post a Comment