import React, {useState} from 'react';
import renderer from 'react-test-renderer';
import {useForm, FormContext} from 'react-hook-form';
import ShippingAddressSelect from '../components/user_summary/shipping_address/ShippingAddressSelect';
import Countries from './mock/Countries.json';
function ComponentWrapper({required = true}) {
const [currentOption, setCurrentOption] = useState(Countries[1]);
const formHelpers = useForm();
return (
);
}
const component = renderer.create();
function getCurrentOption() {
return component.root.findByProps({
className: 'collapsible-text'
}).children[0];
}
describe('ShippingAddressSelect', () => {
test('matches snapshot', () => {
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
describe('is accessible', () => {
const div = component.root.findByProps({
className: 'ShippingAddressForm__input mo-input mo-input__select'
});
test('arrow down changes to next option', () => {
renderer.act(() =>
div.props.onKeyDownCapture(new KeyboardEvent(null, {key: 'ArrowDown'}))
);
expect(getCurrentOption()).toEqual(Countries[2].label);
});
test('arrow up changes to previous option', () => {
renderer.act(() =>
div.props.onKeyDownCapture(new KeyboardEvent(null, {key: 'ArrowUp'}))
);
expect(getCurrentOption()).toEqual(Countries[1].label);
});
test('home key changes to first option', () => {
renderer.act(() =>
div.props.onKeyDownCapture(new KeyboardEvent(null, {key: 'Home'}))
);
expect(getCurrentOption()).toEqual(Countries[0].label);
});
test('end key changes to last option', () => {
renderer.act(() =>
div.props.onKeyDownCapture(new KeyboardEvent(null, {key: 'End'}))
);
expect(getCurrentOption()).toEqual(Countries[Countries.length - 1].label);
});
});
});