Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MMT-3926: Fix for Science Keyword Picker Adds An Additional Science Keyword That Was Not Originally Selected #1324

Merged
merged 8 commits into from
Dec 9, 2024
9 changes: 8 additions & 1 deletion static/src/js/components/KeywordPicker/KeywordPicker.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import React, { useEffect, useState } from 'react'
import React, {
useEffect,
useRef,
useState
} from 'react'
import PropTypes from 'prop-types'
import { cloneDeep, isEmpty } from 'lodash-es'
import { Typeahead } from 'react-bootstrap-typeahead'
Expand Down Expand Up @@ -48,6 +52,7 @@ const KeywordPicker = ({

const [marginTop, setMarginTop] = useState(0)
const [marginLeft, setMarginLeft] = useState(0)
const typeaheadRef = useRef(null)

const filterKeywords = (path) => {
const filteredPath = []
Expand Down Expand Up @@ -272,6 +277,7 @@ const KeywordPicker = ({
}

onChange(formData)
typeaheadRef.current.clear()
}

const displayItems = (item) => {
Expand Down Expand Up @@ -427,6 +433,7 @@ const KeywordPicker = ({
<div className="eui-item-list-pane" style={{ marginTop }}>
<div className="keyword-picker__search-keywords">
<Typeahead
ref={typeaheadRef}
clearButton
cgokey marked this conversation as resolved.
Show resolved Hide resolved
id="keyword-picker-search"
isLoading={loading}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React from 'react'

import { test } from 'vitest'
import useControlledKeywords from '../../../hooks/useControlledKeywords'

import parseCmrResponse from '../../../utils/parseCmrResponse'
Expand Down Expand Up @@ -346,24 +347,39 @@ describe('when removing selected keyword', () => {
})

describe('when searching for a keyword', () => {
test('adds the searched keyword', async () => {
const { props, user } = setup()
describe('and user selects a keyword', () => {
test('adds the searched keyword', async () => {
const { props, user } = setup()

const searchBox = screen.getByRole('combobox')

await user.type(searchBox, 'Earth')
const option = screen.getByRole('option', { name: 'EARTH SCIENCE SERVICES>DATA ANALYSIS AND VISUALIZATION>GEOGRAPHIC INFORMATION SYSTEMS>DESKTOP GEOGRAPHIC INFORMATION SYSTEMS' })
await user.click(option)

expect(props.onChange).toHaveBeenCalledTimes(1)
expect(props.onChange).toHaveBeenCalledWith([
{
ToolCategory: 'EARTH SCIENCE SERVICES',
ToolTopic: 'DATA ANALYSIS AND VISUALIZATION',
ToolTerm: 'GEOGRAPHIC INFORMATION SYSTEMS',
ToolSpecificTerm: 'DESKTOP GEOGRAPHIC INFORMATION SYSTEMS'
}
])
})

const searchBox = screen.getByRole('combobox')
test('clears the search box after the selection', async () => {
const { user } = setup()

await user.type(searchBox, 'Earth')
const option = screen.getByRole('option', { name: 'EARTH SCIENCE SERVICES>DATA ANALYSIS AND VISUALIZATION>GEOGRAPHIC INFORMATION SYSTEMS>DESKTOP GEOGRAPHIC INFORMATION SYSTEMS' })
await user.click(option)
const searchBox = screen.getByRole('combobox')

expect(props.onChange).toHaveBeenCalledTimes(1)
expect(props.onChange).not.toHaveBeenCalledWith([
{
ToolCategory: 'EARTH SCIENCE SERVICES',
ToolTopic: 'DATA ANALYSIS AND VISUALIZATION',
ToolTerm: 'CALIBRATION/VALIDATION',
ToolSpecificTerm: undefined
}
])
await user.type(searchBox, 'Earth')

expect(searchBox).toHaveValue('Earth')
const option = screen.getByRole('option', { name: 'EARTH SCIENCE SERVICES>DATA ANALYSIS AND VISUALIZATION>GEOGRAPHIC INFORMATION SYSTEMS>DESKTOP GEOGRAPHIC INFORMATION SYSTEMS' })
await user.click(option)
expect(searchBox).toHaveValue('')
})
})

describe('when keyword recommender is included', () => {
Expand Down
Loading