javascript - isElementInViewport 的 JavaScript 测试用例
问题描述
我是测试用例的新手,谁能帮我编写isElementInViewport
JavaScript 函数的测试用例
function isElementInViewport(ele) {
const rect = ele.getBoundingClientRect()
const InViewPort = (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= 0 &&
rect.right <= 0
)
return InViewPort
}
解决方案
这是解决方案:
index.ts
:
export function isElementInViewport(ele) {
const rect = ele.getBoundingClientRect();
const InViewPort =
rect.top >= 0 && rect.left >= 0 && rect.bottom <= 0 && rect.right <= 0;
return InViewPort;
}
index.spec.ts
:
import { isElementInViewport } from "./";
describe("isElementInViewport", () => {
it("t-1", () => {
const mEl = {
getBoundingClientRect: jest
.fn()
.mockReturnValueOnce({ top: 0, left: 0, bottom: 0, right: 0 })
};
const actual = isElementInViewport(mEl);
expect(actual).toBeTruthy();
expect(mEl.getBoundingClientRect).toBeCalledTimes(1);
});
});
覆盖率 100% 的单元测试结果:
PASS src/stackoverflow/59057693/index.spec.ts
isElementInViewport
✓ t-1 (5ms)
----------|----------|----------|----------|----------|-------------------|
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: 5.018s, estimated 12s
源代码:https ://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59057693
推荐阅读
- mysql - 带有变量的表中的值
- macos - 使用软件包制作安装程序 - 致用户/应用程序支持
- kubernetes - 如何在 K8s 上重新部署 Neo4j 数据库但不删除持久卷后恢复它?
- c++ - ESP32 HTTPS POST JSON 到 AWS
- javascript - 我们如何在服务器文件中获取在浏览器中输入的 url?
- flutter - 溢出时灵活滚动内容
- ansible - 为清晰起见,ansible adhoc 任务由主机单独输出
- eclipse - 为什么调试器会跳转到程序末尾?
- java - Java:使用 com.github.axet.VGet 下载 YouTube 视频时出错 (DownloadError)
- python-3.x - 如何在 arm64 主机上运行 amd64 docker 镜像