|
| 1 | +import { FC } from 'react' |
| 2 | + |
| 3 | +import { render } from '@baseapp-frontend/test' |
| 4 | +import { IJWTContent } from '@baseapp-frontend/utils' |
| 5 | + |
| 6 | +import withUser from '..' |
| 7 | +import { IUser } from '../../../../types/user' |
| 8 | +import getUser from '../../getUser' |
| 9 | +import { ComponentWithUser } from '../types' |
| 10 | + |
| 11 | +jest.mock('../../getUser', () => jest.fn()) |
| 12 | + |
| 13 | +type User = IUser & IJWTContent |
| 14 | + |
| 15 | +const MockComponent: FC<ComponentWithUser<User>> = ({ user }) => ( |
| 16 | + <div>{user ? `Hello, ${user.firstName}` : 'No user'}</div> |
| 17 | +) |
| 18 | + |
| 19 | +describe('withUser HOC', () => { |
| 20 | + const getUserMock = getUser as jest.Mock |
| 21 | + |
| 22 | + it('passes the user object to the wrapped component', async () => { |
| 23 | + const userMock = { firstName: 'John', lastName: 'Doe' } |
| 24 | + getUserMock.mockResolvedValue(userMock) |
| 25 | + |
| 26 | + const WithUserComponent = withUser<User>(MockComponent) |
| 27 | + const { findByText } = render(await WithUserComponent({})) |
| 28 | + |
| 29 | + const userElement = await findByText(`Hello, ${userMock.firstName}`) |
| 30 | + expect(userElement).toBeInTheDocument() |
| 31 | + }) |
| 32 | + |
| 33 | + it('handles the case when there is no user', async () => { |
| 34 | + getUserMock.mockResolvedValue(null) |
| 35 | + |
| 36 | + const WithUserComponent = withUser<User>(MockComponent) |
| 37 | + const { findByText } = render(await WithUserComponent({})) |
| 38 | + |
| 39 | + const userElement = await findByText('No user') |
| 40 | + expect(userElement).toBeInTheDocument() |
| 41 | + }) |
| 42 | +}) |
0 commit comments