import React from 'react'; import renderer from 'react-test-renderer'; import { fireEvent, render } from '@testing-library/react'; import '@testing-library/jest-dom'; import { EmojiPicker } from './EmojiPicker'; import { Streami18n } from '../i18n/Streami18n'; import { EmojiData, I18n } from 'emoji-mart'; import { Data as EmojiDataSet } from 'emoji-mart/dist-es/utils/data'; import { TranslationContextValue, TranslationProvider } from '../context/TranslationContext'; interface SupportedNimblePickerProps { data: EmojiDataSet; emoji: string; i18n: I18n; onSelect: (emoji: EmojiData) => void; title: string; } jest.mock( 'emoji-mart/dist/components/picker/nimble-picker.js', // eslint-disable-next-line react/display-name () => ({ i18n }: SupportedNimblePickerProps) => { return (
emoji-picker-props
{JSON.stringify(i18n).replace(/\\"/g, '"')}
); }, ); describe('EmojiPicker', () => { it('renders correctly with default props', () => { const tree = renderer.create().toJSON(); expect(tree).toMatchInlineSnapshot(`
`); }); it('open and closes the emoji picker correctly', () => { const { container, getByRole, queryByTestId } = render(); expect(queryByTestId('picker-wrapper')).not.toBeInTheDocument(); fireEvent.click(getByRole('button')); expect(queryByTestId('picker-wrapper')).toBeInTheDocument(); fireEvent.mouseDown(container); expect(queryByTestId('picker-wrapper')).not.toBeInTheDocument(); }); it('provides default i18n strings in english to underlying emoji picker component', () => { const { getByRole, container } = render(); fireEvent.click(getByRole('button')); expect(container.firstChild).toMatchInlineSnapshot(`
emoji-picker-props
{"search":"Search","clear":"Clear","notfound":"No emoji found","skintext":"Choose your default skin tone","categorieslabel":"Emoji categories","categories":{"search":"Search Results","recent":"Frequently Used","people":"Smileys & Emotion","nature":"Animals & Nature","foods":"Food & Drink","activity":"Activity","places":"Travel & Places","objects":"Objects","symbols":"Symbols","flags":"Flags","custom":"Custom"}}
`); }); it('displays default i18n strings in other language than default english - spanish', async () => { const translator = await getTranslator('es'); const { getByRole, container } = render( , ); fireEvent.click(getByRole('button')); expect(container.firstChild).toMatchInlineSnapshot(`
emoji-picker-props
{"search":"Buscar","clear":"Claro","notfound":"No se ha encontrado ningún emoji","skintext":"Elige tu tono de piel por defecto","categorieslabel":"Categorías de emoji","categories":{"search":"Resultados de la búsqueda","recent":"Usado frecuentemente","people":"Sonrisas y emociones","nature":"Animales y naturaleza","foods":"Alimentación y Bebidas","activity":"Actividad","places":"Viajes y lugares","objects":"Objetos","symbols":"Símbolos","flags":"Banderas","custom":"Personalizado"}}
`); }); it('displays custom i18n strings', () => { const { getByRole, container } = render( , ); fireEvent.click(getByRole('button')); expect(container.firstChild).toMatchInlineSnapshot(`
emoji-picker-props
{"search":"Custom Searchh","clear":"Custom Clear","skintext":"Custom Skintext","categories":{"recent":"Custom Recent"}}
`); }); }); async function getTranslator(language: string): Promise { return await new Streami18n({ language }).getTranslators(); }