gpt4 book ai didi

reactjs - 如何使用 Jest 来测试带有 localStorage 的 React 组件?

转载 作者:行者123 更新时间:2023-12-05 01:32:51 24 4
gpt4 key购买 nike

我有一个调用本地存储的组件,想用 jestJS 测试它。据我所知,jest 不支持调用 localStorage。

这是我需要测试的组件:

const NavBar: React.FC = () => {
const history = useHistory();

const handleCLick = () => {
localStorage.clear();
history.push('/login');
};

return (
<div>
<header>
<div className="banner">
<div className="container">
<img
className="icon "
alt="icon"
title="icon"
src={favicon57}
/>
<p>Official website of the Stuff</p>
</div>
</div>
<nav className="navbar navbar-expand-md navbar-dark fixed-top">
<div className="container">
<div className="navbar-header">
<img
className="logo "
alt="logo"
title="Logo"
src={Blah}
/>
</div>
<button
className="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarCollapse"
aria-controls="navbarCollapse"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span className="navbar-toggler-icon" />
</button>
<div className="collapse navbar-collapse" id="navbarCollapse">
<ul className="navbar-nav ml-auto">
{isTokenAdmin() ? (
<li className="nav-item">
<a id="nav-users" className="nav-link" href={ADMIN_URL}>
View Users
</a>
</li>
) : (
<div> </div>
)}
{isTokenActive() ? (
<li className="nav-item">
<a id="nav-log-out" className="nav-link" href={APP_URL}>
Locations
</a>
</li>
) : (
<div> </div>
)}
{isTokenActive() ? (
<li className="nav-item">
<a
id="nav-log-out"
className="nav-link"
href={LOGIN_URL}
onClick={() => {
handleCLick();
}}
>
Logout
</a>
</li>
) : (
<div> </div>
)}
</ul>
</div>
</div>
</nav>
</header>
</div>
);
};

export default NavBar;

如您所见,我正在根据存储在 localStorage 中的 token 呈现按钮。您将如何实现 100% 的测试覆盖率?

编辑:

获取token的函数代码为:

export const isTokenActive = (): boolean => {
const userToken: string | null = localStorage.getItem('exp');
if (typeof userToken === 'string') {
return new Date().getTime() < Number.parseInt(userToken, 10);
}
return false;
};

export const isTokenAdmin = (): boolean => {
const userToken: string | null = localStorage.getItem('access_token');
if (typeof userToken === 'string') {
const decodedToken: TokenDetails = jwt_decode(userToken);
return decodedToken.authorities[0] === 'ROLE_Administrator';
}
return false;
};

最佳答案

你说 Jest 不支持调用 localStorage 是正确的。它不是浏览器,也没有实现 localStorage。

解决方案是模拟您自己的假本地存储支持,如下所示:

浏览器模拟.js

const localStorageMock = (function() {
let store = {}

return {
getItem: function(key) {
return store[key] || null
},
setItem: function(key, value) {
store[key] = value.toString()
},
removeItem: function(key) {
delete store[key]
},
clear: function() {
store = {}
}
}
})()

Object.defineProperty(window, 'localStorage', {
value: localStorageMock
})

Jest 配置(可以在你的 package.json 中)

  "jest": {
"setupFiles": [
"<rootDir>/__jest__/browserMocks.js",

然后查看是否调用了 localstorage,你可以像这样监视它:

describe('signOutUser', () => {
it('should sign out a user', async () => {
const spyLoStoRemove = jest.spyOn(localStorage, 'removeItem')

await signOutUser()

expect(spyLoStoRemove).toHaveBeenCalled()
expect(spyLoStoRemove).toHaveBeenCalledTimes(2)
})
})

关于reactjs - 如何使用 Jest 来测试带有 localStorage 的 React 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65282181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com