Skip to content

Commit

Permalink
chore: fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Oct 17, 2024
1 parent e9a1d19 commit 68a5d57
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ When('the window is scrolled down', () => {
})

Then('the top of the menu is aligned with the bottom of the input', () => {
const selectDataTest = '[data-test="dhis2-uicore-singleselect"]'
const menuDataTest = '[data-test="dhis2-uicore-select-menu-menuwrapper"]'

cy.all(
() => cy.findByRole('combobox'),
() => cy.findByRole('listbox')
Expand All @@ -55,9 +52,6 @@ Then('the top of the menu is aligned with the bottom of the input', () => {
})

Then('the bottom of the menu is aligned with the top of the input', () => {
const selectDataTest = '[data-test="dhis2-uicore-singleselect"]'
const menuDataTest = '[data-test="dhis2-uicore-select-menu-menuwrapper"]'

cy.all(
() => cy.findByRole('combobox'),
// We need to get the parent as the menu itself
Expand All @@ -78,9 +72,6 @@ Then('the bottom of the menu is aligned with the top of the input', () => {
})

Then('it is rendered on top of the SingleSelect', () => {
const selectDataTest = '[data-test="dhis2-uicore-singleselect"]'
const menuDataTest = '[data-test="dhis2-uicore-select-menu-menuwrapper"]'

cy.all(
() => cy.findByRole('combobox'),
() => cy.findByRole('listbox')
Expand All @@ -102,9 +93,6 @@ Then('it is rendered on top of the SingleSelect', () => {
Then(
'the left of the SingleSelect is aligned with the left of the anchor',
() => {
const selectDataTest = '[data-test="dhis2-uicore-singleselect"]'
const menuDataTest = '[data-test="dhis2-uicore-select-menu-menuwrapper"]'

cy.all(
() => cy.findByRole('combobox'),
() => cy.findByRole('listbox')
Expand Down
1 change: 0 additions & 1 deletion components/select/src/single-select-a11y/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export function Option({
dataTest,
disabled,
highlighted,
selected,
...rest
}) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PropTypes from 'prop-types'
import React from 'react'
import i18n from '../locales/index.js'

export const ClearButton = ({ onClear, clearText, dataTest}) => (
export const ClearButton = ({ onClear, clearText, dataTest }) => (
<button
aria-label={i18n.t('{{clearText}}', { clearText })}
data-test={dataTest}
Expand Down Expand Up @@ -60,7 +60,13 @@ ClearButton.propTypes = {
}

export const SelectedValueClearButton = ({ onClear, clearText, dataTest }) => {
const clearButton = <ClearButton onClear={onClear} dataTest={dataTest} clearText={clearText} />
const clearButton = (
<ClearButton
onClear={onClear}
dataTest={dataTest}
clearText={clearText}
/>
)

if (!clearText) {
return clearButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const SelectedValueContainer = forwardRef(function Container(
onFocus={onFocus}
onBlur={onBlur}
onClick={onClick}
onKeyDown={e => {
onKeyDown={(e) => {
e.stopPropagation()
onKeyPress(e)
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useState } from 'react'
import React from 'react'
import { SingleSelectA11y } from './single-select-a11y.js'

export default {
Expand Down
33 changes: 18 additions & 15 deletions components/select/src/single-select-a11y/single-select-a11y.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,20 @@ export function SingleSelectA11y({
// Non-stateful
// ========
const comboBoxId = `${idPrefix}-combo`
const valueLabel = _valueLabel || options.find(option => option.value === value)?.label || ''
const valueLabel =
_valueLabel ||
options.find((option) => option.value === value)?.label ||
''

if (
!valueLabel
&& options.length
&& !options.find(option => option.value === '')
&& !placeholder
!valueLabel &&
options.length &&
!options.find((option) => option.value === '') &&
!placeholder
) {
throw new Error('You must either provide a "valueLabel" or include an empty option in the options array')
throw new Error(
'You must either provide a "valueLabel" or include an empty option in the options array'
)
}

// Stateful
Expand All @@ -64,11 +69,9 @@ export function SingleSelectA11y({
// Using `useState` here so components get notified when the value changes (from null -> div)
const comboBoxRef = useRef()
const [focussedOptionIndex, setFocussedOptionIndex] = useState(() => {
const foundIndex = options.findIndex(option => option.value === value)
const foundIndex = options.findIndex((option) => option.value === value)

return foundIndex !== -1
? foundIndex
: 0
return foundIndex !== -1 ? foundIndex : 0
})
const [selectRef, setSelectRef] = useState()
const [expanded, setExpanded] = useState(false)
Expand All @@ -80,15 +83,15 @@ export function SingleSelectA11y({
} else {
openMenu()
}
}, [expanded])
}, [expanded, openMenu, closeMenu])

const selectFocussedOption = useCallback(() => {
const option = options[focussedOptionIndex]

if (option) {
onChange(option.value)
}
}, [focussedOptionIndex, options])
}, [focussedOptionIndex, options, onChange])

const handleKeyPress = useHandleKeyPress({
value,
Expand Down Expand Up @@ -190,12 +193,12 @@ SingleSelectA11y.propTypes = {
/** An array of options **/
options: optionsProp.isRequired,

/** A callback that will be called with the new value or an empty string **/
onChange: PropTypes.func.isRequired,

/** As of now, this component does not support being uncontrolled */
value: PropTypes.string.isRequired,

/** A callback that will be called with the new value or an empty string **/
onChange: PropTypes.func.isRequired,

/** Will focus the select initially **/
autoFocus: PropTypes.bool,

Expand Down
69 changes: 44 additions & 25 deletions components/select/src/single-select-a11y/single-select-a11y.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ describe('<SingleSelectA11y />', () => {
fireEvent.blur(screen.getByRole('combobox'))

// first argument passed to onBlur is a react event
//
// Reg. eslint: Eslint complains about using hasOwnProperty,
// but accessing `.nativeEvent` directly causes react to log an error
// eslint-disable-next-line no-prototype-builtins
expect(onBlur.mock.calls[0][0].hasOwnProperty('nativeEvent')).toBe(true)
expect(onBlur).toHaveBeenCalledTimes(1)
})
Expand All @@ -40,7 +44,13 @@ describe('<SingleSelectA11y />', () => {
fireEvent.focus(screen.getByRole('combobox'))

// first argument passed to onFocus is a react event
expect(onFocus.mock.calls[0][0].hasOwnProperty('nativeEvent')).toBe(true)
//
// Reg. eslint: Eslint complains about using hasOwnProperty,
// but accessing `.nativeEvent` directly causes react to log an error
// eslint-disable-next-line no-prototype-builtins
expect(onFocus.mock.calls[0][0].hasOwnProperty('nativeEvent')).toBe(
true
)
expect(onFocus).toHaveBeenCalledTimes(1)
})

Expand Down Expand Up @@ -130,8 +140,11 @@ describe('<SingleSelectA11y />', () => {
)

const placeholder = screen.getByText('Placeholder text')
const dataTestValue = placeholder.attributes.getNamedItem('data-test').value
expect(dataTestValue).toBe('dhis2-singleselecta11y-selectedvalue-placeholder')
const dataTestValue =
placeholder.attributes.getNamedItem('data-test').value
expect(dataTestValue).toBe(
'dhis2-singleselecta11y-selectedvalue-placeholder'
)
})

it('should accept a prefix', () => {
Expand All @@ -146,8 +159,11 @@ describe('<SingleSelectA11y />', () => {
)

const placeholder = screen.getByText('Prefix text')
const dataTestValue = placeholder.attributes.getNamedItem('data-test').value
expect(dataTestValue).toBe('dhis2-singleselecta11y-selectedvalue-prefix')
const dataTestValue =
placeholder.attributes.getNamedItem('data-test').value
expect(dataTestValue).toBe(
'dhis2-singleselecta11y-selectedvalue-prefix'
)
})

it('should allow options to be selected', () => {
Expand All @@ -174,15 +190,15 @@ describe('<SingleSelectA11y />', () => {
fireEvent.click(option)

expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledWith("foo")
expect(onChange).toHaveBeenCalledWith('foo')
})

it('should allow custom options to be selected', () => {
const onChange = jest.fn()

// eslint-disable-next-line react/prop-types
const CustomOption = ({ value, label }) => (
<span data-test={`custom-option-${value}`}>
{label}
</span>
<span data-test={`custom-option-${value}`}>{label}</span>
)

render(
Expand All @@ -207,7 +223,7 @@ describe('<SingleSelectA11y />', () => {
fireEvent.click(customOption)

expect(onChange).toHaveBeenCalledTimes(1)
expect(onChange).toHaveBeenCalledWith("foo")
expect(onChange).toHaveBeenCalledWith('foo')
})

it('should be clearable when there is a selected value', () => {
Expand Down Expand Up @@ -317,7 +333,7 @@ describe('<SingleSelectA11y />', () => {
expect(searchInput).toBeInstanceOf(HTMLInputElement)
expect(searchInput).toBeVisible()

fireEvent.change(searchInput, { target: { value: 'Search term'} })
fireEvent.change(searchInput, { target: { value: 'Search term' } })

expect(onFilterChange).toHaveBeenCalledTimes(1)
expect(onFilterChange).toHaveBeenCalledWith('Search term')
Expand All @@ -336,7 +352,10 @@ describe('<SingleSelectA11y />', () => {
value=""
valueLabel=""
onChange={jest.fn()}
options={[{ value: '', label: 'None' }, { value: 'foo', label: 'Foo' }]}
options={[
{ value: '', label: 'None' },
{ value: 'foo', label: 'Foo' },
]}
/>
)

Expand Down Expand Up @@ -372,7 +391,9 @@ describe('<SingleSelectA11y />', () => {
// Is this because of unnecessary re-renders?
expect(consoleError).toHaveBeenNthCalledWith(
1,
expect.stringContaining('Encountered two children with the same key'),
expect.stringContaining(
'Encountered two children with the same key'
),
'foo',
expect.anything()
)
Expand Down Expand Up @@ -404,14 +425,13 @@ describe('<SingleSelectA11y />', () => {
expect(combobox).toContainElement(withTextBar)
})


/**************************
* *
* ===================== *
* Keyboard interactions *
* ===================== *
* *
**************************/
* *
* ===================== *
* Keyboard interactions *
* ===================== *
* *
**************************/

describe.each([
{ key: ' ' },
Expand Down Expand Up @@ -497,12 +517,11 @@ describe('<SingleSelectA11y />', () => {
expect(screen.queryByRole('listbox')).not.toBeNull()

// highlighting the next option
fireEvent.keyDown(screen.getByRole('combobox'), { key: 'ArrowDown' })
fireEvent.keyDown(screen.getByRole('combobox'), {
key: 'ArrowDown',
})

fireEvent.keyDown(
screen.getByRole('combobox'),
keyDownOptions,
)
fireEvent.keyDown(screen.getByRole('combobox'), keyDownOptions)

expect(screen.queryByRole('listbox')).toBeNull()
expect(onChange).toHaveBeenCalledTimes(1)
Expand Down
7 changes: 6 additions & 1 deletion storybook/src/load-stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ exports.loadStories = () => {
case icons.includes(curcomp): {
console.info(`custom => Loading stories for '${curcomp}'`)
return [
path.join(ICONS_DIR, 'src', '**', '*.prod.stories.@(js|jsx|mdx)'),
path.join(
ICONS_DIR,
'src',
'**',
'*.prod.stories.@(js|jsx|mdx)'
),
]
}
case constants.includes(curcomp): {
Expand Down

0 comments on commit 68a5d57

Please sign in to comment.