Skip to content

Commit

Permalink
feat(components): set up react testing
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtatranta committed Jan 24, 2025
1 parent 740f958 commit 9e94a54
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/components/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const baseConfig = require('../../jest.config.base');

module.exports = {
...baseConfig,
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testEnvironment: 'jsdom',
};
1 change: 1 addition & 0 deletions packages/components/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom';
6 changes: 5 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"type-check": "yarn g:tsc --build tsconfig.json",
"type-check:watch": "yarn type-check -- --watch",
"storybook": "storybook dev -p 9003 -c .storybook",
"storybook-build": "storybook build -c .storybook -o .build-storybook"
"storybook-build": "storybook build -c .storybook -o .build-storybook",
"test:unit": "yarn g:jest",
"test-unit:watch": "yarn g:jest -o --watch"
},
"dependencies": {
"@floating-ui/react": "^0.26.9",
Expand Down Expand Up @@ -55,6 +57,8 @@
"@storybook/react": "^7.6.13",
"@storybook/react-webpack5": "^7.6.13",
"@storybook/theming": "^7.6.13",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "14.2.1",
"@trezor/eslint": "workspace:*",
"@types/react": "18.2.79",
"@types/react-date-range": "^1.4.9",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from 'react';

import { render, screen } from '@testing-library/react';

import { VirtualizedList } from '../VirtualizedList';

describe('VirtualizedList', () => {
describe('type tests', () => {
it('should throw type errors because local mock items lack the "height" property', () => {
type MockItem = { id: number; content: string };
const mockItems: MockItem[] = Array.from({ length: 100 }, (_, i) => ({
id: i,
content: `Item ${i}`,
}));

<VirtualizedList
// @ts-expect-error: correct error here, 'height' property not passed
items={mockItems}
listHeight={400}
listMinHeight={400}
// @ts-expect-error: correct error here, 'height' property not passed
renderItem={(item: MockItem) => ({ id: item.id, content: item.content })}
onScrollEnd={() => {}}
/>;
});

it('should render properly with items containing height property', () => {
type MockItemWithHeight = { id: number; content: string; height: number };
const mockItems: MockItemWithHeight[] = Array.from({ length: 100 }, (_, i) => ({
id: i,
content: `Item ${i}`,
height: 40,
}));

<VirtualizedList
items={mockItems}
listHeight={400}
listMinHeight={400}
renderItem={(item: MockItemWithHeight) => (
<div key={item.id} style={{ height: item.height }}>
{item.content}
</div>
)}
onScrollEnd={() => {}}
/>;
});

it('should automatically type the renderItem() parameter according to the passed generic', () => {
type MockItemWithHeight = { id: number; content: string; height: number };
const mockItems: MockItemWithHeight[] = Array.from({ length: 100 }, (_, i) => ({
id: i,
content: `Item ${i}`,
height: 40,
}));

<VirtualizedList
items={mockItems}
listHeight={400}
listMinHeight={400}
renderItem={item => {
const idNumber: number = item.id;

return (
<div key={idNumber} style={{ height: item.height }}>
{item.content}
</div>
);
}}
onScrollEnd={() => {}}
/>;
});
});

describe('smoke tests', () => {
it('renders without crashing with minimal props', () => {
const mockItems = Array.from({ length: 3 }, (_, i) => ({
id: i,
content: `Test Item ${i}`,
height: 40,
}));

render(
<VirtualizedList
items={mockItems}
listHeight={200}
listMinHeight={200}
renderItem={item => (
<div key={item.id} data-testid={`item-${item.id}`}>
{item.content}
</div>
)}
onScrollEnd={() => {}}
/>,
);

// Verify the first item is rendered
expect(screen.getByTestId('item-0')).toBeTruthy();
expect(screen.getByText('Test Item 0')).toBeTruthy();
});

it('renders empty list without crashing', () => {
const { container } = render(
<VirtualizedList
items={[]}
listHeight={200}
listMinHeight={200}
renderItem={() => null}
onScrollEnd={() => {}}
/>,
);

expect(container.firstChild).toBeTruthy();
expect(container.firstChild?.textContent).toBe('');
});
});
});
38 changes: 38 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ __metadata:
languageName: node
linkType: hard

"@adobe/css-tools@npm:^4.4.0":
version: 4.4.1
resolution: "@adobe/css-tools@npm:4.4.1"
checksum: 10/a0ea05517308593a52728936a833b1075c4cf1a6b68baaea817063f34e75faa1dba1209dd285003c4f8072804227dfa563e7e903f72ae2d39cb520aaee3f4bcc
languageName: node
linkType: hard

"@adraffy/ens-normalize@npm:^1.8.8":
version: 1.11.0
resolution: "@adraffy/ens-normalize@npm:1.11.0"
Expand Down Expand Up @@ -11468,6 +11475,21 @@ __metadata:
languageName: node
linkType: hard

"@testing-library/jest-dom@npm:^6.6.3":
version: 6.6.3
resolution: "@testing-library/jest-dom@npm:6.6.3"
dependencies:
"@adobe/css-tools": "npm:^4.4.0"
aria-query: "npm:^5.0.0"
chalk: "npm:^3.0.0"
css.escape: "npm:^1.5.1"
dom-accessibility-api: "npm:^0.6.3"
lodash: "npm:^4.17.21"
redent: "npm:^3.0.0"
checksum: 10/1f3427e45870eab9dcc59d6504b780d4a595062fe1687762ae6e67d06a70bf439b40ab64cf58cbace6293a99e3764d4647fdc8300a633b721764f5ce39dade18
languageName: node
linkType: hard

"@testing-library/react-native@npm:13.0.0":
version: 13.0.0
resolution: "@testing-library/react-native@npm:13.0.0"
Expand Down Expand Up @@ -11686,6 +11708,8 @@ __metadata:
"@suite-common/suite-constants": "workspace:*"
"@suite-common/suite-utils": "workspace:*"
"@suite-common/validators": "workspace:*"
"@testing-library/jest-dom": "npm:^6.6.3"
"@testing-library/react": "npm:14.2.1"
"@trezor/asset-utils": "workspace:*"
"@trezor/connect": "workspace:*"
"@trezor/dom-utils": "workspace:*"
Expand Down Expand Up @@ -19298,6 +19322,13 @@ __metadata:
languageName: node
linkType: hard

"css.escape@npm:^1.5.1":
version: 1.5.1
resolution: "css.escape@npm:1.5.1"
checksum: 10/f6d38088d870a961794a2580b2b2af1027731bb43261cfdce14f19238a88664b351cc8978abc20f06cc6bbde725699dec8deb6fe9816b139fc3f2af28719e774
languageName: node
linkType: hard

"cssesc@npm:^3.0.0":
version: 3.0.0
resolution: "cssesc@npm:3.0.0"
Expand Down Expand Up @@ -20764,6 +20795,13 @@ __metadata:
languageName: node
linkType: hard

"dom-accessibility-api@npm:^0.6.3":
version: 0.6.3
resolution: "dom-accessibility-api@npm:0.6.3"
checksum: 10/83d3371f8226487fbad36e160d44f1d9017fb26d46faba6a06fcad15f34633fc827b8c3e99d49f71d5f3253d866e2131826866fd0a3c86626f8eccfc361881ff
languageName: node
linkType: hard

"dom-converter@npm:^0.2.0":
version: 0.2.0
resolution: "dom-converter@npm:0.2.0"
Expand Down

0 comments on commit 9e94a54

Please sign in to comment.