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

test: add missing unittests #1768

Draft
wants to merge 5 commits into
base: next
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@
"antd": "^5.21.4",
"color": "^4.2.3",
"dotenv": "^16.4.5",
"geostyler": "^15.0.1",
"geostyler-openlayers-parser": "^5.0.0",
"geostyler-style": "^9.1.0",
"geostyler": "^15.0.1",
"i18next-browser-languagedetector": "^8.0.0",
"i18next": "^23.16.0",
"i18next-browser-languagedetector": "^8.0.0",
"js-md5": "^0.8.3",
"keycloak-js": "^26.0.0",
"normalize.css": "^8.0.1",
"ol": "^10.2.1",
"react": "^18.3.1",
"react-cookie-consent": "^9.0.0",
"react-dom": "^18.3.1",
"react-i18next": "^15.0.3",
"react-redux": "^9.1.2",
"react": "^18.3.1",
"shapefile.js": "^1.1.4"
},
"devDependencies": {
Expand All @@ -91,8 +91,8 @@
"@semantic-release/release-notes-generator": "^14.0.1",
"@stylistic/eslint-plugin": "^2.9.0",
"@swc/helpers": "^0.5.13",
"@terrestris/eslint-config-typescript-react": "^3.0.0",
"@terrestris/eslint-config-typescript": "^7.0.0",
"@terrestris/eslint-config-typescript-react": "^3.0.0",
"@testing-library/jest-dom": "^6.6.1",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
Expand All @@ -107,25 +107,26 @@
"chokidar": "^4.0.1",
"copy-webpack-plugin": "^12.0.2",
"css-loader": "^7.1.2",
"eslint": "^9.12.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-react-refresh": "^0.4.12",
"eslint-plugin-react": "^7.37.1",
"eslint": "^9.12.0",
"eslint-plugin-react-refresh": "^0.4.12",
"fs-extra": "^11.2.0",
"globals": "^15.11.0",
"husky": "^9.1.6",
"ignore-loader": "^0.1.2",
"jest-environment-jsdom": "^29.7.0",
"jest": "^29.7.0",
"less-loader": "^12.2.0",
"jest-canvas-mock": "^2.5.2",
"jest-environment-jsdom": "^29.7.0",
"less": "^4.2.0",
"less-loader": "^12.2.0",
"mini-css-extract-plugin": "^2.9.1",
"path-exists-cli": "2.0.0",
"react-refresh": "^0.14.2",
"semantic-release": "^24.1.2",
"style-loader": "^4.0.0",
"typescript-eslint": "^8.9.0",
"typescript": "^5.6.3",
"typescript-eslint": "^8.9.0",
"webpack-merge": "^6.0.1"
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React from 'react';

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

import { Form } from 'antd';

import AttributionRow from './index';

describe('<AttributionRow />', () => {
it('is defined', () => {
expect(AttributionRow).not.toBeUndefined();
});

it('can be rendered with placeholders and options', async () => {
render(
<Form>
<AttributionRow
keyName="testKey"
options={['Option1', 'Option2']}
/>
</Form>
);

expect(screen.getByText('AttributionRow.keyPlaceholder')).toBeInTheDocument();
expect(screen.getByPlaceholderText('AttributionRow.valuePlaceholder')).toBeInTheDocument();

const autoComplete = document.querySelector('#field1_name');

if (!autoComplete) {
return;
};

expect(within(autoComplete.parentElement!).getByText('AttributionRow.keyPlaceholder')).toBeInTheDocument();

const input = screen.getByPlaceholderText('AttributionRow.valuePlaceholder');
expect(input).toBeInTheDocument();

fireEvent.focus(autoComplete);
expect(screen.getByText('Option1')).toBeInTheDocument();
expect(screen.getByText('Option2')).toBeInTheDocument();
});

it('calls onChange when Input value changes', () => {
const handleChange = jest.fn();

render(
<Form>
<AttributionRow
keyName="testKey"
onChange={handleChange}
/>
</Form>
);

const input = screen.getByPlaceholderText('AttributionRow.valuePlaceholder');
fireEvent.change(input, { target: { value: 'TestValue' } });

expect(handleChange).toHaveBeenCalledWith('TestValue');
});

it('validates the required fields', async () => {
render(
<Form>
<AttributionRow keyName="testKey" />
</Form>
);

const autoComplete = document.querySelector('#field1_name');
const input = screen.getByPlaceholderText('AttributionRow.valuePlaceholder');

if (!autoComplete) {
return;
};

fireEvent.blur(autoComplete);
fireEvent.blur(input);

expect(await screen.findByText('AttributionRow.missingKey')).toBeInTheDocument();
expect(await screen.findByText('AttributionRow.missingValue')).toBeInTheDocument();
});

it('validates unique keys', async () => {
const mockFormData = {
fields: {
field1: { name: 'DuplicateKey' },
field2: { name: 'DuplicateKey' }
}
};

render(
<Form initialValues={mockFormData}>
<AttributionRow keyName="field1" />
</Form>
);

const autoComplete = document.querySelector('#field1_name');

if (!autoComplete) {
return;
};

fireEvent.change(autoComplete, { target: { value: 'DuplicateKey' } });
fireEvent.blur(autoComplete);

expect(await screen.findByText('AttributionRow.keyInUse')).toBeInTheDocument();
});
});
Loading
Loading