cypress-file-upload 通过在 dropzone 组件内部使用按钮测试上传功能

huangapple go评论70阅读模式
英文:

cypress-file-upload test the upload functionality with a button inside dropzone component

问题

我使用 react-dropzone 库来处理上传图像文件。现在我想测试这个上传行为。测试是使用 Cypress 编写的,我正在使用 cypress-file-upload。这是我的组件

我尝试遵循这个例子 (https://gist.github.com/ZwaarContrast/00101934954980bcaa4ae70ac9930c60),但没有使其工作。

    <ReactDropzone
      onDrop={onDrop}
      multiple={false}
      noDrag
      data-cy="uploadFile"
    >
      <button className={styles.avatarButton}>
        Upload Profile Picture
      </button>
    </ReactDropzone>
function onDrop(event) {
  const file = event[0];
  //openModal;
}

在我的测试中,我试图测试模态框是否打开,但在我的情况下模态框并没有打开。

cy.fixture('avatar.png', 'base64').then(fileContent => {
  cy.get('[data-cy="uploadFile"]').attachFile({
     fileContent: fileContent,
     fileName: 'avatar.png',
  });
});   
cy.get('[data-cy="uploadFile"]')
  .find('input').trigger('change', {force:true})  

我不确定我在这里做错了什么。我在文件上传时看不到任何模态框。任何帮助将不胜感激。

英文:

I am using react-dropzone library to handle uploading image files. Now I would like to test this upload behaviour. The tests are written using cypress and I am using cypress-file-upload. Here's my component

I tried to follow this example (https://gist.github.com/ZwaarContrast/00101934954980bcaa4ae70ac9930c60) as well, but didn't get it to work .

    &lt;ReactDropzone
      onDrop={onDrop}
      multiple={false}
      noDrag
      data-cy=&quot;uploadFile&quot;
    &gt;
      &lt;button className={styles.avatarButton}&gt;
        Upload Profile Picture
      &lt;/button&gt;
    &lt;/ReactDropzone&gt;
function onDrop(event) {
  const file = event[0];
  //openModal;
}

In my test I'm trying to test if the modal opens up, but the modal doesn't open up in my case.

  cy.fixture(&#39;avatar.png&#39;, &#39;base64&#39;).then(fileContent =&gt; {
  cy.get(&#39;[data-cy=&quot;uploadFile&quot;]&#39;).attachFile({
     fileContent: fileContent,
     fileName: &#39;avatar.png&#39;,
    });
  });   
  cy.get(&#39;[data-cy=&quot;uploadFile&quot;]&#39;)
  .find(&#39;input&#39;).trigger(&#39;change&#39;, {force:true})  
  });

I'm not sure what I'm doing wrong here. I don't see any modal on file upload. Any help would be appreciated.

答案1

得分: 2

请参见Cypress组件测试无法渲染React组件

> 你不能直接向React组件添加data-id属性并使其出现在DOM内容中。

所以 &lt;ReactDropzone ... data-cy=&quot;uploadFile&quot;&gt; 不起作用。

尝试将 data-cy=&quot;uploadFile&quot; 添加到内部元素(在你的片段中是 &lt;button&gt;

我不确定你的确切React代码是什么,因为你展示的部分无法运行,但要获取 &lt;input&gt; 元素,你可以使用Cypress .closest()查询来查找相关联的输入。

cy.fixture(&#39;avatar.png&#39;, &#39;base64&#39;).then(fileContent =&gt; {
  cy.get(&#39;[data-cy=&quot;uploadFile&quot;]&#39;)
   .closest(&#39;input&#39;)                // &lt;button&gt;最近的输入
   .attachFile({
      fileContent: fileContent,
      fileName: &#39;avatar.png&#39;,
    });
  });   
  cy.get(&#39;[data-cy=&quot;uploadFile&quot;]&#39;)
    .find(&#39;input&#39;).trigger(&#39;change&#39;, {force:true})  
});
英文:

Please see Cypress Component Test does not render react Component

> You can't add data-id attributes directly to the the React component and have it appear on the DOM content.

so &lt;ReactDropzone ... data-cy=&quot;uploadFile&quot;&gt; does not work.

Try instead adding data-cy=&quot;uploadFile&quot; to the inner element (in your snippet it's &lt;button&gt;)

I'm not sure of the exact React code you have, since the piece you show does not run, but to get the &lt;input&gt; element you can use Cypress .closest() query to find the associated input.

cy.fixture(&#39;avatar.png&#39;, &#39;base64&#39;).then(fileContent =&gt; {
  cy.get(&#39;[data-cy=&quot;uploadFile&quot;]&#39;)
   .closest(&#39;input&#39;)                // nearest input to &lt;button&gt;
   .attachFile({
      fileContent: fileContent,
      fileName: &#39;avatar.png&#39;,
    });
  });   
  cy.get(&#39;[data-cy=&quot;uploadFile&quot;]&#39;)
    .find(&#39;input&#39;).trigger(&#39;change&#39;, {force:true})  
});


</details>



# 答案2
**得分**: 0

When using `.attachFile` with a drag'n'drop component, you have to specify it as the `subjectType`.
[From the docs:][1] 
```js
cy.get('[data-cy="dropzone"]')
  .attachFile('myfixture.json', { subjectType: 'drag-n-drop' });

In your specific case, it would just be adding the additional options object.

cy.get('[data-cy="uploadFile"]').attachFile({
     fileContent: fileContent,
     fileName: 'avatar.png',
    }, { 
     subjectType: 'drag-n-drop' 
    });
  });  
英文:

When using .attachFile with a drag'n'drop component, you have to specify it as the subjectType.
From the docs:

cy.get(&#39;[data-cy=&quot;dropzone&quot;]&#39;)
  .attachFile(&#39;myfixture.json&#39;, { subjectType: &#39;drag-n-drop&#39; });

In your specific case, it would just be adding the additional options object.

cy.get(&#39;[data-cy=&quot;uploadFile&quot;]&#39;).attachFile({
     fileContent: fileContent,
     fileName: &#39;avatar.png&#39;,
    }, { 
     subjectType: &#39;drag-n-drop&#39; 
    });
  });  

huangapple
  • 本文由 发表于 2023年3月9日 22:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定