{"id":85445,"date":"2020-04-05T03:07:13","date_gmt":"2020-04-04T19:07:13","guid":{"rendered":"http:\/\/4563.org\/?p=85445"},"modified":"2020-04-05T03:07:13","modified_gmt":"2020-04-04T19:07:13","slug":"react-testing-library-%e6%b5%8b%e8%af%95-multi-select-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=85445","title":{"rendered":"React testing library \u6d4b\u8bd5 multi-select \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  React testing library \u6d4b\u8bd5 multi-select \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hantsy <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>React testing library <code>handleChange.mockClear<\/code> \u6ca1\u6709\u91cd\u7f6e\u8c03\u7528\u6b21\u6570\u3002<\/p>\n<pre><code> expect(handleChange).toHaveBeenCalledTimes(2); <\/code><\/pre>\n<p>\u6e90\u6587\u4ef6\uff1a<\/p>\n<pre><code>it('handles changes in multi select box', () =&gt; {     const handleChange = jest.fn();      const {container, getByRole} = render(        &lt;select multiple role='select' onChange={handleChange}&gt;          &lt;option value=''&gt;---&lt;\/option&gt;          &lt;option value='one'&gt;one&lt;\/option&gt;          &lt;option value='two'&gt;two&lt;\/option&gt;          &lt;option value='three'&gt;threeone&lt;\/option&gt;       &lt;\/select&gt;      )     const elements = container.querySelectorAll('option');     const select = getByRole('select');     expect(elements.length).toEqual(4);     expect(handleChange).not.toHaveBeenCalled();     \/\/expect(elements[1].selected).toBe(true);      userEvent.selectOptions(select, ['two']);     expect(handleChange).toHaveBeenCalledTimes(1);     expect(elements[2].selected).toBe(true);      handleChange.mockClear();      userEvent.selectOptions(select, ['two', 'three']);     expect(handleChange).toHaveBeenCalledTimes(2);     expect(elements[2].selected).toBe(true);     expect(elements[3].selected).toBe(true);   }); <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1419919\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : hantsy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u539f\u6765 userEvent.selectOptions(select, [&#8216;two&#8217;, &#8216;three&#8217;]); \u8bbe\u7f6e selected values\uff0c\u4e24\u4e2a\u503c\u7684\u6570\u7ec4\u4f1a\u8c03\u7528\u4e24\u6b21 onChange \u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>React testing lib&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85445"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85445"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/85445\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85445"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}