Skip to content

Commit

Permalink
Merge pull request #2464 from SUI-Components/fix/#2463-onblur-not-cal…
Browse files Browse the repository at this point in the history
…led-after-typing

fix(components/molecule/autosuggest): call on blur after typing
  • Loading branch information
alfdocimo authored Nov 15, 2022
2 parents b0d675c + 08f9b12 commit 3207d9d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion components/molecule/autosuggest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ const MoleculeAutosuggest = ({
closeList(ev)
} else {
setFocus(false)
typeof onBlur === 'function' && onBlur()
}
typeof onBlur === 'function' && onBlur()
}
}, 1)
setFocus(true)
Expand Down
39 changes: 38 additions & 1 deletion components/molecule/autosuggest/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import chai, {expect} from 'chai'
import chaiDOM from 'chai-dom'
import sinon from 'sinon'

import {fireEvent} from '@testing-library/react'
import {fireEvent, waitFor} from '@testing-library/react'

import MoleculeDropDownOption from '@s-ui/react-molecule-dropdown-option'

Expand Down Expand Up @@ -418,6 +418,43 @@ describe(json.name, () => {
})
})
})
describe('onBlur', () => {
it('should call onBlur handler when clicking outside', async () => {
const onBlurSpy = sinon.spy()

const props = {
onBlur: onBlurSpy
}

const {getByRole} = setup(props)

const $textBox = getByRole('textbox')

$textBox.focus()
$textBox.blur()

await waitFor(() => expect(sinon.assert.called(onBlurSpy)))
})

it('should call onBlur handler when clicking outside after input a text', async () => {
const onBlurSpy = sinon.spy()

const props = {
onBlur: onBlurSpy
}

const {getByRole} = setup(props)
const $textBox = getByRole('textbox')
const changeEvent = {target: {value: 'asdf'}}

fireEvent.change(getByRole('textbox'), changeEvent)

$textBox.focus()
$textBox.blur()

await waitFor(() => expect(sinon.assert.called(onBlurSpy)))
})
})
})
})

Expand Down

0 comments on commit 3207d9d

Please sign in to comment.