gpt4 book ai didi

firebase - 有没有办法对于Jest的方式模拟 firebase 模块?

转载 作者:行者123 更新时间:2023-12-02 19:33:52 25 4
gpt4 key购买 nike

代码.js

saveToFirebase = () => {
let statusMsg = ""

firebase
.firestore()
.collection("messages")
.doc(this.state.field.email)
.set(this.state.field)
.then((statusMsg = "Your Message have been submitted successfully."))

this.clearFields(statusMsg)
}

代码.test.js

it("should call mock firebase module", () => {
const docData = {
field: {
name: "Rob Bob",
email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3b1aca183a1aca1eda0acae" rel="noreferrer noopener nofollow">[email protected]</a>",
phone: "9999999999",
subject: "Test subject",
message: "Test message",
},
}

const docResult = {
data: () => docData,
}

const get = jest.fn(() => Promise.resolve(docResult))
const set = jest.fn()

const doc = jest.fn(() => {
return {
set,
get,
}
})
const colllection = jest.fn((messages) => {
return { doc }
})
const firestore = () => {
return colllection
}

firebase.firestore = firestore

const mockData = { fake: "data" }
jest.clearAllMocks()
wrapper.instance().saveToFirebase()
})

运行 Code.test.js 时,它会抛出一个错误,指出 firebase.firestore().collection() 不是函数。请建议对上述代码的 firebase 模块进行正确的模拟,并添加一个案例来检查 firebase 是否已成功返回,然后调用clearFields()。

最佳答案

您可以使用jest.spyOn(object, methodName)模拟 firebase.firestore() 函数。

例如index.jsx:

import React, { Component } from 'react';
import firebase from 'firebase';

export default class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
field: { email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8de0e2eee6cde8f5ece0fde1e8a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>' },
};
}
clearFields(statusMsg) {
console.log(statusMsg);
}
saveToFirebase = () => {
let statusMsg = '';

return firebase
.firestore()
.collection('messages')
.doc(this.state.field.email)
.set(this.state.field)
.then(() => {
statusMsg = 'Your Message have been submitted successfully.';
this.clearFields(statusMsg);
});
};
render() {
return <div>my component</div>;
}
}

index.test.jsx:

import MyComponent from '.';
import { shallow } from 'enzyme';
import React from 'react';
import firebase from 'firebase';

describe('61358076', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('should pass', async () => {
const firestoreMock = {
collection: jest.fn().mockReturnThis(),
doc: jest.fn().mockReturnThis(),
set: jest.fn().mockResolvedValueOnce(),
};
const clearFieldsSpy = jest.spyOn(MyComponent.prototype, 'clearFields');
jest.spyOn(firebase, 'firestore').mockImplementationOnce(() => firestoreMock);
const wrapper = shallow(<MyComponent></MyComponent>);
await wrapper.instance().saveToFirebase();
expect(firestoreMock.collection).toBeCalledWith('messages');
expect(firestoreMock.doc).toBeCalledWith('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b5b7bbb398bda0b9b5a8b4bdf6bbb7b5" rel="noreferrer noopener nofollow">[email protected]</a>');
expect(firestoreMock.set).toBeCalledWith({ email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="335e5c505873564b525e435f561d505c5e" rel="noreferrer noopener nofollow">[email protected]</a>' });
expect(clearFieldsSpy).toBeCalledWith('Your Message have been submitted successfully.');
});
});

100%覆盖率的单元测试结果:

 PASS  stackoverflow/61358076/index.test.jsx (17.824s)
61358076
✓ should pass (39ms)

console.log stackoverflow/61358076/index.jsx:1611
Your Message have been submitted successfully.

-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.jsx | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 19.945s

源代码:https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/61358076

关于firebase - 有没有办法对于Jest的方式模拟 firebase 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61358076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com