-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bdb1c3
commit 7a47e5e
Showing
9 changed files
with
414 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { OrderPriceData } from '@openmrs/esm-patient-orders-app/src/types/order'; | ||
|
||
export const mockOrderPriceData: OrderPriceData = { | ||
resourceType: 'Bundle', | ||
id: 'test-id', | ||
meta: { | ||
lastUpdated: '2024-01-01T00:00:00Z', | ||
}, | ||
type: 'searchset', | ||
link: [ | ||
{ | ||
relation: 'self', | ||
url: 'test-url', | ||
}, | ||
], | ||
entry: [ | ||
{ | ||
resource: { | ||
resourceType: 'ChargeItemDefinition', | ||
id: 'test-resource-id', | ||
name: 'Test Item', | ||
status: 'active', | ||
date: '2024-01-01', | ||
propertyGroup: [ | ||
{ | ||
priceComponent: [ | ||
{ | ||
type: 'base', | ||
amount: { | ||
value: 99.99, | ||
currency: 'USD', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export const mockOrderStockData = { | ||
resourceType: 'Bundle', | ||
id: 'test-id', | ||
meta: { | ||
lastUpdated: '2024-01-01T00:00:00Z', | ||
}, | ||
type: 'searchset', | ||
link: [ | ||
{ | ||
relation: 'self', | ||
url: 'test-url', | ||
}, | ||
], | ||
entry: [ | ||
{ | ||
resource: { | ||
resourceType: 'InventoryItem', | ||
id: 'test-resource-id', | ||
meta: { | ||
profile: ['test-profile'], | ||
}, | ||
status: 'active', | ||
code: [ | ||
{ | ||
coding: [ | ||
{ | ||
system: 'test-system', | ||
code: 'test-code', | ||
display: 'Test Item', | ||
}, | ||
], | ||
}, | ||
], | ||
name: [ | ||
{ | ||
name: 'Test Item', | ||
}, | ||
], | ||
netContent: { | ||
value: 10, | ||
unit: 'units', | ||
}, | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/esm-patient-orders-app/src/components/order-price-details.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from 'react'; | ||
import { screen } from '@testing-library/react'; | ||
import OrderPriceDetailsComponent from './order-price-details.component'; | ||
import { useOrderPrice } from '../hooks/useOrderPrice'; | ||
import { renderWithSwr } from 'tools'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { mockOrderPriceData } from '../../../../__mocks__/order-price-data.mock'; | ||
|
||
jest.mock('../hooks/useOrderPrice'); | ||
jest.mock('react-i18next', () => ({ | ||
useTranslation: jest.fn(), | ||
})); | ||
|
||
const mockUseTranslation = useTranslation as jest.Mock; | ||
|
||
describe('OrderPriceDetailsComponent', () => { | ||
const mockOrderItemUuid = 'test-uuid'; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
mockUseTranslation.mockImplementation(() => ({ | ||
t: (key: string, fallback: string) => fallback, | ||
i18n: { language: 'en-US' }, | ||
})); | ||
}); | ||
|
||
it('renders loading skeleton when data is loading', () => { | ||
(useOrderPrice as jest.Mock).mockReturnValue({ | ||
data: null, | ||
isLoading: true, | ||
}); | ||
|
||
renderWithSwr(<OrderPriceDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
expect(screen.getByTestId('skeleton-text')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders nothing when amount is null', () => { | ||
(useOrderPrice as jest.Mock).mockReturnValue({ | ||
data: { | ||
...mockOrderPriceData, | ||
entry: [], | ||
}, | ||
isLoading: false, | ||
}); | ||
|
||
const { container } = renderWithSwr(<OrderPriceDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('renders price correctly with USD currency', () => { | ||
(useOrderPrice as jest.Mock).mockReturnValue({ | ||
data: mockOrderPriceData, | ||
isLoading: false, | ||
}); | ||
|
||
renderWithSwr(<OrderPriceDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
|
||
expect(screen.getByText('Price:')).toBeInTheDocument(); | ||
expect(screen.getByText('$99.99')).toBeInTheDocument(); | ||
}); | ||
|
||
it('formats price correctly for different locales', () => { | ||
(useOrderPrice as jest.Mock).mockReturnValue({ | ||
data: mockOrderPriceData, | ||
isLoading: false, | ||
}); | ||
|
||
// Change to German locale for this test | ||
mockUseTranslation.mockImplementation(() => ({ | ||
t: (key: string, fallback: string) => fallback, | ||
i18n: { language: 'de-DE' }, | ||
})); | ||
|
||
renderWithSwr(<OrderPriceDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
|
||
// German locale uses comma as decimal separator | ||
expect(screen.getByText('99,99 $')).toBeInTheDocument(); | ||
}); | ||
|
||
it('displays tooltip with price disclaimer', () => { | ||
(useOrderPrice as jest.Mock).mockReturnValue({ | ||
data: mockOrderPriceData, | ||
isLoading: false, | ||
}); | ||
|
||
renderWithSwr(<OrderPriceDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
|
||
expect(screen.getByRole('button')).toBeInTheDocument(); | ||
expect(screen.getByLabelText(/This price is indicative/)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/esm-patient-orders-app/src/components/order-stock-details.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import React from 'react'; | ||
import { screen } from '@testing-library/react'; | ||
import OrderStockDetailsComponent from './order-stock-details.component'; | ||
import { useOrderStockInfo } from '../hooks/useOrderStockInfo'; | ||
import { renderWithSwr } from 'tools'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { mockOrderStockData } from '../../../../__mocks__/order-stock-data.mock'; | ||
|
||
jest.mock('../hooks/useOrderStockInfo'); | ||
jest.mock('react-i18next', () => ({ | ||
useTranslation: jest.fn(), | ||
})); | ||
|
||
const mockUseTranslation = useTranslation as jest.Mock; | ||
|
||
describe('OrderStockDetailsComponent', () => { | ||
const mockOrderItemUuid = 'test-uuid'; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
mockUseTranslation.mockImplementation(() => ({ | ||
t: (key: string, fallback: string) => fallback, | ||
})); | ||
}); | ||
|
||
it('renders loading skeleton when data is loading', () => { | ||
(useOrderStockInfo as jest.Mock).mockReturnValue({ | ||
data: null, | ||
isLoading: true, | ||
}); | ||
|
||
renderWithSwr(<OrderStockDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
expect(screen.getByTestId('skeleton-text')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders nothing when stock data is null', () => { | ||
(useOrderStockInfo as jest.Mock).mockReturnValue({ | ||
data: null, | ||
isLoading: false, | ||
}); | ||
|
||
const { container } = renderWithSwr(<OrderStockDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
expect(container).toBeEmptyDOMElement(); | ||
}); | ||
|
||
it('renders "In Stock" when item is active and has positive quantity', () => { | ||
(useOrderStockInfo as jest.Mock).mockReturnValue({ | ||
data: mockOrderStockData, | ||
isLoading: false, | ||
}); | ||
|
||
renderWithSwr(<OrderStockDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
|
||
expect(screen.getByText('In Stock')).toBeInTheDocument(); | ||
expect(screen.getByText('CheckmarkFilledIcon')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders "Out of Stock" when item has zero quantity', () => { | ||
const outOfStockData = { | ||
...mockOrderStockData, | ||
entry: [ | ||
{ | ||
...mockOrderStockData.entry[0], | ||
resource: { | ||
...mockOrderStockData.entry[0].resource, | ||
netContent: { | ||
value: 0, | ||
unit: 'units', | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
(useOrderStockInfo as jest.Mock).mockReturnValue({ | ||
data: outOfStockData, | ||
isLoading: false, | ||
}); | ||
|
||
renderWithSwr(<OrderStockDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
|
||
expect(screen.getByText('Out of Stock')).toBeInTheDocument(); | ||
expect(screen.getByText('CloseFilledIcon')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders "Out of Stock" when item is inactive', () => { | ||
const inactiveData = { | ||
...mockOrderStockData, | ||
entry: [ | ||
{ | ||
...mockOrderStockData.entry[0], | ||
resource: { | ||
...mockOrderStockData.entry[0].resource, | ||
status: 'inactive', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
(useOrderStockInfo as jest.Mock).mockReturnValue({ | ||
data: inactiveData, | ||
isLoading: false, | ||
}); | ||
|
||
renderWithSwr(<OrderStockDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
|
||
expect(screen.getByText('Out of Stock')).toBeInTheDocument(); | ||
expect(screen.getByText('CloseFilledIcon')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders "Out of Stock" when entry array is empty', () => { | ||
const emptyData = { | ||
...mockOrderStockData, | ||
entry: [], | ||
}; | ||
|
||
(useOrderStockInfo as jest.Mock).mockReturnValue({ | ||
data: emptyData, | ||
isLoading: false, | ||
}); | ||
|
||
renderWithSwr(<OrderStockDetailsComponent orderItemUuid={mockOrderItemUuid} />); | ||
|
||
expect(screen.getByText('Out of Stock')).toBeInTheDocument(); | ||
expect(screen.getByText('CloseFilledIcon')).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.