Skip to content

Commit

Permalink
fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
daibhin committed Mar 18, 2024
1 parent 49de8c4 commit a0b7793
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 47 deletions.
63 changes: 19 additions & 44 deletions frontend/src/lib/components/ObjectTags/objectTagsLogic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,43 @@ describe('objectTagsLogic', () => {
props = {
id: 1,
onChange: jest.fn(),
tags: ['a', 'b', 'c'],
}
logic = objectTagsLogic(props)
logic.actions.setTags(['a', 'b', 'c'])
logic.mount()
})

describe('local tags state', () => {
it('initialization', async () => {
await expectLogic(logic).toMatchValues({
tags: ['a', 'b', 'c'],
addingNewTag: false,
newTag: '',
deletedTags: [],
editingTags: false,
})
})
it('handle adding a new tag', async () => {
await expectLogic(logic, async () => {
logic.actions.setNewTag('Nigh')
logic.actions.handleAdd('Nightly')
logic.actions.setEditingTags(true)
logic.actions.setTags(['a', 'b', 'c', 'Nightly'])
})
.toDispatchActions(['setNewTag'])
.toDispatchActions([logic.actionCreators.setTags(['a', 'b', 'c', 'nightly'])])
.toMatchValues({
newTag: 'Nigh',
cleanedNewTag: 'nigh', //user only needs to type part of the tag to find it in a list
})
.toDispatchActions(['handleAdd', logic.actionCreators.setTags(['a', 'b', 'c', 'nightly'])])
.toMatchValues({
tags: ['a', 'b', 'c', 'nightly'],
addingNewTag: false,
newTag: '',
})
// @ts-expect-error
const mockedOnChange = props.onChange?.mock
expect(mockedOnChange.calls.length).toBe(1)
expect(mockedOnChange.calls[0][0]).toBe('nightly')
expect(mockedOnChange.calls[0][1]).toEqual(['a', 'b', 'c', 'nightly'])
})
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)
})
it('handle deleting a tag', async () => {
await expectLogic(logic, async () => {
logic.actions.handleDelete('a')
})
.toDispatchActions(['handleDelete', logic.actionCreators.setTags(['b', 'c'])])
.toMatchValues({
tags: ['b', 'c'],
editingTags: true,
})
// @ts-expect-error
const mockedOnChange = props.onChange?.mock
expect(mockedOnChange.calls.length).toBe(1)
expect(mockedOnChange.calls[0][0]).toBe('a')
expect(mockedOnChange.calls[0][1]).toEqual(['b', 'c'])
expect(mockedOnChange.calls[0]).toEqual(['a', 'b', 'c', 'nightly'])
})
// 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)
// })
})
})
10 changes: 8 additions & 2 deletions frontend/src/lib/lemon-ui/LemonInputSelect/LemonInputSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LemonSkeleton } from 'lib/lemon-ui/LemonSkeleton'
import { LemonSnack } from 'lib/lemon-ui/LemonSnack/LemonSnack'
import { range } from 'lib/utils'
import { range, uniqueBy } from 'lib/utils'
import { useEffect, useMemo, useRef, useState } from 'react'

import { KeyboardShortcut } from '~/layout/navigation-3000/components/KeyboardShortcut'
Expand Down Expand Up @@ -29,6 +29,7 @@ export type LemonInputSelectProps = Pick<
disableFiltering?: boolean
mode: 'multiple' | 'single'
allowCustomValues?: boolean
allowDuplicates?: boolean
onChange?: (newValue: string[]) => void
onBlur?: () => void
onInputChange?: (newValue: string) => void
Expand All @@ -48,6 +49,7 @@ export function LemonInputSelect({
disableFiltering = false,
allowCustomValues = false,
autoFocus = false,
allowDuplicates = true,
...props
}: LemonInputSelectProps): JSX.Element {
const [showPopover, setShowPopover] = useState(false)
Expand All @@ -58,7 +60,7 @@ export function LemonInputSelect({
const values = value ?? []

const visibleOptions = useMemo(() => {
const res: LemonInputSelectOption[] = []
let res: LemonInputSelectOption[] = []
const customValues = [...values]

options.forEach((option) => {
Expand Down Expand Up @@ -88,6 +90,10 @@ export function LemonInputSelect({
res.unshift({ key: inputValue, label: inputValue })
}

if (!allowDuplicates) {
res = uniqueBy(res, (i) => i.key)
}

return res
}, [options, inputValue, value])

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/scenes/insights/InsightPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export function InsightPageHeader({ insightLogicProps }: { insightLogicProps: In
<ObjectTags
tags={insight.tags ?? []}
saving={insightSaving}
onChange={(_, tags) => setInsightMetadata({ tags: tags ?? [] })}
onChange={(tags) => setInsightMetadata({ tags: tags ?? [] })}
tagsAvailable={tags}
className="mt-2"
data-attr="insight-tags"
Expand Down

0 comments on commit a0b7793

Please sign in to comment.