简体   繁体   English

如何使用 Jest 或其他测试库通过 JavaScript 测试对 DOM 的动态更改?

[英]How to test dynamic changes to the DOM via JavaScript with Jest or some other testing library?

I am dynamically adding a <script> tag to the document <head> on page load based on the environment.我根据环境在页面加载时向文档<head>动态添加<script>标记。

The Function:功能:

export const loadScript = () => {
  // load script tag into head
  const HEAD = document.getElementsByTagName('head')
  const SCRIPT_TAG = document.createElement('script')
  SCRIPT_TAG.setAttribute('src', process.env.SCRIPT_SRC)
  SCRIPT_TAG.setAttribute('async', true)
  HEAD[0].append(SCRIPT_TAG)
}

I want to write a test that checks if once the loadScript() function is run that the <script> tag made it into the head.我想编写一个测试来检查loadScript()函数是否在运行后<script>标记进入头部。 Our environment is set up with Jest, and I haven't found a satisfactory example that demonstrates how to do it, or works.我们的环境是用 Jest 设置的,我还没有找到一个令人满意的示例来演示如何做到这一点或如何工作。

I am new to testing, and would appreciate any solutions, or hints offered.我是测试新手,希望能提供任何解决方案或提示。

I suppose the easiest way to test it would be something like this: 我想最简单的测试方法是这样的:

test('loadScript', () => {
  process.env.SCRIPT_SRC = 'the-src';
  loadScript();
  expect(document.head.innerHTML).toBe('<script src="the-src" async="true"></script>');
});

This works because the default test environment for Jest is jsdom which simulates the document . 这是有效的,因为Jest的默认测试环境是模拟document jsdom

test('loadScript', () => {
  loadScript();
  const script = document.getElementsByTagName('script')[0];
  // trigger the callback
  script.onreadystatechange(); // or script.onLoad();
  expect("something which you have on load").toBe('expected result on load');
});

暂无
暂无

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

相关问题 如何使用 Jest 和 React 测试库测试 className - How to test a className with the Jest and React testing library Jest + React 测试库:如何从某个库中模拟出只影响一个测试块的方法 - Jest + React Testing Library: how do I mock out a method from some library that only affects one test block React 测试库 + Jest:如何测试接收数字然后对其进行格式化的组件 - React testing library + Jest: How to test a component that receives a number then format it 如何使用 onClick 函数 Jest/React 测试库测试 React 组件 - How to test react component with onClick function Jest/React testing library 如何使用 React 测试库和 jest 测试条件渲染的字符串 - How to test conditionally rendered string using React testing library and jest 如何使用 React 测试库和 jest 测试 redux state 更新 - How to test redux state update with react testing library and jest 是否可以使用Jest或任何其他测试库/框架加载URL并获取浏览器呈现的DOM对象? - Is it possible to load url and get the browser rendered DOM object using Jest or any other testing library/framework? 如何将@testing-library/jest-dom 添加到 svelte 中的每个测试文件中? - How to add @testing-library/jest-dom to every testing file in svelte? 在 Jest 和普通 JavaScript 中针对 DOM 进行测试 - Testing against a DOM in Jest and plain JavaScript Jest 和 React 测试库未在页面加载时加载最终 DOM - Jest and React Testing Library not loading the final DOM on page load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM