Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 920
HVenom Avatar asked Jul 25 '18 12:07

HVenom


2 Answers

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)  
like image 110
Shubham Khatri Avatar answered Sep 24 '22 21:09

Shubham Khatri


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]);
});
like image 26
TachyonVortex Avatar answered Sep 24 '22 21:09

TachyonVortex