mercredi 29 juillet 2020

How to simply unit test function that returns array of li element in Jest/React

I am trying to write a very simply unit test for a function that takes an array of objects like this:

const object = {
  id: 1,
  name: 'testy test
}

and will return an array of li elements like this:

const returnArray =[ <li id='1'>1-testy test</li> ]

my test is very simple

expect(mapFunction(object).toEqual(returnArray)

However I the results never match, the above version shows the return with line breaks in it so they do not match. I have tried using different matchers, like .toContain/.toStrictEqual/.toBe

And have tried to break down expected return array to two variables and test the return array contains them.

using equality matchers results in the returned having line breaks in it that do not match the expected.

Using .toContain results in a console log showing the array matching but the test fails.

I do not want to render the list yet and I know the function works correctly.

What is the correct way to test the return array will match the expected.

The function:

arrayOfObjects.map(object => return <li id={object.id}>{object.id}-{object.name}</li>)

Aucun commentaire:

Enregistrer un commentaire