'Jest matcher to match any one of three values

I have this selector in my component whose default state is '' (empty) string but when change event is fired user can select any one of the three values that is 6, 12 or 24

it("feedform testing the selector feed frequency for value of 6, 12, 24 ", () => {
  const addFeedForm = shallow(
    <FeedForm
      submitForm={() => {}}
      setFeedData={() => {}}
      formType="add"
      feedsubmit={{
        status: null,
        error: {
          formsubmitwarning: "",
          feedname: "",
          feedurl: "",
          feedposttype: "",
          feedfrequency: "",
          feedpost: "",
          feedhashtag: "",
          formloginid: ""
        }
      }}
    />
  );
  expect(addFeedForm.state().feedfrequency).toEqual("");
  addFeedForm.simulate("change");
  expect(addFeedForm.state().feedfrequency).toEqual(6 || 12 || 24);
});

Now while writing unit test cases for this I quickly went through Jest documentation to find matcher for any one of the three value but found no matcher that does that.

I even tried using || (or) operator in toEqual and toBe matcher but as you guessed it didn't work. Is there a way to make it work or should I skip the test all together?

Note: I am using Enzyme with Jest



Solution 1:[1]

In order to one among the expected value, you can reverse the comparison and test it using toContain method like

expect(addFeedForm.state().feedfrequency).toEqual('');
addFeedForm.simulate('change');
expect([6, 12, 24]).toContain(addFeedForm.state().feedfrequency) 

Solution 2:[2]

The jest-extended library provides a .toBeOneOf([members]) matcher:

it("feedform testing the selector feed frequency for value of 6, 12, 24 ", () => {
  expect(addFeedForm.state().feedfrequency).toBeOneOf([6, 12, 24]);
});

Solution 3:[3]

There is no method on the Jest API to match multiple values.

A way to do this check is using a regex:

expect(String(addFeedForm.state().feedfrequency)).toMatch(/^6|12|24$/);

Solution 4:[4]

Another way to achieve this is to do the comparison outside of Jest's assertion and simply expect that to be true:

expect(
    addFeedForm.state().feedfrequency === 6 ||
    addFeedForm.state().feedfrequency === 12 ||
    addFeedForm.state().feedfrequency === 24
).toBe(true)

Solution 5:[5]

We can create a regex of allowed values from an object

Here's an example using object matching:

const userStatusValues = Object.values(statusObject).join('|')
const possibleUserStatusRegex = new RegExp(`^${userStatusValues}$`)

const exampleResponse = {
      id: expect.any(Number),
      status: expect.stringMatching(possibleUserStatusRegex),
      email: '[email protected]',
}

expect(result).toMatchObject(exampleResponse)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TachyonVortex
Solution 2 TachyonVortex
Solution 3 Augusto Franzoia
Solution 4 jordrake
Solution 5 Jonas Braga