How do I mock a function that I have assigned a new name in Jest?

So I apologize if this question is a bit trivial, but I’ve been combing the deocs looking for help and I’m still stuck.

I have a custom React hook called “useTranslator” that I use in some React components to translated strings into other languages. useTranslator is built on top of a custom i18N library my team uses.

Okay, so I’m working on writing some test for my React components using Jest. Since I want to isolate this library I just need to mock one function in it that comes from useTranslator". It's a name export and I assign it to the function named translate()`.

The component looks like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { useTranslator } from "../../../stores/i18nStore";
const DialogBox = ({open, handleClose, closeBtnText, handleConfirm, confirmBtnText , title, children}: DialogBoxProps) => {
const translated = useTranslator();
closeBtnText = closeBtnText ?? translated("js.dialog_default_cancel");
confirmBtnText = confirmBtnText ?? translated("js.dialog_default_confirm");
</code>
<code>import { useTranslator } from "../../../stores/i18nStore"; const DialogBox = ({open, handleClose, closeBtnText, handleConfirm, confirmBtnText , title, children}: DialogBoxProps) => { const translated = useTranslator(); closeBtnText = closeBtnText ?? translated("js.dialog_default_cancel"); confirmBtnText = confirmBtnText ?? translated("js.dialog_default_confirm"); </code>
import { useTranslator } from "../../../stores/i18nStore";


const DialogBox = ({open, handleClose, closeBtnText, handleConfirm, confirmBtnText , title, children}: DialogBoxProps) => {
    const translated = useTranslator();
    closeBtnText = closeBtnText ?? translated("js.dialog_default_cancel");
    confirmBtnText = confirmBtnText ?? translated("js.dialog_default_confirm");

So here’s the problem I’m having is that Jest can’t find the function named translated() because I’m not sure how I’m supposed to mock it.

All I need Jest to do is return a string with whatever is pass into translated().

I want to check that "js.dialog_default_cancel" or "js.dialog_default_confirm" is now what is render into the Dom.

So far my test looks like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { useTranslator } from "../../../stores/i18nStore";
jest.mock('../../../stores/i18nStore', () => ({
useTranslator: () => {},
}))
test("Dialog Box", () => {
it('renders default text strings', () => {
render(
<DialogBox
title="Test Dialog"
open={true}
handleClose={()=>{}}
handleConfirm={()=>{}}>
Test Dialog Message 3
</DialogBox>
)
const dialogComp = screen.getByText("Test Dialog");
expect(dialogComp).toBeInTheDocument()
})
</code>
<code>import { useTranslator } from "../../../stores/i18nStore"; jest.mock('../../../stores/i18nStore', () => ({ useTranslator: () => {}, })) test("Dialog Box", () => { it('renders default text strings', () => { render( <DialogBox title="Test Dialog" open={true} handleClose={()=>{}} handleConfirm={()=>{}}> Test Dialog Message 3 </DialogBox> ) const dialogComp = screen.getByText("Test Dialog"); expect(dialogComp).toBeInTheDocument() }) </code>
import { useTranslator } from "../../../stores/i18nStore";
jest.mock('../../../stores/i18nStore', () => ({
    useTranslator: () => {},
}))

test("Dialog Box", () => {

    it('renders default text strings', () => {
       
        render( 
            <DialogBox 
                title="Test Dialog" 
                open={true} 
                handleClose={()=>{}}
                handleConfirm={()=>{}}>
                    Test Dialog Message 3
            </DialogBox>
        )

        const dialogComp = screen.getByText("Test Dialog");
        expect(dialogComp).toBeInTheDocument()
    })

When I run it I get this error message.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>console.error
Error: Uncaught [TypeError: translated is not a function]
</code>
<code>console.error Error: Uncaught [TypeError: translated is not a function] </code>
console.error
    Error: Uncaught [TypeError: translated is not a function]

How do I tell Jest this i function and i want it to just return me what ever string I pass into it.

Thanks

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật