import * as React from 'react'; import expect from 'expect'; import { render, screen } from '@testing-library/react'; import { useChoices } from './useChoices'; import { TestTranslationProvider } from '../i18n'; import { useRecordContext } from '../controller'; describe('useChoices hook', () => { const defaultProps = { choice: { id: 42, name: 'test' }, optionValue: 'id', optionText: 'name', translateChoice: true, }; const Component = ({ choice, optionText, optionValue, translateChoice, }) => { const { getChoiceText, getChoiceValue } = useChoices({ optionText, optionValue, translateChoice, }); return (
{getChoiceText(choice)}
); }; it('should use optionValue as value identifier', () => { render(); expect(screen.getByText('test').getAttribute('data-value')).toEqual( '42' ); }); it('should use optionText with a string value as text identifier', () => { render(); expect(screen.queryAllByText('test')).toHaveLength(1); }); it('should use optionText with a function value as text identifier', () => { render( choice.foobar} choice={{ id: 42, foobar: 'test' }} /> ); expect(screen.queryAllByText('test')).toHaveLength(1); }); it('should use optionText with an element value as text identifier', () => { const Foobar = () => { const record = useRecordContext(); return {record.foobar}; }; render( } choice={{ id: 42, foobar: 'test' }} /> ); expect(screen.queryAllByText('test')).toHaveLength(1); }); it('should translate the choice by default', () => { render( `**${x}**`}> ); expect(screen.queryAllByText('test')).toHaveLength(0); expect(screen.queryAllByText('**test**')).toHaveLength(1); }); it('should not translate the choice if translateChoice is false', () => { render( `**${x}**`}> ); expect(screen.queryAllByText('test')).toHaveLength(1); expect(screen.queryAllByText('**test**')).toHaveLength(0); }); });