Angular 如何在void函数中测试函数范围变量的赋值?

Angular 如何在void函数中测试函数范围变量的赋值?,angular,typescript,unit-testing,jestjs,jasmine,Angular,Typescript,Unit Testing,Jestjs,Jasmine,我有一个函数,由于代码覆盖率的原因,我需要对它进行测试 const downloadPdfDataContent=(标题:string,url:string):void=>{ const link=document.createElement('a'); link.target=标题; link.href=url; link.download=标题; document.body.appendChild(链接); link.click(); document.body.removeChild(li

我有一个函数,由于代码覆盖率的原因,我需要对它进行测试

const downloadPdfDataContent=(标题:string,url:string):void=>{
const link=document.createElement('a');
link.target=标题;
link.href=url;
link.download=标题;
document.body.appendChild(链接);
link.click();
document.body.removeChild(link);
revokeObjectURL(URL);
};
如何完成对变量和文档对象的分配测试?我想我需要用间谍。。但我不知道具体是如何实现的,因为变量只存在于函数范围内。我想简单地返回变量,但如果可能的话,我想阻止它。

一种方法是为实际的全局文档设置一个变量,作为测试的一部分

如果您在链接元素中引入了一些独特且可测试的内容(例如,唯一ID),那么您可以通过检查click event target是否为瞬态链接的ID来证明对其进行了单击

此外,您可以检查它的URL是否与正确的值对齐

假设这是一个单元测试,那么即使获得任何点击事件也可能是一个合法的测试


如果测试套件在浏览器上下文中运行,并且理想情况下您会阻止导航,那么您的事件处理程序可以使用来阻止任何导航的实际发生。

您可以使用
jest.spyOn()
来模拟每个DOM操作

例如

index.ts

export const downloadPdfDataContent = (title: string, url: string): void => {
  const link = document.createElement('a');
  link.target = title;
  link.href = url;
  link.download = title;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
};
import { downloadPdfDataContent } from '.';

describe('67634069', () => {
  afterEach(() => {
    jest.restoreAllMocks();
  });
  it('should pass', () => {
    const mAnchor = ({
      target: '',
      href: '',
      download: '',
      click: jest.fn(),
    } as unknown) as HTMLAnchorElement;
    const createElementSpy = jest.spyOn(document, 'createElement').mockReturnValueOnce(mAnchor);
    const appendChildSpy = jest.spyOn(document.body, 'appendChild').mockImplementation();
    const removeChildSpy = jest.spyOn(document.body, 'removeChild').mockImplementation();
    URL.revokeObjectURL = jest.fn();
    downloadPdfDataContent('teresa teng', 'example.com');
    expect(createElementSpy).toBeCalledWith('a');
    expect(appendChildSpy).toBeCalledWith(
      expect.objectContaining({
        target: 'teresa teng',
        href: 'example.com',
        download: 'teresa teng',
      })
    );
    expect(mAnchor.click).toBeCalledTimes(1);
    expect(removeChildSpy).toBeCalledWith(
      expect.objectContaining({
        target: 'teresa teng',
        href: 'example.com',
        download: 'teresa teng',
      })
    );
    expect(URL.revokeObjectURL).toBeCalledWith('example.com');
  });
});
index.test.ts

export const downloadPdfDataContent = (title: string, url: string): void => {
  const link = document.createElement('a');
  link.target = title;
  link.href = url;
  link.download = title;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
};
import { downloadPdfDataContent } from '.';

describe('67634069', () => {
  afterEach(() => {
    jest.restoreAllMocks();
  });
  it('should pass', () => {
    const mAnchor = ({
      target: '',
      href: '',
      download: '',
      click: jest.fn(),
    } as unknown) as HTMLAnchorElement;
    const createElementSpy = jest.spyOn(document, 'createElement').mockReturnValueOnce(mAnchor);
    const appendChildSpy = jest.spyOn(document.body, 'appendChild').mockImplementation();
    const removeChildSpy = jest.spyOn(document.body, 'removeChild').mockImplementation();
    URL.revokeObjectURL = jest.fn();
    downloadPdfDataContent('teresa teng', 'example.com');
    expect(createElementSpy).toBeCalledWith('a');
    expect(appendChildSpy).toBeCalledWith(
      expect.objectContaining({
        target: 'teresa teng',
        href: 'example.com',
        download: 'teresa teng',
      })
    );
    expect(mAnchor.click).toBeCalledTimes(1);
    expect(removeChildSpy).toBeCalledWith(
      expect.objectContaining({
        target: 'teresa teng',
        href: 'example.com',
        download: 'teresa teng',
      })
    );
    expect(URL.revokeObjectURL).toBeCalledWith('example.com');
  });
});
测试结果:

 PASS  examples/67634069/index.test.ts (8.691 s)
  67634069
    ✓ should pass (4 ms)

----------|---------|----------|---------|---------|-------------------
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:        9.612 s

fileURL
来自哪里?还有,你为什么尝试
appendChild
a
url
string?@slideshowp2谢谢,更新了问题!应该是
removeChild(link)
?是!第二次编辑。。。对不起,有点累了:)谢谢!遗憾的是,这对我来说是不可能的,因为我可能无法解析DOM中的元素,也无法注册任何额外的单击处理程序。我需要一种jest、jasmine和spyOn的方法:/如果可以通过模拟来监视对象,然后通过访问其属性进行验证,那将很有趣