我试图用反应来渲染一个HOC,但我无法弄清楚如何让它起作用.所以我有一个HOC与反应导航完美配合.我的想法是展示渲染包装HOC的组件.我想这样做:
render() {
return (
<View style={viewStyle}>
{CheckLogin(Login)}
</View>
);
}
Run Code Online (Sandbox Code Playgroud)
这个CheckLogin是HOC,Login是组件本身.结果是React没有抱怨但是空白.知道如何渲染HOC调用Component本身吗?
我正在使用我在 apollo 中创建的缓存的一个组件,它存储在client
.
在某些时候,作为来自输入的回调,我执行以下调用:
const originalName = client.readQuery<NamesCached>({ query: NamesCached })!.names
Run Code Online (Sandbox Code Playgroud)
这直接返回一个对象数组。
但是,我不知道如何在我的组件中测试它。我的第一个假设是专门模拟client.readQuery
从我的__fixtures__
. 那没有用。
这是我嘲笑它的最佳镜头:
it('filters the name in the search', () => {
jest.doMock('@/ApolloClient', () => ({
readQuery: jest.fn()
}))
const client = require('@/ApolloClient')
client.readQuery.mockImplementation(()=> Promise.resolve(names()))
const { getByPlaceholderText, queryByText } = renderIndexNames()
const input = getByPlaceholderText('Search…')
fireEvent.change(input, { target: { value: 'Second Name' } })
expect(queryByText('Title for a name')).not.toBeInTheDocument()
})
Run Code Online (Sandbox Code Playgroud)
对于这个测试,我使用react-testing-library
. 这是我目前最好的一次投篮...
jest.doMock('@/ApolloClient', () => ({
readQuery: jest.fn()
}))
const …
Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我们的测试是使用 Angular 的 Harnesses 进行的,扩展ComponentHarness
类。我正在测试一个表,并希望根据行确定一些元素的范围。例如,我想从第一行提取按钮。我找不到一种方法让所有按钮的范围仅限于第一行。这是我到目前为止得到的
export class ProductComponentHarness extends ComponentHarness {
static readonly hostSelector = 'my-selector-component';
private readonly table = this.locatorFor(MatTableHarness);
async getRows(): Promise<MatRowHarness[]> {
const tableResolved = await this.table();
return tableResolved.getRows();
}
async getAllButtonsFromFirstRow(): Promise<void> {
const firstRow = (await this.getRows())[0];
// Stuck here, how to scope?
this.locatorForAll(firstReleaseRow, MattButtonHarness); // My logic pointed me here first time
this.locatorForAll(MatButtonHarness.with({ ancestor: firstRow })); //Not working
}
}
Run Code Online (Sandbox Code Playgroud)
也许方法不正确?我找不到更多关于具有 Material Angular 的嵌套线束的信息。最坏的情况我只是提取所有按钮并从那里进行检查,但我想学习如何正确确定范围,如果在任何时候我发现类似的情况
我有一个从 REST API 获取信息的对象。假设我从 API 调用 Posts,并且可以指定要提取的字段。仅举一个例子来说明它的外观:
Post.find!(id: 1, fields: [:id, :title, :description])
Run Code Online (Sandbox Code Playgroud)
这会执行 API 调用,并返回一个包含这些指定字段的 Post 对象。
为了进行测试,我使用 Factory Bot 对该 API 调用进行存根,并直接返回一个 Post 对象,其中包含它可以查询的所有可能字段。
这种方法不是最好的,因为测试总是返回所有字段和代码本身,也许我只需要几个字段而不是全部字段
所以我试图实现类似的目标(在 FactoryBot 中):
build(:post, fields: [:id,:title])
并设置一个仅包含 id 和 title 的 Post 对象。
如果我这样做build(:post, fields: [:title, :created_at])
并设置一个仅包含标题和created_at的Post对象。等等...
做了一些研究并尝试了一些想法,但关于如何构建这个流程的所有想法都失败了。
关于如何实现这种行为有什么想法吗?
EDIT
Traits 似乎是一个不错的选择,但我必须与 API 调用保持一致,指定这些fields
. 所以特质对我不起作用......
有一些文件我在重复这个模式:
beforeAll(() => {
MockDate.set(1487076708000)
})
afterAll(() => {
MockDate.reset()
})
Run Code Online (Sandbox Code Playgroud)
我在这里做的唯一一件事就是模拟日期。我想知道是否有办法将这些方法导出到帮助程序中,然后在所需的文件中调用它。
Global beforeAll 对我不起作用,因为我只需要 4 或 5 个文件。
谢谢!
我正在使用 apollo-boost 构建一个应用程序,其中应该已经包含一些使用 graphql 执行 React 应用程序的包。
我想将数据片段存储到 Apollo 缓存时遇到错误。
ApolloClient.tsx
import ApolloClient from 'apollo-boost'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { secretToken } from './secretToken'
const cache = new InMemoryCache({
})
const client = new ApolloClient({
uri: 'https://someUrlIremovedForTheQuestion.com',
request: async operation => {
operation.setContext({
headers: {
Authorization: secretToken
}
})
},
cache
})
client.writeData({
data: {
names: [
{
firstName: 'Mario'
},
{
firstName: 'Luigi'
}
]
}
})
export default client
Run Code Online (Sandbox Code Playgroud)
现在我试图访问我在同一个 apollo 配置中创建的这些名称:
我的组件.tsx
const result …
Run Code Online (Sandbox Code Playgroud) reactjs ×4
graphql ×2
javascript ×2
jestjs ×2
react-apollo ×2
unit-testing ×2
angular ×1
factory-bot ×1
react-native ×1
redux ×1
rspec ×1
ruby ×1
testing ×1