0

I have this code and I want to check that whether each element has either "Company" or "Individual".

This code fails. If the element has only "Company", jasmine looks for "Individual" as well. If the element has one of the options it should pass but it doesn't

$$('div.modal').then(function (list) {
            list.forEach(function (elm) {
                expect(elm.getText()).toContain('Company' || 'Individual');
            })
        })
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
ByAS
  • 21
  • 5
  • if the element has "company" ,jasmine looks for "individual" as well eg Expected 'Former (Company) to contain 'Individual' – ByAS Dec 09 '21 at 14:04
  • I meant it fails – ByAS Dec 09 '21 at 14:06
  • 3
    `'Company' || 'Individual'` is simply `'Company'`; the way it is used is like a Coalescing Operator so the first not null value is 'company' – Firewizz Dec 09 '21 at 14:06
  • @freedomn-m fixed the description – ByAS Dec 09 '21 at 14:10
  • @Firewizz make it more clear please – ByAS Dec 09 '21 at 14:11
  • 2
    You could just use a [regex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) like `.toMatch(/Company|Individual/)` – Reyno Dec 09 '21 at 14:14
  • `.toContain(value)` where value = `'Company' || 'Individual'` that's not company or individual, it's company || individual. See [meaning of ||](https://stackoverflow.com/questions/30108466/what-is-the-meaning-of-in-javascript). *Unlike most languages, JS returns the left operand if it is truthy* - a string is truthy, so returns the first string. You need a different method, not .toContain() – freedomn-m Dec 09 '21 at 14:15
  • I mean `null || false || "" || "Valid"` just returns valid. Use Reyno's solution with regex to solve multiple cases – Firewizz Dec 09 '21 at 14:18
  • @Reyno 's solution didnt work. – ByAS Dec 09 '21 at 14:40
  • `["Individual", "Company", "Company", "Individual", "exclude"].flatMap(e=> e.match(/Company|Individual/g))` I tested it the plain javascript way but that should work. No mathes return null – Firewizz Dec 09 '21 at 15:45
  • for me `.toMatch(/Company || Individual/) ` worked – ByAS Dec 09 '21 at 16:10

0 Answers0