Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Dec 2, 2024
1 parent 43167e4 commit e9ccb07
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"react-zoom-pan-pinch": "^3.6.1"
},
"devDependencies": {
"@testing-library/react-hooks": "^8.0.1",
"@types/autosuggest-highlight": "^3.2.3",
"@types/jest": "^29.5.13",
"@types/js-cookie": "^3.0.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ export const ChangeLocationEditor = () => {

export const OptionsEditor = () => {
const { isAddPlace, isUndelete } = useEditDialogFeature();
const { items } = useEditContext();

return (
<>
{!isAddPlace && !isUndelete && (
<>
<DialogHeading>{t('editdialog.options_heading')}</DialogHeading>
<PlaceCancelledToggle />
<ChangeLocationEditor />
{items.length >= 2 ? null : <ChangeLocationEditor />}
</>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { renderHook } from '@testing-library/react-hooks';
import { useEditItems } from '../useEditItems';
import { Feature } from '../../../../services/types';
// @ts-ignore
import { act } from 'react';

const originalFeature: Feature = {
osmMeta: { type: 'node', id: 1 },
tags: { amenity: 'cafe' },
type: 'Feature',
properties: { class: 'x', subclass: 'y' },
center: [14, 50],
};

describe('useEditItems', () => {
it('should initialize with the original feature', () => {
const { result } = renderHook(() => useEditItems(originalFeature));

expect(result.current.items).toHaveLength(1);
expect(result.current.items[0].shortId).toEqual('n1');
expect(result.current.items[0].tags).toEqual(originalFeature.tags);
});

it('should add a new feature', () => {
const { result } = renderHook(() => useEditItems(originalFeature));

const newFeature: Feature = {
osmMeta: { type: 'node', id: 2 },
tags: { amenity: 'restaurant' },
type: 'Feature',
properties: { class: 'x', subclass: 'y' },
center: [14, 50],
};

act(() => {
result.current.addFeature(newFeature);
});

expect(result.current.items).toHaveLength(2);
expect(result.current.items[1].tags).toEqual(newFeature.tags);
});

it('should update tags of a feature', () => {
const { result } = renderHook(() => useEditItems(originalFeature));

act(() => {
result.current.items[0].setTag('name', 'Test Cafe');
});

expect(result.current.items[0].tags).toEqual({
amenity: 'cafe',
name: 'Test Cafe',
});
});

it('should toggle toBeDeleted flag', () => {
const { result } = renderHook(() => useEditItems(originalFeature));

act(() => {
result.current.items[0].toggleToBeDeleted();
});

expect(result.current.items[0].toBeDeleted).toBe(true);

act(() => {
result.current.items[0].toggleToBeDeleted();
});

expect(result.current.items[0].toBeDeleted).toBe(false);
});
});
15 changes: 15 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,14 @@
resolved "https://registry.yarnpkg.com/@teritorio/openmaptiles-gl-language/-/openmaptiles-gl-language-1.5.4.tgz#4959c78c0e7f9845746dbc414d431cd95655db2f"
integrity sha512-S4kXGBBSMzX4fDrmcUdPSLBNBaIUUPihuQWeaLmNXniJbNsHOppo1NtZXlivzvvl874KCmN7+UWr4q2W2l/JaA==

"@testing-library/react-hooks@^8.0.1":
version "8.0.1"
resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12"
integrity sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==
dependencies:
"@babel/runtime" "^7.12.5"
react-error-boundary "^3.1.0"

"@tootallnate/once@2":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf"
Expand Down Expand Up @@ -6812,6 +6820,13 @@ react-dom@^18.3.1:
loose-envify "^1.1.0"
scheduler "^0.23.2"

react-error-boundary@^3.1.0:
version "3.1.4"
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
dependencies:
"@babel/runtime" "^7.12.5"

react-is@^16.13.1, react-is@^16.7.0, react-is@^16.8.1:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
Expand Down

0 comments on commit e9ccb07

Please sign in to comment.