不从函数调用getlocalstorage [英] Not calling getlocalstorage from the function

查看:98
本文介绍了不从函数调用getlocalstorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在尝试测试本地存储功能,但我不知道为什么不调用该函数或为什么它返回null.

Hello I'm trying to test my local storage functions but I don't know why is not calling the function or why it returns null.

代码

export const getRange = (type = '') => {
    switch (type) {
    case RANGE_SELECTION_TYPE.OPTIDRIVE:
        return getLocalStorageItem(LOCAL_STORAGE_KEYS.OPTIDRIVERANGE) || DATE_OPTIONS.LAST_30_DAYS
    case RANGE_SELECTION_TYPE.LOGBOOK:
        return getLocalStorageItem(LOCAL_STORAGE_KEYS.LOGBOOKRANGE) || DATE_OPTIONS.CURRENT_YEAR
    default:
        return DATE_OPTIONS.LAST_30_DAYS
    }
}

测试

test('should return the current value from the local storage from the opridrive selection', () => {
    setLocalStorageItem(type, DATE_OPTIONS.LAST_YEAR)
    expect(getRange(type)).toBe(DATE_OPTIONS.LAST_YEAR)
    removeLocalStorageItem(type)
})

因此,该函数返回本地存储值(如果有的话),如果没有默认值,但是在不需要任何建议时会得到默认值?

So the function return the local storage value if it has but if not the default value, but it gets the default value when it shouldn't any suggestions?

P.S该程序有效!

有什么建议吗?

推荐答案

您的type设置不正确,并且在获取LocalStorage时,找不到本地存储变量的正确名称.我建议通过打开开发工具进行调试.如果您使用的是Chrome:

Your type is not being set correctly and when you getLocalStorage, it's not finding the right name of the local storage variable. I'd suggest debugging it by opening up Dev Tools. If you're using Chrome:

  1. Ctrl + Shift + i(PC)或Cmd + Shift + i(Mac)
  2. 在开发工具"顶部栏上,导航到应用程序"
  3. 在左侧栏中,展开本地存储",然后单击有问题的端点(通常是localhost)
  4. 查看您的本地存储变量键是什么,以及是否正确设置了.

这篇关于不从函数调用getlocalstorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆