From 448ba37fa249b1c76a4994d128ab758aee93f593 Mon Sep 17 00:00:00 2001 From: David Newell Date: Mon, 18 Mar 2024 16:23:35 +0000 Subject: [PATCH] fix tests --- .../ObjectTags/objectTagsLogic.test.ts | 31 ++++++++----------- .../components/ObjectTags/objectTagsLogic.ts | 3 +- .../LemonInputSelect/LemonInputSelect.tsx | 10 ++---- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/frontend/src/lib/components/ObjectTags/objectTagsLogic.test.ts b/frontend/src/lib/components/ObjectTags/objectTagsLogic.test.ts index 8602e5d7b0b42..5a4b0eebba7aa 100644 --- a/frontend/src/lib/components/ObjectTags/objectTagsLogic.test.ts +++ b/frontend/src/lib/components/ObjectTags/objectTagsLogic.test.ts @@ -24,31 +24,26 @@ describe('objectTagsLogic', () => { editingTags: false, }) }) - it('handle adding a new tag', async () => { + it('cleans new tags', async () => { await expectLogic(logic, async () => { logic.actions.setEditingTags(true) logic.actions.setTags(['a', 'b', 'c', 'Nightly']) + }).toMatchValues({ + editingTags: true, }) - .toDispatchActions([logic.actionCreators.setTags(['a', 'b', 'c', 'nightly'])]) - .toMatchValues({ - editingTags: true, - }) // @ts-expect-error const mockedOnChange = props.onChange?.mock expect(mockedOnChange.calls.length).toBe(1) - expect(mockedOnChange.calls[0]).toEqual(['a', 'b', 'c', 'nightly']) + expect(mockedOnChange.calls[0][0]).toEqual(['a', 'b', 'c', 'nightly']) + }) + it('removes duplicate tags', async () => { + await expectLogic(logic, async () => { + logic.actions.setTags(['a', 'nightly', 'b', 'c', 'nightly']) + }) + // @ts-expect-error + const mockedOnChange = props.onChange?.mock + expect(mockedOnChange.calls.length).toBe(1) + expect(mockedOnChange.calls[0][0]).toEqual(['a', 'nightly', 'b', 'c']) }) - // it('noop on duplicate tag', async () => { - // await expectLogic(logic, async () => { - // logic.actions.handleAdd('a') - // }) - // .toDispatchActions(['handleAdd']) - // .toNotHaveDispatchedActions(['setTags']) - // .toMatchValues({ - // tags: ['a', 'b', 'c'], - // }) - // // @ts-expect-error - // expect(props.onChange?.mock.calls.length).toBe(0) - // }) }) }) diff --git a/frontend/src/lib/components/ObjectTags/objectTagsLogic.ts b/frontend/src/lib/components/ObjectTags/objectTagsLogic.ts index 82cd8b83897d9..b9d60d6bc156c 100644 --- a/frontend/src/lib/components/ObjectTags/objectTagsLogic.ts +++ b/frontend/src/lib/components/ObjectTags/objectTagsLogic.ts @@ -1,4 +1,5 @@ import { actions, kea, key, listeners, path, props, reducers } from 'kea' +import { uniqueBy } from 'lib/utils' import type { objectTagsLogicType } from './objectTagsLogicType' @@ -32,7 +33,7 @@ export const objectTagsLogic = kea([ })), listeners(({ props }) => ({ setTags: ({ tags }) => { - const nextTags = tags.map(cleanTag) + const nextTags = uniqueBy(tags.map(cleanTag), (i) => i) props.onChange?.(nextTags) }, })), diff --git a/frontend/src/lib/lemon-ui/LemonInputSelect/LemonInputSelect.tsx b/frontend/src/lib/lemon-ui/LemonInputSelect/LemonInputSelect.tsx index e7561c435786b..4a9ec5e469cd1 100644 --- a/frontend/src/lib/lemon-ui/LemonInputSelect/LemonInputSelect.tsx +++ b/frontend/src/lib/lemon-ui/LemonInputSelect/LemonInputSelect.tsx @@ -1,6 +1,6 @@ import { LemonSkeleton } from 'lib/lemon-ui/LemonSkeleton' import { LemonSnack } from 'lib/lemon-ui/LemonSnack/LemonSnack' -import { range, uniqueBy } from 'lib/utils' +import { range } from 'lib/utils' import { useEffect, useMemo, useRef, useState } from 'react' import { KeyboardShortcut } from '~/layout/navigation-3000/components/KeyboardShortcut' @@ -29,7 +29,6 @@ export type LemonInputSelectProps = Pick< disableFiltering?: boolean mode: 'multiple' | 'single' allowCustomValues?: boolean - allowDuplicates?: boolean onChange?: (newValue: string[]) => void onBlur?: () => void onInputChange?: (newValue: string) => void @@ -49,7 +48,6 @@ export function LemonInputSelect({ disableFiltering = false, allowCustomValues = false, autoFocus = false, - allowDuplicates = true, ...props }: LemonInputSelectProps): JSX.Element { const [showPopover, setShowPopover] = useState(false) @@ -60,7 +58,7 @@ export function LemonInputSelect({ const values = value ?? [] const visibleOptions = useMemo(() => { - let res: LemonInputSelectOption[] = [] + const res: LemonInputSelectOption[] = [] const customValues = [...values] options.forEach((option) => { @@ -90,10 +88,6 @@ export function LemonInputSelect({ res.unshift({ key: inputValue, label: inputValue }) } - if (!allowDuplicates) { - res = uniqueBy(res, (i) => i.key) - } - return res }, [options, inputValue, value])