我怎样才能获得在jest模拟函数中调用的参数?
我想检查对象传递作为参数.
我在使用 Generic Type 动态访问对象属性时遇到问题。这是代码:
import React, { useState } from 'react'
function useForm<FormValues>(initialValues: FormValues) {
const [formValues, setFormValues] = useState<FormValues>(initialValues)
function getValue(fieldName: string) {
return formValues[fieldName]
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
7: return formValues[fieldName] ^ 无法获取,
formValues[fieldName]因为FormValues[1] 中缺少声明预期键/值类型的索引签名。参考文献:4:const [formValues, setFormValues] = useState(initialValues) ^ [1]
谢谢您的帮助。
当我尝试使用 ECS 在我的服务中运行任务时,会显示一条错误消息
运行任务失败原因:["RESOURCE:PORTS"]
我该如何解决这个问题?
我尝试使用nock和其他libs(如sinonjs)模拟HTTP请求,但没有成功.
import nock from "nock"
const URL = "http://localhost:8080/"
const SIGN_IN_PATH = "/fake/users/sign_in.json"
export const signInRequest = (status, payload = {}) => {
return nock(URL).get(SIGN_IN_PATH).reply(status, payload)
}
Run Code Online (Sandbox Code Playgroud)
-
import { signInRequest } from "./../../utils/fakeRequests"
const doLogin = (browser) => {
return browser
.url("http://localhost:8080")
.waitForElementVisible('form', 1000)
.setValue('input[name=email]', 'foo@foo.com')
.setValue('input[name=password]', 'somepass')
.click('button[type=submit]')
.pause(500)
}
export default {
"Do login and shows error message": (browser) => {
signInRequest(403)
doLogin(browser)
.waitForElementVisible('.error', 1000)
.end()
}
}
Run Code Online (Sandbox Code Playgroud)
它可能用夜视仪模拟http请求?
通常,当我使用 React + Typescript 并且必须使用 refs 时,我通常使用此检查:
const ref = useRef<HTMLDivElement>(null)
...
if(ref && ref.current)
Run Code Online (Sandbox Code Playgroud)
但最近,我收到此错误:
Property 'current' does not exist on type '((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement>'.
Property 'current' does not exist on type '(instance: HTMLDivElement | null) => void'.ts(2339)
Run Code Online (Sandbox Code Playgroud)
关于这个错误意味着什么的任何想法?为了解决这个问题,作为一种解决方法,我添加了第三个检查:
if(ref && "current" in ref && ref.current)
Run Code Online (Sandbox Code Playgroud)
但这看起来很糟糕,主要是当您必须同时处理多个引用时。
谢谢你的帮助。
我正在使用这个组件:
<AceEditor
value={data}
className="code-editor"
mode="json"
theme="dracula"
onChange={setData}
name="data"
height="320px"
width="100%"
editorProps={{ $blockScrolling: true }}
showPrintMargin={false}
highlightActiveLine={false}
/>
Run Code Online (Sandbox Code Playgroud)
那么,使用react-testing-library如何获取这个组件并触发onChange事件?我试图找到一些像getByComponentName或类似的函数,但没有成功。
我正在使用 axios 拦截器在请求标头中设置授权令牌。
axios.interceptors.request.use(config => {
config.headers.Authorization = 'Bearer ' + Auth.getToken()
return config
})
Run Code Online (Sandbox Code Playgroud)
如何测试我的请求是否在标头中使用正确的令牌发送?
Ps.: 我用的是 Mocha + Chai
我尝试运行示例代码.
<form onSubmit={values => console.log("========>", values)}>
<div>
<label htmlFor="firstName">First Nameeee</label>
<Field name="firstName" component="Input" type="text"/>
</div>
<div>
<label htmlFor="lastName">Last Name</label>
<Field name="lastName" component="Input" type="text"/>
</div>
<div>
<label htmlFor="email">Email</label>
<Field name="email" component="Input" type="email"/>
</div>
<button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
但是当我处理onSubmit事件时,param值返回一个Proxy而不是一个带有输入值的对象.
//Console.log output
Proxy {dispatchConfig: Object, _targetInst: ReactDOMComponent, _dispatchInstances: ReactDOMComponent, nativeEvent: Event, type: "submit"…}
Run Code Online (Sandbox Code Playgroud) 我有一个函数,它应该返回 LoginResponse 类型或 null,但 Typescript lint 忽略了 null 的可能性,只是将 LoginResponse 显示为可能的值。
export const getSession = (): LoginResponse | null => {
return null
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用 undefined 没有成功。有什么想法吗?
我正在使用此代码。
date.toLocaleDateString('pt-BR')
Run Code Online (Sandbox Code Playgroud)
发生什么事了?
附注:我正在使用Jest和JSm进行测试。
javascript ×7
reactjs ×3
mocking ×2
typescript ×2
amazon-ecs ×1
aws-cognito ×1
axios ×1
flowtype ×1
jest ×1
jestjs ×1
jsdom ×1
nock ×1
node.js ×1
redux ×1
redux-form ×1
sinon ×1
testing ×1