简体   繁体   English

用React测试库测试文档对象

[英]Test with react testing library the document object

With react testing library I want to test a method that it does not return anything. 使用react testing library我想测试一个不返回任何东西的方法。 Basically it is used to download a chart (svg). 基本上,它用于下载图表(svg)。

export const downloadWithLink = (imgDataUrl, fileName) => {
  const link = document.createElement('a')
  link.href = imgDataUrl
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}

How it works, I create an a tag , I insert the data of the cart in the href , I append the link to the document, I click it so that the file is downloaded by the user, and then I remove the link. 它是如何工作的,创建a tag ,将购物车的数据插入href ,将链接附加到文档,单击它,以便用户下载文件,然后删除链接。

Using the React testing library, I am struggling to understand how to test it, since, in the end, the method does not return anything. 使用React测试库,我正在努力了解如何进行测试,因为最后该方法不会返回任何内容。 All the steps are in between. 所有步骤都在两者之间。

  describe('downloadWithLink', () => {
    it('creates and removes the download a tag', () => {
      const generateFileName = jest.fn()
      generateFileName.mockReturnValue('foo_file_name')
      expect(downloadWithLink('foo_img_data_url', 'foo_name'))
    })
  })

In other scenarios I have components, then I can use the render method, and get the getByTestId and then fire the event click, but that's not the case. 在其他情况下,我具有组件,然后可以使用render方法,并获取getByTestId ,然后触发事件单击,但事实并非如此。

Any ideas about how to tackle this test? 关于如何解决此测试的任何想法?

If you only want to write a unit test for downloadWithLink function, it's nothing related with reactjs , you can test it as a simple function only with jestjs . 如果您只想为downloadWithLink函数编写单元测试,则与reactjs ,您可以仅通过jestjs其作为简单函数进行jestjs

For example: 例如:

index.ts : index.ts

export const downloadWithLink = (imgDataUrl, fileName) => {
  const link = document.createElement('a');
  link.href = imgDataUrl;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};

Unit test: 单元测试:

index.spec.ts : index.spec.ts

import { downloadWithLink } from './';

describe('downloadWithLink', () => {
  it('t1', () => {
    const anchorMocked = { href: '', click: jest.fn() } as any;
    const createElementSpyOn = jest.spyOn(document, 'createElement').mockReturnValueOnce(anchorMocked);
    document.body.appendChild = jest.fn();
    document.body.removeChild = jest.fn();
    downloadWithLink('https://github.com/mrdulin', 'test name');
    expect(createElementSpyOn).toBeCalledWith('a');
    expect(document.body.appendChild).toBeCalledWith(anchorMocked);
    expect(anchorMocked.click).toBeCalledTimes(1);
    expect(document.body.removeChild).toBeCalledWith(anchorMocked);
  });
});

Unit test result with 100% coverage: 单元测试结果覆盖率100%:

 PASS  src/stackoverflow/57394312/index.spec.ts (6.666s)
  downloadWithLink
    ✓ t1 (13ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        7.595s

Here is the completed demo: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57394312 这是完整的演示: https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57394312

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM