- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我正在编写 Jest/测试库测试。
假设我们有一个名为 BenchmarksPage
的组件。
请看它的返回语句的第一行。
import {
Box,
capitalize,
Container,
FormControl,
InputLabel,
MenuItem,
Select,
} from '@material-ui/core';
import { NextPage } from 'next';
import React, { useCallback, useEffect, useState } from 'react';
import { Helmet } from 'react-helmet-async';
import RequireScope from 'src/components/authentication/RequireScope';
import BenchmarkTable from 'src/components/benchmark/BenchmarkTable';
import DashboardLayout from 'src/components/dashboard/DashboardLayout';
import Heading from 'src/components/Heading';
import useSettings from 'src/hooks/useSettings';
import gtm from 'src/lib/gtm';
import { useDispatch, useSelector } from 'src/store';
import { getAllRollingTwelveCalcs } from 'src/store/rolling-twelve/rolling-twelve.thunk';
import { Timeframe, timeframeMap } from 'src/types/benchmark';
const BenchmarksPage: NextPage = () => {
const { settings } = useSettings();
const dispatch = useDispatch();
const [selectedTimeframe, setSelectedTimeframe] = useState<Timeframe>(
Timeframe.Monthly,
);
const company = useSelector((state) => state.company.current);
useEffect(() => {
gtm.push({ event: 'page_view' });
}, []);
useEffect(() => {
dispatch(getAllRollingTwelveCalcs());
}, [company]);
const handleTimeframeChange = useCallback(
(
event: React.ChangeEvent<{
name?: string;
value: Timeframe;
event: Event | React.SyntheticEvent<Element, Event>;
}>,
) => {
setSelectedTimeframe(event.target.value);
},
[],
);
return (
<RequireScope scopes={['query:benchmark-calcs']}>
<DashboardLayout>
<Helmet>
<title>Benchmarks</title>
</Helmet>
<Container maxWidth={settings.compact ? 'xl' : false}>
<Box
sx={{
display: 'flex',
alignItems: 'flex-end',
justifyContent: 'space-between',
mb: 4,
}}
>
<Heading>Benchmarks</Heading>
<FormControl sx={{ width: 300 }}>
<InputLabel>Timeframe</InputLabel>
<Select
sx={{ background: '#ffffff', maxWidth: 400 }}
value={selectedTimeframe}
label="Timeframe"
onChange={handleTimeframeChange}
>
{[...timeframeMap.keys()].map((timeframe) => (
<MenuItem key={timeframe} value={timeframe}>
{capitalize(timeframe)}
</MenuItem>
))}
</Select>
</FormControl>
</Box>
<BenchmarkTable timeframe={selectedTimeframe} />
</Container>
</DashboardLayout>
</RequireScope>
);
};
export default BenchmarksPage;
注意它的返回值是用RequireScope
包裹的
RequireScope
只会在用户通过身份验证时呈现其子项
RequireScope
:
import React, { useEffect, useState } from 'react';
import useAuth from 'src/hooks/useAuth';
export interface RequireScopeProps {
scopes: string[];
}
const RequireScope: React.FC<RequireScopeProps> = React.memo((props) => {
const { children, scopes } = props;
const { isInitialized, isAuthenticated, permissions } = useAuth();
const [isPermitted, setIsPermitted] = useState(false);
useEffect(() => {
if (process.env.NEXT_PUBLIC_IS_LOCAL) {
setIsPermitted(true);
}
}, []);
useEffect(() => {
if (isAuthenticated && isInitialized) {
(async () => {
const hasPermissions = scopes
.map((s) => {
return permissions.includes(s);
})
.filter(Boolean);
if (hasPermissions.length === scopes.length) {
setIsPermitted(true);
}
})();
}
}, [isAuthenticated, isInitialized, scopes, permissions]);
if (isPermitted) {
return <>{children}</>;
}
return null;
});
export default RequireScope;
我们只需要将isPermitted
设置为true
(顺便说一句,useAuth 使用 JWT 登录用户)
现在,当我使用测试库的 render
方法呈现 BenchmarksPage
时,
它不会在 jsdom 上呈现,因为
'isPermitted'
在 RequireScope
中仍然是 false。
所以,为了让isPermitted = true
我已经设置了
NEXT_PUBLIC_IS_LOCAL=true
在.env.development
根据 RequireScope 中的第一个 useEffect
,isPermitted
现在应该是 true
。
然而,组件仍然没有渲染,返回
<body></div></body>
这仍然意味着 isPermitted 为假。
我尝试了什么:
我也尝试过使用 .env.local
我可以向您保证我的设置都是正确的(jest.config,使用 MockProvider 包装 BenchmarksPage 等)
为什么 .env.development 不工作,并使 isPermitted = true
?
根据我的理解,一切在逻辑上都是正确的。
编辑:我试过写
NEXT_PUBLIC_IS_LOCAL=true
也在 .env.test 文件中
编辑 - 回答:
必须设置
import { loadEnvConfig } from '@next/env';
export default async (): Promise<void> => {
loadEnvConfig(process.env.PWD);
};
在 setupEnv.ts 文件中。
然后在 package.json 中添加 globalSetup(因为我不使用 jest.config.js -> eslint 与 tsconfig 一起搞乱了它)
"jest": {
"moduleNameMapper": {
"^src/(.*)$": "<rootDir>/src/$1"
},
"testEnvironment": "jsdom",
"globalSetup": "<rootDir>/test/setupEnv.ts"
},
我在 A 和 B 两个团队工作。因此我需要创建和安装两个开发证书。当我在 Xcode 的“build设置”中将“代码签名身份”设置为一般值“iOS Developer”时,我两个团队的应用程序正在构建
我刚刚将 XCode 升级到 4.3.1。我正在使用脚本来构建(然后通过 Testflight 部署)我的应用程序。但我现在收到此错误: Error: No developer directory f
我找不到在 Firefox Developer Edition 56.0b4 上的所有 .js 和 .css 文件中搜索字符串的方法。 我找到了办法,但那是在52版本之前。我想知道为什么他们删除了这个
我制作了一个 Gatsby starter,两个不同的人向我报告说他们无法运行它,因为它在构建“开发包”时卡住了。开发和生产构建都不起作用。我无法在我自己的机器上重现这个问题,但我想为想要使用我的 s
我正在尝试创建一个过去 3 周的苹果开发者帐户。选择后 “选择您的实体类型 = 个人”和“条款和条件 = 同意” 出现错误提示“您的注册无法完成”。我给支持打了几次电话,发了几次邮件,他们都只是简单地
我会使用robovm填写我正在使用Android libgdx进行研究的应用程序,但我无法识别证书。 xcode首选项中的证书已正确下载,然后通常可以正常使用....我做了什么,因为它已过期,然后我使
如何禁用 同源政策在 Firefox 开发者版中。出于某种原因,我认为在 Developer Edition 中执行此操作应该很容易,但我找不到设置。 最佳答案 老实说,您不应该在任何浏览器中执行此操
我有一个提交列表(最新的在前): abcd4 message abcd3 wrong commit message2 abcd2 wrong commit message1 abcd1 message
假设当前分支是 MyFeatureX。并且本地 develop 分支是最新的。下面的两个陈述是等价的吗?推荐的语法是什么? git rebase origin/develop git rebase d
我刚刚 merge 了我的整个 develop history历史进入我的主分支。正如我所见,应该连接 network graph 中的两个分支,它没有。另外,我的主分支显示:This branch
嗨,我有一个基于主分支的开发分支。 我在开发分支中进行了更改。 现在,我的问题是如果要更新 master 分支,如何在不丢失我在 develop 分支中的更改的情况下升级基于 master 分支的 d
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
与 firebug 相比,我更喜欢 firefox 开发人员检查器。 但是可以在原生 JS 控制台中显示 PHP 日志吗? 谢谢 :) 最佳答案 我刚刚安装了 Chrome Logger ( gith
今天我注意到我的 Mac 上有最新版本的 Xcode,一个在/Developer/Applications 中,另一个在/Applications 中。 /Applications 中的那个是 App
这很奇怪。显然,针对我的本地 develop 分支和远程 develop 分支进行 rebase 之间存在一些差异。以下是一些详细信息: 假设我有一个特性分支,feat,我不时对其进行 rebase:
这个问题在这里已经有了答案: difference between origin/branch_name and branch_name? (4 个答案) 关闭 5 年前。 我是新手,开始在项目中使
已经有很多关于此的问题回答了我的一些问题。我正在寻找在设置和管理这两个帐户方面有直接经验的人。 我有一种情况需要向超过 100 人发送私有(private) Beta 测试(iOS 的临时设备限制),
我想了解如何获取开发人员ID应用程序或开发人员ID安装程序证书的私钥? 开发人员ID证书只能由帐户持有者 https://developer.apple.com/support/roles/创建-完成
Mac Developer中的3rd Party Mac Developer Application和Code Signing Identity配置文件有什么区别?哪个是Mac App Store发行
我正在尝试使用 OminChannel 方法将 Google Pay 集成到我的网站中。官方文档提到 要打开 Google API 库,请从导航菜单中选择 API 和服务 > 库。搜索“NBU 付款
我是一名优秀的程序员,十分优秀!