I have a React Functional Component, and I want to write a unit test that checks if a function that gets called further down the chain has actually been called as a result of the component getting rendered.
I don't care which library I use, or which exact method from said library I use - I just want to be able to test that otherHelper
has been called, so feel free to suggest other approaches than this one below.
helper.ts
export const otherHelper = () => {}
export const helper = () => {
otherHelper()
}
App.tsx
import { helper } from './helper'
const App = () => {
helper()
return <h1>App</h1>
}
Now, here is what I'd like my test to be:
App.test.tsx
import { App } from './App'
import {otherHelper} from './helper'
import {render} from '@testing-library/react'
describe("App", () => {
it("calls otherHelper", () => {
const RenderedApp = render(<App />)
const spy = jest.spyOn(RenderedApp, otherHelper)
expect(spy).toHaveBeenCalled()
})
})
Currently this does not work. Any ideas on how to make it work?
Because Jest's spyOn
method requires an object, I'd be happy to re-write my otherHelper code as follows:
helper.ts
export const otherHelper = {
help: () => {}
}