From 1de44c3e1c03b81e2c8b816dfaf07f65d955f651 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 25 Feb 2025 09:39:09 +0100 Subject: [PATCH 1/5] feat(release): improve typing of itemsApi (WIP) (#148) * Improve typing of itemsApi * Show error if name of new item cannot be determined --------- Co-authored-by: Ulf Gebhardt Co-authored-by: Anton Tranelis <31516529+antontranelis@users.noreply.github.com> --- src/Components/Map/Subcomponents/ItemFormPopup.tsx | 9 ++++++++- src/Components/Map/hooks/useItems.tsx | 1 - src/types/LayerProps.d.ts | 3 +-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Components/Map/Subcomponents/ItemFormPopup.tsx b/src/Components/Map/Subcomponents/ItemFormPopup.tsx index ef191276..5a2ad79d 100644 --- a/src/Components/Map/Subcomponents/ItemFormPopup.tsx +++ b/src/Components/Map/Subcomponents/ItemFormPopup.tsx @@ -53,6 +53,13 @@ export function ItemFormPopup(props: ItemFormPopupProps) { }) formItem.position = { type: 'Point', coordinates: [props.position.lng, props.position.lat] } evt.preventDefault() + + const name = formItem.name ? formItem.name : user?.first_name + if (!name) { + toast.error('Name is must be defined') + return + } + setSpinner(true) formItem.text && @@ -98,8 +105,8 @@ export function ItemFormPopup(props: ItemFormPopupProps) { ;(!props.layer.userProfileLayer || !item) && (await props.layer.api?.createItem!({ ...formItem, + name, id: uuid, - name: formItem.name ? formItem.name : user?.first_name, })) success = true // eslint-disable-next-line no-catch-all/no-catch-all diff --git a/src/Components/Map/hooks/useItems.tsx b/src/Components/Map/hooks/useItems.tsx index bf8779fe..0fb1af78 100644 --- a/src/Components/Map/hooks/useItems.tsx +++ b/src/Components/Map/hooks/useItems.tsx @@ -82,7 +82,6 @@ function useItemsManager(initialItems: Item[]): { }, }) result.map((item) => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment dispatch({ type: 'ADD', item: { ...item, layer } }) return null }) diff --git a/src/types/LayerProps.d.ts b/src/types/LayerProps.d.ts index 44f55a95..9c720bc4 100644 --- a/src/types/LayerProps.d.ts +++ b/src/types/LayerProps.d.ts @@ -18,8 +18,7 @@ export interface LayerProps { markerShape: string markerDefaultColor: string markerDefaultColor2?: string - // eslint-disable-next-line @typescript-eslint/no-explicit-any - api?: ItemsApi + api?: ItemsApi itemType: ItemType userProfileLayer?: boolean customEditLink?: string From c1eafc9a0f9c239bda998be8e9173d0025fdfca3 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 25 Feb 2025 20:21:06 +0100 Subject: [PATCH 2/5] feat(source): test text area input (#155) * unit tests for text area input component --- src/Components/Input/TextAreaInput.spec.tsx | 33 ++++++++++++++++++ src/Components/Input/TextAreaInput.tsx | 12 ------- .../__snapshots__/TextAreaInput.spec.tsx.snap | 34 +++++++++++++++++++ vite.config.ts | 8 ++--- 4 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 src/Components/Input/TextAreaInput.spec.tsx create mode 100644 src/Components/Input/__snapshots__/TextAreaInput.spec.tsx.snap diff --git a/src/Components/Input/TextAreaInput.spec.tsx b/src/Components/Input/TextAreaInput.spec.tsx new file mode 100644 index 00000000..0fe0a40f --- /dev/null +++ b/src/Components/Input/TextAreaInput.spec.tsx @@ -0,0 +1,33 @@ +import { render, screen, fireEvent } from '@testing-library/react' +import { describe, it, expect, beforeEach, vi } from 'vitest' + +import { TextAreaInput } from './TextAreaInput' + +describe('', () => { + let wrapper: ReturnType + + const updateFormValue = vi.fn() + + beforeEach(() => { + vi.clearAllMocks() + wrapper = render() + }) + + it('renders properly', () => { + expect(wrapper.container.firstChild).toMatchSnapshot() + }) + + describe('handleChange', () => { + it('calls updateFormValue with new value', () => { + fireEvent.change(screen.getByRole('textbox'), { target: { value: 'test' } }) + expect(updateFormValue).toBeCalledWith('test') + }) + }) + + describe('labelTitle', () => { + it('sets label', () => { + wrapper.rerender() + expect(wrapper.container.firstChild).toMatchSnapshot() + }) + }) +}) diff --git a/src/Components/Input/TextAreaInput.tsx b/src/Components/Input/TextAreaInput.tsx index 05549a36..660b0990 100644 --- a/src/Components/Input/TextAreaInput.tsx +++ b/src/Components/Input/TextAreaInput.tsx @@ -1,7 +1,5 @@ import { useEffect, useRef, useState } from 'react' -import { useTags } from '#components/Map/hooks/useTags' - interface TextAreaProps { labelTitle?: string labelStyle?: string @@ -14,8 +12,6 @@ interface TextAreaProps { updateFormValue?: (value: string) => void } -type KeyValue = Record - /** * @category Input */ @@ -33,14 +29,6 @@ export function TextAreaInput({ const ref = useRef(null) const [inputValue, setInputValue] = useState(defaultValue) - const tags = useTags() - - const values: KeyValue[] = [] - - tags.forEach((tag) => { - values.push({ key: tag.name, value: tag.name, color: tag.color }) - }) - useEffect(() => { setInputValue(defaultValue) }, [defaultValue]) diff --git a/src/Components/Input/__snapshots__/TextAreaInput.spec.tsx.snap b/src/Components/Input/__snapshots__/TextAreaInput.spec.tsx.snap new file mode 100644 index 00000000..48991cae --- /dev/null +++ b/src/Components/Input/__snapshots__/TextAreaInput.spec.tsx.snap @@ -0,0 +1,34 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[` > labelTitle > sets label 1`] = ` +
+ +