Michal Kurz
Michal Kurz

Reputation: 2095

Is there a `.should()` assertion that takes a `criteria` callback, and passes when `!!criteria(yield) === true `?

I use this code to check it .tml-tag's text content is a valid XML element

const isXmlTag = (tagStr: string) =>
  tagStr.startsWith('<') &&
  tagStr.endsWith('>') &&
  getCharacterUseCountInString(tagStr, '<') === 1 &&
  getCharacterUseCountInString(tagStr, '>') === 1

cy.get('.xml-tag')
  .invoke('text')
  .should(text => assert(isXmlTag(text)))

But it feels overly complicated - is there a way to use one of the predefined should assertions to evaluate if yield passes my callback criteria (isXmlTag)?

Something like:

cy.get('.xml-tag')
  .invoke('text')
  .should('satisfy', isXmlTag)

Upvotes: 0

Views: 199

Answers (1)

Nopal Sad
Nopal Sad

Reputation: 644

satisfy is a valid chai matcher that can take a function, so if you run

cy.get('.xml-tag')
  .invoke('text')
  .should('satisfy', isXmlTag)

you should pass the test.

The yielded subject text is automatically passed into your function.

Upvotes: 1

Related Questions