Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) O3-2666: Order getting removed after clicking on Back to order basket #1549

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { screen, render, within, renderHook } from '@testing-library/react';
import { screen, render, within, renderHook, fireEvent, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { getByTextWithMarkup } from '../../../../tools/test-helpers';
import { getTemplateOrderBasketItem, useDrugSearch, useDrugTemplate } from './drug-search/drug-search.resource';
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('AddDrugOrderWorkspace drug search', () => {
expect(asprin162.closest('div')).toHaveTextContent(/Aspirin.*162.5mg.*tablet/i);
});

test('no buttons to click if the medicaiton is already prescribed', async () => {
test('no buttons to click if the medication is already prescribed', async () => {
usePatientOrdersMock.mockReturnValue({
isLoading: false,
data: [mockPatientDrugOrdersApiData[0]],
Expand Down Expand Up @@ -147,21 +147,10 @@ describe('AddDrugOrderWorkspace drug search', () => {
const aspirin81OpenFormButton = within(aspirin81Div).getByText(/Order form/i);
await user.click(aspirin81OpenFormButton);

expect(hookResult.current.orders).toEqual([
expect.objectContaining({
...getTemplateOrderBasketItem(
mockDrugSearchResultApiData[0],
undefined,
mockDrugOrderTemplateApiData[mockDrugSearchResultApiData[0].uuid][0],
),
startDate: expect.any(Date),
}),
]);

usamaidrsk marked this conversation as resolved.
Show resolved Hide resolved
expect(screen.getByText(/Order Form/i)).toBeInTheDocument();
});

test('can open an item in the medication form', async () => {
test('can open an item in the medication form and on saving, it should add the order in the order basket store', async () => {
const user = userEvent.setup();
renderDrugSearch();
const { result: hookResult } = renderHook(() =>
Expand All @@ -172,16 +161,26 @@ describe('AddDrugOrderWorkspace drug search', () => {
const openFormButton = within(aspirin81Div).getByText(/Order form/i);
await user.click(openFormButton);

expect(hookResult.current.orders).toEqual([
expect.objectContaining({
...getTemplateOrderBasketItem(
mockDrugSearchResultApiData[0],
undefined,
mockDrugOrderTemplateApiData[mockDrugSearchResultApiData[0].uuid][0],
),
startDate: expect.any(Date),
}),
]);
usamaidrsk marked this conversation as resolved.
Show resolved Hide resolved
expect(screen.getByText(/Order Form/i)).toBeInTheDocument();
const indicationField = screen.getByRole('textbox', { name: 'Indication' });
await user.type(indicationField, 'Hypertension');
const saveFormButton = screen.getByText(/Save order/i);
fireEvent.click(saveFormButton);

await waitFor(() =>
expect(hookResult.current.orders).toEqual([
expect.objectContaining({
...getTemplateOrderBasketItem(
mockDrugSearchResultApiData[0],
undefined,
mockDrugOrderTemplateApiData[mockDrugSearchResultApiData[0].uuid][0],
),
startDate: expect.any(Date),
indication: 'Hypertension',
careSetting: '6f0c9a92-6f24-11e3-af88-005056821db0',
orderer: 'mock-provider-uuid',
}),
]),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default function AddDrugOrderWorkspace({ order: initialOrder, closeWorksp
const session = useSession();

const cancelDrugOrder = useCallback(() => {
setOrders(orders.filter((order) => !ordersEqual(order, currentOrder)));
closeWorkspace();
launchPatientWorkspace('order-basket');
}, [closeWorkspace, currentOrder, orders, setOrders]);
Expand All @@ -30,11 +29,10 @@ export default function AddDrugOrderWorkspace({ order: initialOrder, closeWorksp
if (existingOrder) {
setCurrentOrder(existingOrder);
} else {
setOrders([...orders, searchResult]);
setCurrentOrder(searchResult);
}
},
[setOrders, orders],
[orders],
);

const saveDrugOrder = useCallback(
Expand All @@ -43,11 +41,15 @@ export default function AddDrugOrderWorkspace({ order: initialOrder, closeWorksp
finalizedOrder.orderer = session.currentProvider.uuid;
const newOrders = [...orders];
const existingOrder = orders.find((order) => ordersEqual(order, finalizedOrder));
newOrders[orders.indexOf(existingOrder)] = {
...finalizedOrder,
// Incomplete orders should be marked completed on saving the form
isOrderIncomplete: false,
};
if (existingOrder) {
newOrders[orders.indexOf(existingOrder)] = {
...finalizedOrder,
// Incomplete orders should be marked completed on saving the form
isOrderIncomplete: false,
};
} else {
newOrders.push(finalizedOrder);
}
setOrders(newOrders);
closeWorkspace();
launchPatientWorkspace('order-basket');
Expand Down
Loading