34

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

tanguy_k
  • 11,307
  • 6
  • 54
  • 58
HVenom
  • 712
  • 1
  • 10
  • 25

5 Answers5

61

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) 
TachyonVortex
  • 8,242
  • 3
  • 48
  • 63
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
22

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]);
});
TachyonVortex
  • 8,242
  • 3
  • 48
  • 63
16

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$/);
Augusto Franzoia
  • 570
  • 3
  • 13
12

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)
jordrake
  • 500
  • 3
  • 9
  • 5
    The disadvantage of this approach is assertions without actual data that caused the problem, whilst [`toContain`](https://jestjs.io/docs/en/expect#tocontainitem) would highlight such data. – Shiraz Apr 10 '19 at 10:41
  • 2
    To expand on the above: `expect(value).toBe(true)` should never be used unless you're actually testing something that returns a boolean, because the [diagnostics](http://growing-object-oriented-software.com/figures/feedback-on-diagnostics.svg) of "expected false to be true" are pretty useless. – jonrsharpe Apr 27 '22 at 20:18
1

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: 'myemail@email.com',
}

expect(result).toMatchObject(exampleResponse)
Jonas Braga
  • 379
  • 4
  • 5