Issues in unit testing using React testing library #550
Replies: 4 comments 9 replies
-
Hi @aravind-merck |
Beta Was this translation helpful? Give feedback.
-
I took a look at the unit tests as they are configured in the liquid-sandbox-cra-tailwind and using the web components' implementation without having to mock them. An example test could look like this: import { LdInput } from '@emdgroup-liquid/liquid/dist/react'
import { jest } from '@jest/globals'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
test('renders heading', async () => {
const handler = jest.fn()
render(<LdInput placeholder="Example value" onLdinput={handler} />)
const ldInput =
screen.getByPlaceholderText<HTMLLdInputElement>('Example value')
await waitFor(() => expect(ldInput).toHaveClass('hydrated'))
// eslint-disable-next-line testing-library/no-node-access
const innerInput = ldInput.shadowRoot?.querySelector('input')!
await userEvent.type(innerInput, ' new')
expect(handler).toHaveBeenCalledTimes(4)
}) the jest setup file that is referenced in the // jest-setup.ts
import { defineCustomElements } from '@emdgroup-liquid/liquid/dist/loader'
defineCustomElements() @aravind-merck Please tell me, if this helps you getting tests to work without having to mock every Liquid Oxygen web component. Please be aware that you need the latest versions of the React Testing Library dependencies for the above example to work properly. |
Beta Was this translation helpful? Give feedback.
-
How can I test the Internal Functions at the React Component by React Testing Library & Jest ? |
Beta Was this translation helpful? Give feedback.
-
I have a function that dispatch an action to the state & it works perfectly. But, the problem is does not execute setState method next to the dispatch method in that function. Then, how to solve it ..? |
Beta Was this translation helpful? Give feedback.
-
Hi, We are trying to add the unit tests using React testing library. As Liquid Oxygen components are the web component, we are not able to use the React testing library effectively.
Using jest mocks its working perfectly but the problem is, we have mock each of the components and exact functionality. Its quiet time consuming work.
Please suggest which library we can use for the unit testing without mocking the behaviour of those component?
Beta Was this translation helpful? Give feedback.
All reactions