Skip to content

Commit

Permalink
Fixed media field unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
konzz committed Dec 13, 2024
1 parent 56edc57 commit 7eb49d7
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions app/react/Forms/components/specs/MediaField.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,34 @@ describe('MediaField', () => {
name: '',
};

const render = (otherProps = {}) => {
({ renderResult } = renderConnectedContainer(
<MediaField
formField=""
type={MediaModalType.Image}
value={null}
{...{ ...baseProps, ...otherProps }}
/>,
() => defaultState
));
const render = async (otherProps = {}) => {
await act(async () => {
({ renderResult } = renderConnectedContainer(
<MediaField
formField=""
type={MediaModalType.Image}
value={null}
{...{ ...baseProps, ...otherProps }}
/>,
() => defaultState
));
});
};

describe('Object URL handling', () => {
const mockedCreateObjectURL: jest.Mock = jest.fn();
const mockedRevokeObjectURL: jest.Mock = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
global.fetch = jest.fn();
(global.fetch as jest.Mock).mockResolvedValue({ ok: true });
});

afterEach(() => {
jest.restoreAllMocks();
});

beforeAll(() => {
URL.createObjectURL = mockedCreateObjectURL;
mockedCreateObjectURL.mockReturnValue('blob:abc');
Expand All @@ -96,20 +108,21 @@ describe('MediaField', () => {

it('should create an object URL with the file', async () => {
const file = new File(['hello'], 'hello.png', { type: 'image/png' });
render(imageProps);
const img = screen.getByRole('presentation') as HTMLImageElement;
await render(imageProps);

const img = screen.getByRole('img') as HTMLImageElement;
expect(img.src).toEqual('blob:abc');
expect(mockedCreateObjectURL).toHaveBeenCalledWith(file);
});

it('should revoke the created URL', async () => {
render(imageProps);
await render(imageProps);
renderResult.unmount();
expect(mockedRevokeObjectURL).toHaveBeenCalledWith('blob:abc');
});

it('should change the media value according with markdownmedia variations', async () => {
render(mediaProps);
await render(mediaProps);

await act(async () => {
fireEvent.click(screen.getByText('Add timelink').parentElement!);
Expand Down Expand Up @@ -158,7 +171,7 @@ describe('MediaField', () => {
});

it('should show and error if the image is not valid', async () => {
render(imageProps);
await render(imageProps);
const img = renderResult.container.getElementsByTagName('img')[0];
fireEvent.error(img);
expect(
Expand Down

0 comments on commit 7eb49d7

Please sign in to comment.