我正在为一个 React 聊天应用程序编写测试用例,我们为此使用了 amazon-connect-chatjs 库,因此该库提供了一些 api,如 session.onConnectionEstablished 和 session.onMessage 等,在这个 api 中,我们正在编写一些逻辑基于该组件将相应地呈现。 所以我想模拟例如 session.onConnectionEstablished 并在该方法或 api 内部传递,以便它通过我们将呈现特定组件的逻辑,我将为该组件编写测试用例 哪个渲染,任何想法如何做到这一点。请看下面的逻辑
import 'amazon-connect-chatjs'
const chat = ()=>{
const [userName setUserName] = useState('')
/* initial setup to initiate amazon-connect-chatjs */
connect.ChatSession.setGlobalConfig({
loggerConfig: {
logger: {
debug: (msg) => console.debug(msg),
info: (msg) => console.info(msg),
warn: (msg) => console.warn(msg),
error: (msg) => console.error(msg)
},
level: connect.ChatSession.LogLevel.WARN,
},
region: "us-east-1",
});
const customerChatSession = connect.ChatSession.create({
chatDetails: { // REQUIRED
contactId: "...", // REQUIRED
participantId: "...", // REQUIRED
participantToken: "...", // REQUIRED
},
options: {
region: "us-east-1",
`connect.ChatSession.setGlobalConfig()`
},
type: connect.ChatSession.SessionTypes.CUSTOMER, // REQUIRED
});
customerChatSession.create().then((response)=>{
return response;
}
)
customerChatSession.onConnectionEstablished(()=>{
setLoaderMessage('connecting...')
setUserName('true')
})
{{userName && <MessageList>}}
}
注意:通过提供模拟数据,我可以将所需的详细信息传递给 chatjs 库,然后我会收到来自 customerChatSession.create().then((response)=>{ return response; }
但在那之后我有点坚持如何嘲笑
customerChatSession.onConnectionEstablished
并通过该方法,如果它通过,它会设置将呈现组件的 useState,以便我可以测试该组件是否正在呈现
我不确定模拟是写方法还是其他任何方法
谢谢