标签: browser-api

如何使用 JEST 模拟通知 API?

我开玩笑地测试 redux-actions。特定的 redux-action 使用 Notifications API 作为副作用。我如何模拟通知 API?

现在,我只是这样嘲笑它:

global.Notification = {...};
Run Code Online (Sandbox Code Playgroud)

它有效,但我认为有更优雅的解决方案来解决这个问题。有任何想法吗?

我有这个模块来处理通知 API:

export const requestNotifyPermission = () => {
    try {
        return Notification.requestPermission().then(function(result) {
            return result;
        });
    } catch(err) {
        console.warn('NotificationsAPI error: ' + err);
    }
};

export const getCurrentNotifyPermission = () => {
    // Possible values = default, granted, denied
    try {
      return Notification.permission;
    } catch {
      return 'denied';
    }
};

export const createNotify = (title, body)  => {
  try {
    if (getCurrentNotifyPermission() === 'granted') { …
Run Code Online (Sandbox Code Playgroud)

api notifications mocking jestjs browser-api

5
推荐指数
1
解决办法
1432
查看次数

ScrollToOptions 的行为:Chrome 和 Edge v81+ 中的“平滑”被破坏

在 BrowserStack 中进行测试后,我得出的结论是,自版本 81 以来,使用scrollTo()选项参数behavior: smooth在 Chrome 和 Edge 中不起作用。Edge 和 Chrome 的版本 80 都按预期工作。根据MDN 的说法,它应该可以在没有星号的情况下工作。(与 Safari 不同)

在诸如此之类的流行答案中,behavior: smooth推荐使用“使用”来在 Web 应用程序中启用平滑滚动。

这是一个小的可复制的:

<html>
<button onclick="goToAnchor('b')">Scroll to B</button>
<div id="a" style="height: 1000px; background-color: blue;">Blue</div>
<div id="b" style="height: 1000px; background-color: red;">Red</div>
<div id="c" style="height: 1000px; background-color: green;">Green</div>

</html>

<script>
  function goToAnchor(anchor) {
    let rect = document.getElementById(anchor).getBoundingClientRect();
    window.scrollTo({
      left: rect.left + window.pageXOffset,
      top: rect.top + window.pageYOffset,
      behavior: 'smooth',
    });
  }
</script>
Run Code Online (Sandbox Code Playgroud)

预期的行为是浏览器窗口将视图平滑地插入红色 div。在我测试过的所有版本的 …

html javascript google-chrome microsoft-edge browser-api

3
推荐指数
1
解决办法
2168
查看次数