import React, { act } from 'react'; import '@testing-library/jest-dom'; import { render, screen, waitFor } from '@testing-library/react'; import { TolgeeProvider, DevTools, TolgeeInstance, Tolgee } from '../index'; import { FormatIcu } from '@tolgee/format-icu'; import { mockCoreFetch } from '@tolgee/testing/fetchMock'; import { T } from '../index'; const API_URL = 'http://localhost'; const API_KEY = 'dummyApiKey'; const fetch = mockCoreFetch(); describe('T component integration', () => { let tolgee: TolgeeInstance; const TestComponent = () => { return ( <>
hello_world
hello_world
Non existant
, i: }} />
}} defaultValue="Here is empty

tag" />
should, }} defaultValue="This shouldn't be here" />
{text}, }} />
world }} defaultValue="Hello {name}" />
}} defaultValue="Before{icon}After" />
); }; beforeEach(async () => { fetch.enableMocks(); tolgee = Tolgee().use(DevTools()).use(FormatIcu()).init({ apiUrl: API_URL, apiKey: API_KEY, language: 'cs', fallbackLanguage: 'en', }); act(() => { render( ); }); await waitFor(() => { expect(screen.queryByTestId('hello_world')?.textContent).toContain( 'Ahoj světe!' ); expect(screen.queryByTestId('hello_world')).toHaveAttribute( '_tolgee', 'true' ); }); }); it('wraps translation correctly', async () => { expect(screen.queryByTestId('hello_world')).toContainHTML('Ahoj světe!'); expect(screen.queryByTestId('hello_world')).toHaveAttribute('_tolgee'); }); it('works with no wrap', () => { expect(screen.queryByTestId('hello_world_no_wrap')).toContainHTML( 'Ahoj světe!' ); expect(screen.queryByTestId('hello_world_no_wrap')).not.toHaveAttribute( '_tolgee' ); }); it('works with parameters', () => { expect(screen.queryByTestId('peter_dogs')).toContainHTML('Petr má 5 psů.'); expect(screen.queryByTestId('peter_dogs')).toHaveAttribute('_tolgee'); }); it('works with default value', async () => { expect(screen.queryByTestId('non_existant')).toContainHTML('Non existant'); expect(screen.queryByTestId('non_existant')).toHaveAttribute('_tolgee'); }); it('works with default value in params', async () => { expect(screen.queryByTestId('default_value_in_params')).toContainHTML( 'Non existant' ); expect(screen.queryByTestId('default_value_in_params')).toHaveAttribute( '_tolgee' ); }); it('works with tags', () => { expect(screen.queryByTestId('with_tags')).toContainHTML( 'Tento text je formátovaný' ); expect(screen.queryByTestId('with_tags')).toHaveAttribute('_tolgee'); }); it('works with empty tag', () => { expect(screen.queryByTestId('with_empty_tag')).toContainHTML( 'Here is empty
tag' ); expect(screen.queryByTestId('with_empty_tag')).toHaveAttribute('_tolgee'); }); it("won't pass children if the element already has children", () => { expect(screen.queryByTestId('with_children_conflict')).toContainHTML( 'This should be here' ); expect(screen.queryByTestId('with_children_conflict')).toHaveAttribute( '_tolgee' ); }); it('works with tag as function', () => { expect(screen.queryByTestId('with_tag')).toContainHTML( 'bold' ); expect(screen.queryByTestId('with_tag')).toHaveAttribute('_tolgee'); }); it('interpolates react element with {placeholder} syntax', () => { expect(screen.queryByTestId('element_placeholder')).toContainHTML( 'Hello world' ); }); it('interpolates self-closing react element with {placeholder} syntax', () => { expect( screen.queryByTestId('element_placeholder_self_closing') ).toContainHTML('Before
After'); }); it('works with language prop', () => { expect(screen.queryByTestId('with_language_prop')).toContainHTML( 'Hello world!' ); expect(screen.queryByTestId('with_language_prop')).toHaveAttribute( '_tolgee' ); }); describe('language switch', () => { beforeEach(async () => { await act(async () => { await tolgee.changeLanguage('en'); }); }); it('changes translation with tags', () => { expect(screen.queryByTestId('with_tags')).toContainHTML( 'This text is formatted' ); expect(screen.queryByTestId('with_tags')).toHaveAttribute('_tolgee'); }); }); });