在我的新 React Native 应用程序中,我想添加一些 Jest 测试。
一个组件渲染背景图像,该图像直接位于项目assets文件夹中。
现在我偶然发现了如何测试此图像是否实际上是从该路径中获取的,因此存在于组件中并正确呈现。
我尝试将toHaveStylefrom@testing-library/jest-native与容器一起使用,它返回的错误toHaveStyle不是函数。然后我尝试了queryByTestId同样的错误,同样的错误。当我这样做时, expect(getByTestId('background').toBeInTheDocument);我觉得这没用,因为它只检查是否存在具有此 testId 的元素,而不检查图像源。
请问这个怎么测试啊 毕竟,测试图像源真的有意义吗?
这是我的代码:
1.) 应测试的组件 ( Background):
const Background: React.FC<Props> = () => {
const image = require('../../../../assets/images/image.jpg');
return (
<View>
<ImageBackground testID="background" source={image} style={styles.image}></ImageBackground>
</View>
);
};
Run Code Online (Sandbox Code Playgroud)
2.) 测试:
import React from 'react';
import {render, container} from 'react-native-testing-library';
import {toHaveStyle} from '@testing-library/jest-native';
import '@testing-library/jest-native/extend-expect';
import Background from '../Background';
describe('Background', () => {
test('renders Background image', …Run Code Online (Sandbox Code Playgroud) 我正在开发我的第一个结合了Java和Javascript的应用程序.由于它越来越多,现在是时候实现一些适当的错误处理.但说实话,我没有经验.
我的应用程序只允许用户填充存储在数据库中的不同配方器.对于前端,我使用Angular.数据库是MySQL,使用Hibernate和Spring.
前端已经有一些检查,例如验证用户是否键入了一个不超过10年的日期.但我想到的是某种类型检查.例如,如果接收的值实际上是布尔值,如果不是,则抛出错误.
问题是:
这种类型检查应该在哪里执行?在将接收到的值添加到数据库之前不久,已经在Javascript的前端(想到"typeof")或者在后端?是否更好地使用Http状态代码或将错误写入日志文件?
也许您可以推荐一些适合您的最佳实践或选项.
非常感谢!
So I have a React Native application using TypeScript, with an error that's driving me crazy.
Basically, there is a Searchable List. It is initiated with an Array of values. Once the user types in a SearchBar, the initiated Array is filtered, returning an updated Array.
However, TypeScript gives me the error: Type '{ id: string; name: string; selected: boolean; }[]' provides no match for the signature '(prevState: undefined): undefined'.
I am confused because I don't know where this '(prevState: …
我正在将我们的项目从Java 8迁移到Java 9。
使用:IntelliJ IDEA 2017.2.5
Maven版本:3.5.1
到目前为止,我module-info.java已经为要迁移的所有模块创建了。在这里,requires与搭配使用效果很好java.base。但是所有其他外部模块/依赖项都没有,比如说log4j和项目中尚未迁移到Java 9的程序包。这些程序包也作为依赖项添加到POM中。但是,IntelliJ在这里不会引发错误。
从作品导航module-info.java到相应的JAR External Libraries。
但是,当我使用进行构建时clean install,编译失败:module not found: log4j。与所有其他外部软件包相同。
知道为什么会发生此错误吗?我认为Java 9会将非模块化的依赖关系转换为自动模块。我做错了什么或不了解正确的方法?
这是项目的POM:
<groupId>de.project</groupId>
<artifactId>basicProject</artifactId>
<version>1.0</version>
<name>Basic Project</name>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
<compilerVersion>3.7.0</compilerVersion>
</configuration>
<version>3.5.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>../output/intern/</outputDirectory>
<resources>
<resource>
<directory>intern</directory>
<includes>
<include>loggingConfig.xml</include>
</includes>
<!--<filtering>true</filtering>-->
</resource>
</resources>
</configuration> …Run Code Online (Sandbox Code Playgroud) 我有一个类加载器问题与Java 9。
这段代码可用于以前的Java版本:
private static void addNewURL(URL u) throws IOException {
final Class[] newParameters = new Class[]{URL.class};
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class newClass = URLClassLoader.class;
try {
Method method = newClass.getDeclaredMethod("addNewURL", newParameters );
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[]{u});
} catch (Throwable t) {
throw new IOException("Error, could not add URL to system classloader");
}
}
Run Code Online (Sandbox Code Playgroud)
从这个线程中,我了解到必须将其替换为以下内容:
Class.forName(classpath, true, loader);
loader = URLClassLoader.newInstance(
new URL[]{u},
MyClass.class.getClassLoader()
Run Code Online (Sandbox Code Playgroud)
MyClass是我要在其中实现该Class.forName()方法的类。
u = file:/C:/Users/SomeUser/Projects/MyTool/plugins/myNodes/myOwn-nodes-1.6.jar
String classpath = URLClassLoader.getSystemResource("plugins/myNodes/myOwn-nodes-1.6.jar").toString();
Run Code Online (Sandbox Code Playgroud)
由于某些原因-我真的不知道为什么-在运行时我收到ClassNotFoundException Class.forName(classpath, …
作为 React 的绝对新手,我想将数据从子组件传递到父组件。但是如果我寻找这个问题,我总能找到使用“状态”和“回调”函数的“旧”方式。例如,参见这篇文章:https : //medium.com/@ruthmpardee/passing-data-between-react-components-103ad82ebd17
由于所有指南都使用传统方式,我很困惑,因为它们看起来与我所知道的大不相同。例如,我没有在构造函数中使用“this.state”,而是使用 useState() 钩子。
有什么方法或者我看不到 Hook 可以将数据从子组件传递到父组件吗?
我有一个使用 Virtual Box 在 Mac 主机上运行的 Windows 10 Pro Guest。
对于 Windows 客户系统,我想激活 Windows 身份验证,因此我选择Turn Windows Features On/Off--> Internet Information Services--> World Wide Web Services-->Security并选中Windows Authentication。
现在系统已经卡了Searching for required files近半个小时了!我已经杀死并重新启动该进程两次,结果每次都相同。这很令人困惑,因为在另一台“真正的”Windows 10(第二台电脑)上,这就像一个魅力。
有人知道这里出了什么问题吗?我用谷歌搜索解决方案,但没有成功。只有像这样的关于 Net Framework 3.5 的论坛帖子存在此问题。
我完全迷失在这里了。请帮忙!
我正在将我的小型 React Native 项目从纯 Javascript 迁移到 Typescript。虽然到目前为止进展顺利,但我现在在使用 Redux 时遇到了问题。
我正在努力使用的减速器是radioButtonSelectedReducer. 在该应用程序中,有两个单选按钮。如果单击第一个,则给出字符串“itemOne”,如果单击第二个,则给出字符串“itemTwo”。因此,radioButtonSelected我想,必须是字符串类型。但它给出了错误:radioButtonSelected: string;-->
"The expected type comes from property 'radioButtonSelected' which is declared here on type 'ReducersMapObject<IApplicationState, AnyAction>'"
Run Code Online (Sandbox Code Playgroud)
我是 Typescript 新手,不知道如何处理此消息。
imports ...
export interface IApplicationState {
radioButtonSelected: string;
searchBarInput: string;
searchBarResult: string;
}
const rootReducer = combineReducers<IApplicationState>({
radioButtonSelected: radioButtonSelectedReducer,
searchBarInput: searchBarInputReducer,
searchBarResult: searchBarResultReducer,
});
export type RootState = ReturnType<typeof rootReducer>;
Run Code Online (Sandbox Code Playgroud)
当我离开combineReducers()并<IApplicationState>调用RootState时mapStateToProps(),它给出了错误:
error TS2339: Property 'itemSelected' does …Run Code Online (Sandbox Code Playgroud) 所以我有一个ScrollView包含许多元素的组件,所以你必须向下滚动很长一段路。
现在页面底部应该有一个按钮,单击该按钮会将页面滚动回顶部。
我已经在一个额外的组件中将按钮创建为 FAB(浮动操作按钮)。
它集成在父组件中,该组件ScrollView位于该父组件中。
我发现你必须ref在ScrollView组件中创建一个并在那里实现一个按钮,使用它ref来进行滚动工作。简化,这是我到目前为止所拥有的:
imports ...
const ParentComponent: React.FC<Props> = () => {
const scroll = React.createRef();
return (
<View>
<ScrollView ref={scroll}>
<SearchResult></SearchResult> // creates a very long list
<FloatingButton
onPress={() => scroll.current.scrollTo(0)}></FloatingButton>
</ScrollView>
</View>
);
};
export default ParentComponent;
Run Code Online (Sandbox Code Playgroud)
如您所见,有一个FloatingButton带有onPress()方法的组件。
这是实现:
import React, {useState} from 'react';
import {Container, Content, Button, Icon, Fab} from 'native-base';
const FloatingButton: React.FC<Props> = () => {
return ( …Run Code Online (Sandbox Code Playgroud) 所以我有一个 React Native 应用程序,它使用 Native Base 作为 UI 库和 Typescript。
现在有一个手风琴- 一旦展开 - 就会呈现第二个(嵌套的)手风琴。问题是 TypeScript 抱怨:
A VirtualizedList contains a cell which itself contains more than one VirtualizedList of the same orientation as the parent list. You must pass a unique listKey prop to each sibling list.
这完全没问题。但是当我将其添加listKey到我的时Accordion,TypeScript 抱怨No overload matches this call.
我怎样才能抑制这个警告?因为 Native Base 不提供listKey其Accordion.
这是代码:
imports ...
type Props = {};
const Test: React.FC<Props> = ({}) …Run Code Online (Sandbox Code Playgroud) react-native ×3
reactjs ×3
typescript ×3
java ×2
java-9 ×2
javascript ×2
native-base ×2
classloader ×1
jar ×1
jestjs ×1
maven ×1
maven-3 ×1
react-hooks ×1
react-native-testing-library ×1
react-redux ×1
redux ×1
virtualbox ×1
windows-10 ×1