有人告訴我,我們應該使用模擬資料,而不是與我們的真實資料庫資源互動。
我還被告知我應該“模擬 firebase 物件并從中回傳虛假結果”,但是我在哪里模擬 firebase 物件,它是我的實際組件還是在我的測驗檔案中,我如何實際模擬它?
這是我的代碼:
// just a hook that fetches current user data
export const useGetCurrentUsersProfile = () => {
const userInfoRef = doc(firestore, "users", auth.currentUser?.uid ?? "_STUB_");
return useFirestoreDocument(
["userInfo"],
userInfoRef,
{
subscribe: false,
},
{
select: (data) => {
const currentUsersProfile = data.data() as UserInfo;
return currentUsersProfile;
},
enabled: !!auth.currentUser?.uid,
}
);
};
我使用該鉤子獲取資料的反應組件:
import React from "react";
import { useGetCurrentUsersProfile } from "../app/utilityfunctions";
function Siuu() {
const { data: currentUser } = useGetCurrentUsersProfile();
return (
<div>
<div>{currentUser?.name}</div>
</div>
);
}
export default Siuu;
/** @jest-environment jsdom */
import { render, screen } from "@testing-library/react";
import Siuu from "./siuu";
test("renders component", () => {
render(<Siuu />);
const randomElement = screen.getByText(/Jack/i);
expect(randomElement).toBeInTheDocument();
});
如果我只想從我的鉤子中回傳這些資料,我在哪里以及如何模擬它:
{姓名:"jack",電子郵件:"[email protected]"}
uj5u.com熱心網友回復:
模擬是在測驗代碼中完成的,而不是在真實的應用程式代碼中。
它的意思是:你創建了一個東西的假版本,這樣當測驗讓應用程式做一些事情時,真實的東西就不會被使用。Jest 有工具可以幫助你做到這一點。
在您的情況下,您應該模擬的東西似乎是docand/oruseFirestoreDocument函式。您的代碼示例沒有說明這兩件事的來源,而且我不知道 firestore,所以我假設兩者都是從單個“some-firestore-pkg”包中匯入的,如下所示:import { doc, useFirestoreDocument } from 'some-firestore-pkg'
最簡單的方法是專門為此測驗創建一個一次性模擬,但如果通過應用程式使用這個 firestore 的東西,并且您想為其余部分撰寫測驗,您將需要閱讀工具和模式Jest 提供了創建可重用的模擬。對于這個答案,我將使用一次性模擬來完成這一切。
/*
STEP 1: Import some-firestore-pkg package so we can manipulate it during the
tests. This will be the real firestore package.
*/
import firestorePkg from 'some-firestore-pkg'
// ...skipping down to your test
test("renders component", () => {
/*
STEP 2: replace the real `doc` function with a fake version that returns a
hard-coded value.
We MUST replace it to prevent the real one from doing any real work,
although we may not need to make the fake version return anything. But,
let's do so, in order to miminize the chance that this test will stop
working when the hook is modified in the future to verify the results of
each operation.
*/
let docSpy = jest.spyOn(firestorePkg, 'doc').mockReturnValue(
'A_FAKE_USER_INFO_REF'
)
// now, when the real hook calls the doc(...) function, it will get that value
/*
STEP 3: replace the real useFirestoreDocument with a fake version that
returns the hard-coded fake data that you want the app to receive during the
test. You can probably guess what this will look like:
*/
let useDocSpy = jest.spyOn(firestorePkg, 'useFirestoreDocument').mockReturnValue(
{ data: { name: "jack", email: "[email protected]" } }
)
/*
STEP 4: now that we've "arranged," let's "act" and then "assert"
*/
let app = render(<Siuu />)
let randomElement = app.getByText(/Jack/i)
expect(randomElement).toBeInTheDocument()
// STEP 5: completely remove our fake versions so other tests aren't impacted
docSpy.mockRestore()
useDocSpy.mockRestore()
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/452778.html
