Skip to content

Commit

Permalink
chore(usephoneinput): increased use phone input coverage (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhthinh388 authored Mar 6, 2024
1 parent 8f6ea12 commit f967bbd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { formatWithFixedCountry } from './formatWithFixedCountry'

describe('formatWithFixedCountry', () => {
it('Should return empty string when phone value is invalid', () => {
expect(formatWithFixedCountry('', 'US')).toBe('')
})

it('Should leave as-is when the value is already has valid country code.', () => {
expect(formatWithFixedCountry('+123456', 'US')).toBe('+123456')
})

it('Should format to the correct country code.', () => {
expect(formatWithFixedCountry('+123456', 'VN')).toBe('+84123456')
expect(formatWithFixedCountry('0123456', 'VN')).toBe('+84123456')
expect(formatWithFixedCountry('123456', 'VN')).toBe('+84123456')
})
})
47 changes: 47 additions & 0 deletions packages/phone-input/src/hooks/usePhoneInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,51 @@ describe('usePhoneInput', () => {
expect(input.getAttribute('value')).toBe('+1 23')
expect(onChange).toHaveBeenCalledTimes(4) // OnChange event shouldn't be triggered since user entered invalid letters.
})

/**
* Fixed country
*/
it('Should disable country detection when country is passed', async () => {
const onChange = vitest.fn()
const { container } = render(<Comp country="VN" onChange={onChange} />)
const input = container.querySelector('input')

if (!input) {
throw new Error('input is not a valid element.')
}

expect(container.querySelector('#VN')).toBeVisible()

await act(async () => {
input.focus()
await user.keyboard('{+},{1},{2},{3}')
})

expect(input.getAttribute('value')).toBe('+84 123')

expect(container.querySelector('#VN')).toBeVisible()
})

it('Should keep passed country and format as national', async () => {
const onChange = vitest.fn()
const { container } = render(
<Comp country="VN" mode="national" onChange={onChange} />,
)
const input = container.querySelector('input')

if (!input) {
throw new Error('input is not a valid element.')
}

expect(container.querySelector('#VN')).toBeVisible()

await act(async () => {
input.focus()
await user.keyboard('{+},{1},{2},{3}')
})

expect(input.getAttribute('value')).toBe('123')

expect(container.querySelector('#VN')).toBeVisible()
})
})

0 comments on commit f967bbd

Please sign in to comment.