GVKun编程网logo

reactjs – jest没有实现window.alert()(react setstate不生效)

15

对于想了解reactjs–jest没有实现window.alert()的读者,本文将提供新的信息,我们将详细介绍reactsetstate不生效,并且为您提供关于ReactMaterial-UI菜单锚

对于想了解reactjs – jest没有实现window.alert()的读者,本文将提供新的信息,我们将详细介绍react setstate不生效,并且为您提供关于React Material-UI菜单锚被react-window列表破坏、react native navigation TypeError: null is not an object (evaluating '_RNGestureHandlerModule.default.handleSetJSResponder')、React.js:如何从window.open()到我的父组件使用Window.postMessage()、ReactJs Antd - window.open 动态 url的有价值信息。

本文目录一览:

reactjs – jest没有实现window.alert()(react setstate不生效)

reactjs – jest没有实现window.alert()(react setstate不生效)

我用开玩笑为我的api写了测试.我添加了在测试文件中调用我的api的函数,如下所示:

import AuthManager from "../Client/Modules/Auth/AuthManager";

并使用如下:

test("login api resolves true",() => {
  return expect(AuthManager.login("test","test")).resolves.toMatchObject(
    expect.objectContaining({
      accesstoken: expect.any(String),email: expect.any(String),expiresIn: expect.any(Number),refreshToken: expect.any(String),userFullName: expect.any(String),userId: expect.any(Number)
    })
  );
});

我的测试通过,但我有一个错误如下:

Error: Not implemented: window.alert

如何解决这个问题呢 ?

解决方法

Jest的 default test environment是jsdom提供的类似浏览器的环境.

jsdom实现了实际浏览器提供的大部分内容(包括全局窗口对象),但它并没有实现所有内容.

特别是对于这种情况,jsdom没有实现window.alert,而是在调用时抛出一个Error,如源代码here所示.

只要您知道为什么您的代码正在启动警报并且知道您的测试在Error之外正常工作,那么您可以通过为window.alert提供一个空实现来抑制错误:

test("login api resolves true",() => {
  const jsdomAlert = window.alert;  // remember the jsdom alert
  window.alert = () => {};  // provide an empty implementation for window.alert
  return expect(AuthManager.login("test",userId: expect.any(Number)
    })
  );  // SUCCESS
  window.alert = jsdomAlert;  // restore the jsdom alert
});

React Material-UI菜单锚被react-window列表破坏

React Material-UI菜单锚被react-window列表破坏

我在项目中使用Material-UI和react-window。我的问题是,当Material-UI菜单组件位于react-
window虚拟列表中时,material-
ui菜单组件不会锚定到该元素。菜单将显示在屏幕的左上角,而不是固定在打开菜单的按钮上。在非虚拟化列表中使用它们时,它会按预期工作。菜单正确锚定在打开它的按钮上。

这是一个示例沙箱。沙盒非常特定于我如何使用相关组件。

关于如何解决此问题的任何指导?

react native navigation TypeError: null is not an object (evaluating '_RNGestureHandlerModule.default.handleSetJSResponder')

如何解决react native navigation TypeError: null is not an object (evaluating ''_RNGestureHandlerModule.default.handleSetJSResponder'')?

我遵循 react-native 文档的步骤。

npx react-native init SelfApp --template react-native-template-typescript

然后按照react-native-navigation的步骤操作。

yarn add @react-navigation/native 
yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view

然后我添加代码

import ''react-native-gesture-handler'';

到 app.tsx 的第一行。

然后运行 ​​yarn start,我收到此错误。

error

我尝试了很多次,但还是有问题,这是我的 package.json

enter image description here

解决方法

嘿,根据 react-native-gesture-handler 的 GitHub 上的 this discussion,这要么是链接问题(如果您使用的是 Android),要么是某些 pod 未安装(如果您使用的是 iOS)的问题。

>

知道您使用的是哪个版本的 React Native 会很有帮助,因为如果 >= 60,它应该支持自动链接,如果它

在最后一种情况下: Try these steps

对于 iOS:

$ cd ios

$ pod install

根据我上面提到的讨论(参见 this specific response),另一种可能对其他人有效的解决方案:

1 - Delete/Remove your node_modules directory

rm -rf node_modules

2 - Install your dependencies again

如果您使用的是 Yarn $ yarn install 或者您使用的是 npm npm install

3 Clean your Gradle

cd android
./gradlew clean
cd ..
react-native run-android

React.js:如何从window.open()到我的父组件使用Window.postMessage()

React.js:如何从window.open()到我的父组件使用Window.postMessage()

您可以通过访问子窗口的postMessage属性来完成此操作。

var child = window.open('https://www.example.com')

child.postMessage('something')

JSFiddle-https://jsfiddle.net/8850s/ev1xj8uc/

ReactJs Antd - window.open 动态 url

ReactJs Antd - window.open 动态 url

如何解决ReactJs Antd - window.open 动态 url?

我使用下面的代码在弹出窗口中打开一个网址。

{
        title: ''Contract'',dataIndex: ''index'',key: ''contract'',render: (text,record,index) =>
            <button onClick={() => window.open({record.contract},"Popup","width=600,height=600")}>
              Open
            </button>
      },

问题是我无法调用对象值 {record.contract}。 它抛出 Unexpected token,expected "," 错误。

有人可以帮忙吗?

解决方法

当您的编程语法不正确时,通常会发生意外的令牌错误。

{record.contract} 不是有效的语法。

当传递给事件内部的方法时,应该直接使用变量名。 按钮的正确语法是:

<button onClick={() => window.open(record.contract,"Popup","width=600,height=600")}>
    Open
</button>

这将解决意外的令牌错误。

我们今天的关于reactjs – jest没有实现window.alert()react setstate不生效的分享已经告一段落,感谢您的关注,如果您想了解更多关于React Material-UI菜单锚被react-window列表破坏、react native navigation TypeError: null is not an object (evaluating '_RNGestureHandlerModule.default.handleSetJSResponder')、React.js:如何从window.open()到我的父组件使用Window.postMessage()、ReactJs Antd - window.open 动态 url的相关信息,请在本站查询。

本文标签: