如何用开玩笑测试chrome.storage.sync.get()值?
我的React组件包含一个类似的处理程序,该处理程序在按钮的on Click函数中称为:
const handleSaveInformationButtonClick = () => {
chrome.storage.sync.set({ information }, () => {
// do something
})
}
我想使用开玩笑和React-Testing-library在存储中测试键“信息”的值。我写了这项测试:
test("save new information into browser storage", () => {
const { rerender } = render(<MyComponent />);
const inputInformation = screen.getByTestId("main-input");
const buttonSave = screen.getByTestId("main-button");
const newInformation = "something here";
fireEvent.change(inputInformation, { target: { value: newInformation } });
fireEvent.click(buttonSave);
rerender(<MyComponent />);
expect(chrome.storage.sync.get("information")).toBe(newInformation);
});
我将Chrome添加为包装中的全局变量。JSON:
"jest": {
"globals": {
"chrome": {}
},
...
但是,当我运行测试时,我得到了:
TypeError: Cannot read property 'sync' of undefined
on line : 'expect(chrome.storage.sync.get("information")).toBe(newInformation);'
我该如何测试?
My React component contains a handler like this one which is called in a onClick function of a button:
const handleSaveInformationButtonClick = () => {
chrome.storage.sync.set({ information }, () => {
// do something
})
}
I would like to test the value of the key "information" in the storage using Jest and react-testing-library. I wrote this test :
test("save new information into browser storage", () => {
const { rerender } = render(<MyComponent />);
const inputInformation = screen.getByTestId("main-input");
const buttonSave = screen.getByTestId("main-button");
const newInformation = "something here";
fireEvent.change(inputInformation, { target: { value: newInformation } });
fireEvent.click(buttonSave);
rerender(<MyComponent />);
expect(chrome.storage.sync.get("information")).toBe(newInformation);
});
I added chrome as global variable inside package.json :
"jest": {
"globals": {
"chrome": {}
},
...
but when I run the test, I got :
TypeError: Cannot read property 'sync' of undefined
on line : 'expect(chrome.storage.sync.get("information")).toBe(newInformation);'
How can I test this please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论