From e9ccb070e70853aa4fc2acd70d1713ec77aeffb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Zbytovsk=C3=BD?= Date: Mon, 2 Dec 2024 11:35:51 +0100 Subject: [PATCH] add tests --- package.json | 1 + .../FeatureEditSection/OptionsEditor.tsx | 3 +- .../__tests__/useEditItems.test.tsx | 71 +++++++++++++++++++ yarn.lock | 15 ++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/components/FeaturePanel/EditDialog/__tests__/useEditItems.test.tsx diff --git a/package.json b/package.json index d858a17b..f35e3afa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/components/FeaturePanel/EditDialog/EditContent/FeatureEditSection/OptionsEditor.tsx b/src/components/FeaturePanel/EditDialog/EditContent/FeatureEditSection/OptionsEditor.tsx index 41a08651..5fb7b6bc 100644 --- a/src/components/FeaturePanel/EditDialog/EditContent/FeatureEditSection/OptionsEditor.tsx +++ b/src/components/FeaturePanel/EditDialog/EditContent/FeatureEditSection/OptionsEditor.tsx @@ -70,6 +70,7 @@ export const ChangeLocationEditor = () => { export const OptionsEditor = () => { const { isAddPlace, isUndelete } = useEditDialogFeature(); + const { items } = useEditContext(); return ( <> @@ -77,7 +78,7 @@ export const OptionsEditor = () => { <> {t('editdialog.options_heading')} - + {items.length >= 2 ? null : } )} diff --git a/src/components/FeaturePanel/EditDialog/__tests__/useEditItems.test.tsx b/src/components/FeaturePanel/EditDialog/__tests__/useEditItems.test.tsx new file mode 100644 index 00000000..653a3704 --- /dev/null +++ b/src/components/FeaturePanel/EditDialog/__tests__/useEditItems.test.tsx @@ -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); + }); +}); diff --git a/yarn.lock b/yarn.lock index 4a130283..5f002ca7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"