Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions frontend/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Config } from 'jest';

const config: Config = {
rootDir: 'src',
testEnvironment: 'jest-environment-jsdom',
testMatch: ['**/*.test.tsx', '**/*.test.ts'],
transform: {
'^.+\\.(t|j)sx?$': [
'ts-jest',
{
tsconfig: '<rootDir>/../tsconfig.jest.json',
},
],
},
setupFilesAfterEnv: ['<rootDir>/../jest.setup.ts'],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/$1',
'config/api': '<rootDir>/__mocks__/config/api.ts',
},
Comment on lines +16 to +19
};

export default config;
9 changes: 9 additions & 0 deletions frontend/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import '@testing-library/jest-dom';
import { configure } from '@testing-library/react';

configure({ asyncUtilTimeout: 5000 });
jest.setTimeout(15000);

afterEach(() => {
window.sessionStorage.clear();
});
4 changes: 4 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"preview": "vite preview",
"typecheck": "tsc --noEmit",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"test": "jest --config jest.config.ts",
"clean": "rm -rf build node_modules/.vite"
},
"devDependencies": {
Expand All @@ -36,6 +37,9 @@
"eslint": "^8.55.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
"jest": "^29.7.0",
"jest-environment-jsdom": "^30.4.1",
"ts-jest": "^29.4.5",
Comment on lines +40 to +42
"typescript": "^5.3.3",
"vite": "^5.0.8"
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/__mocks__/config/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const API_URL = 'http://localhost:3001';
18 changes: 16 additions & 2 deletions frontend/src/pages/Inventory.editor-mode.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const mockCreateOrgItem = jest.fn();
const mockUpdateOrgItem = jest.fn();
const mockSearchItems = jest.fn();
const mockGetUserPermissions = jest.fn();
const mockGetCategories = jest.fn();

jest.mock('../services/inventory.service', () => ({
inventoryService: {
getCategories: jest.fn().mockResolvedValue([]),
getCategories: (...args: unknown[]) => mockGetCategories(...args),
getUserOrganizations: (...args: unknown[]) =>
mockGetUserOrganizations(...args),
getInventory: (...args: unknown[]) => mockGetInventory(...args),
Expand Down Expand Up @@ -65,6 +66,7 @@ const mockItem = {
describe('Inventory editor mode inline controls', () => {
beforeEach(() => {
jest.resetAllMocks();
mockGetCategories.mockResolvedValue([]);
mockGetInventory.mockResolvedValue({
items: [mockItem],
total: 1,
Expand Down Expand Up @@ -385,6 +387,11 @@ describe('Inventory editor mode inline controls', () => {
});

it('hides the add button for view-only org users but still shows org inventory', async () => {
const orgExclusiveItem = {
...mockItem,
id: 'org-item-view',
itemName: 'Org Exclusive Item',
};
mockGetUserOrganizations.mockResolvedValue([
{
id: 1,
Expand All @@ -395,6 +402,12 @@ describe('Inventory editor mode inline controls', () => {
},
]);
mockGetUserPermissions.mockResolvedValue(['can_view_org_inventory']);
mockGetOrgInventory.mockResolvedValue({
items: [orgExclusiveItem],
total: 1,
limit: 25,
offset: 0,
});

render(
<MemoryRouter initialEntries={['/inventory']}>
Expand All @@ -411,9 +424,10 @@ describe('Inventory editor mode inline controls', () => {
fireEvent.click(await screen.findByText('Test Org'));

await waitFor(() =>
expect(screen.getByText('Test Org')).toBeInTheDocument(),
expect(screen.getByText('Org Exclusive Item')).toBeInTheDocument(),
);

expect(mockGetOrgInventory).toHaveBeenCalledWith(42, expect.any(Object));
expect(
screen.queryByRole('button', { name: 'Add org item' }),
).not.toBeInTheDocument();
Expand Down
10 changes: 10 additions & 0 deletions frontend/tsconfig.jest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "node",
"allowImportingTsExtensions": false,
"noEmit": false,
"jsx": "react-jsx"
}
}
Loading
Loading