|
1 | 1 | import React from 'react' |
2 | 2 | import { instance, mock } from 'ts-mockito' |
3 | | -import { fireEvent, render, screen } from 'testSrc/helpers' |
4 | | -import { EditEntireItemAction, Props } from './EditEntireItemAction' |
| 3 | +import { screen, fireEvent } from 'testSrc/helpers' |
5 | 4 | import { JSONErrors } from '../../constants' |
6 | 5 |
|
7 | | -const mockedProps = mock<Props>() |
8 | | - |
| 6 | +const mockedProps = mock<any>() |
9 | 7 | const valueOfEntireItem = '"Sample string"' |
10 | 8 |
|
| 9 | +async function renderWithMockedMonaco(props: any) { |
| 10 | + vi.resetModules() |
| 11 | + vi.doMock('react-monaco-editor', () => ({ |
| 12 | + default: ({ value, onChange, 'data-testid': dataTestId }: any) => ( |
| 13 | + <textarea |
| 14 | + data-testid={dataTestId} |
| 15 | + value={value} |
| 16 | + onChange={(e) => onChange?.(e.target.value)} |
| 17 | + /> |
| 18 | + ), |
| 19 | + })) |
| 20 | + |
| 21 | + const { EditEntireItemAction } = await import('./EditEntireItemAction') |
| 22 | + const { render } = await import('testSrc/helpers') |
| 23 | + return render(<EditEntireItemAction {...props} />) |
| 24 | +} |
| 25 | + |
11 | 26 | describe('EditEntireItemAction', () => { |
12 | | - it('renders correctly with provided props', () => { |
13 | | - render(<EditEntireItemAction |
14 | | - {...instance(mockedProps)} |
15 | | - initialValue={valueOfEntireItem} |
16 | | - />) |
| 27 | + it('renders correctly with provided props', async () => { |
| 28 | + await renderWithMockedMonaco({ |
| 29 | + ...instance(mockedProps), |
| 30 | + initialValue: valueOfEntireItem, |
| 31 | + }) |
17 | 32 |
|
18 | 33 | expect(screen.getByTestId('json-value')).toBeInTheDocument() |
19 | 34 | expect(screen.getByTestId('json-value')).toHaveValue(valueOfEntireItem) |
20 | 35 | }) |
21 | 36 |
|
22 | | - it('triggers handleUpdateValueFormSubmit when the form is submitted', () => { |
| 37 | + it('triggers handleUpdateValueFormSubmit when the form is submitted', async () => { |
23 | 38 | const handleUpdateValueFormSubmit = vi.fn() |
24 | | - render(<EditEntireItemAction |
25 | | - {...instance(mockedProps)} |
26 | | - initialValue={valueOfEntireItem} |
27 | | - onSubmit={handleUpdateValueFormSubmit} |
28 | | - />) |
| 39 | + |
| 40 | + await renderWithMockedMonaco({ |
| 41 | + ...instance(mockedProps), |
| 42 | + initialValue: valueOfEntireItem, |
| 43 | + onSubmit: handleUpdateValueFormSubmit, |
| 44 | + }) |
29 | 45 |
|
30 | 46 | fireEvent.submit(screen.getByTestId('json-entire-form')) |
31 | 47 | expect(handleUpdateValueFormSubmit).toHaveBeenCalled() |
32 | 48 | }) |
33 | 49 |
|
34 | | - it('shouuld show error and do not submit', () => { |
| 50 | + it('should show error and not submit', async () => { |
35 | 51 | const handleUpdateValueFormSubmit = vi.fn() |
36 | | - render(<EditEntireItemAction |
37 | | - {...instance(mockedProps)} |
38 | | - initialValue="xxxx" |
39 | | - onSubmit={handleUpdateValueFormSubmit} |
40 | | - />) |
| 52 | + |
| 53 | + await renderWithMockedMonaco({ |
| 54 | + ...instance(mockedProps), |
| 55 | + initialValue: 'xxxx', |
| 56 | + onSubmit: handleUpdateValueFormSubmit, |
| 57 | + }) |
41 | 58 |
|
42 | 59 | fireEvent.submit(screen.getByTestId('json-entire-form')) |
43 | | - expect(screen.getByTestId('edit-json-error')).toHaveTextContent(JSONErrors.valueJSONFormat) |
| 60 | + expect(screen.getByTestId('edit-json-error')).toHaveTextContent( |
| 61 | + JSONErrors.valueJSONFormat, |
| 62 | + ) |
44 | 63 | expect(handleUpdateValueFormSubmit).not.toHaveBeenCalled() |
45 | 64 | }) |
46 | 65 | }) |
0 commit comments