小编Con*_*ine的帖子

如何使用 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
查看次数

标签 统计

api ×1

browser-api ×1

jestjs ×1

mocking ×1

notifications ×1