Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
usavkov-epam committed Oct 11, 2023
1 parent ba6b0db commit 6429ac3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/components/NewOrderModal/NewOrderModalContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const NewOrderModalContainer = ({
const route = orderId ? `/orders/view/${orderId}/po-line/create` : '/orders/create';
const instanceTenantId = instance?.tenantId || stripes.okapi.tenant;

history.push(route, { instanceId: id, instanceTenantId });
history.push(route, { instanceId: instance?.id, instanceTenantId });
}, [instance, orderId, stripes.okapi.tenant]);

return (
Expand Down
44 changes: 34 additions & 10 deletions src/components/NewOrderModal/NewOrderModalContainer.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import React from 'react';
import {
BrowserRouter as Router,
useHistory,
} from 'react-router-dom';
import { act, render, screen, fireEvent } from '@folio/jest-config-stripes/testing-library/react';

import '../../../test/jest/__mock__';

// eslint-disable-next-line import/order
import userEvent from '@folio/jest-config-stripes/testing-library/user-event';
import { render, screen } from '@folio/jest-config-stripes/testing-library/react';
import { useOkapiKy } from '@folio/stripes/core';

import '../../../test/jest/__mock__';

import { translationsProperties } from '../../../test/jest/helpers';
import Harness from '../../../test/jest/helpers/Harness';
import { instance } from '../../../test/fixtures/instance';
import { useInstance } from '../../common';
import NewOrderModalContainer from './NewOrderModalContainer';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useHistory: jest.fn()
}));
jest.mock('../../common', () => ({
...jest.requireActual('../../common'),
useInstance: jest.fn(),
}));

global.document.createRange = jest.fn(() => new Range());

const defaultProps = {
onCancel: jest.fn(),
Expand All @@ -44,18 +51,25 @@ describe('NewOrderModalContainer', () => {
const historyMock = {
push: jest.fn(),
};
const order = {
id: 'orderId',
};
const kyMock = {
get: jest.fn(() => ({
json: () => Promise.resolve({
purchaseOrders: [{ id: 'orderId' }],
purchaseOrders: [order],
})
}))
};

beforeEach(() => {
historyMock.push.mockClear();
useHistory
.mockClear()
.mockReturnValue(historyMock);
useInstance
.mockClear()
.mockReturnValue({ instance });
useOkapiKy
.mockClear()
.mockReturnValue(kyMock);
Expand All @@ -70,17 +84,27 @@ describe('NewOrderModalContainer', () => {
it('should navigate to \'PO\' creation form when create btn was clicked and \'PO number\' field is empty', async () => {
renderNewOrderModalContainer();

await act(async () => fireEvent.click(screen.getByText(/^Create$/)));
await userEvent.click(screen.getByText(/^Create$/));

expect(historyMock.push).toHaveBeenCalledWith('/orders/create', expect.anything());
});

it('should navigate to \'PO Line\' creation form when create btn was clicked and PO is exist', async () => {
renderNewOrderModalContainer();

await act(async () => fireEvent.change(screen.getByLabelText(/PO number/i), { target: { value: '123' } }));
await act(async () => fireEvent.click(screen.getByText(/^Create$/)));
await userEvent.type(screen.getByLabelText(/PO number/i), '123');
await userEvent.tab();

expect(screen.queryByText('ui-inventory.newOrder.modal.PONumber.doesNotExist')).not.toBeInTheDocument();

await userEvent.click(await screen.findByText(/^Create$/));

expect(historyMock.push).toHaveBeenCalledWith('/orders/create', { 'instanceId': undefined });
expect(historyMock.push).toHaveBeenCalledWith(
`/orders/view/${order.id}/po-line/create`,
{
instanceId: instance.id,
instanceTenantId: 'diku',
},
);
});
});

0 comments on commit 6429ac3

Please sign in to comment.