import '@testing-library/jest-dom';
import { render, screen, waitFor } from '@testing-library/vue';
import {
TolgeeProvider,
T,
TolgeeInstance,
Tolgee,
DevTools,
VueTolgee,
} from '..';
import { FormatIcu } from '@tolgee/format-icu';
import { mockCoreFetch } from '@tolgee/testing/fetchMock';
const API_URL = 'http://localhost';
const API_KEY = 'dummyApiKey';
const fetch = mockCoreFetch();
const TestComponent = {
components: { T },
template: `
`,
};
const WrapperComponent = {
components: { TestComponent, TolgeeProvider },
template: `
`,
props: ['tolgee'],
};
describe('T component integration', () => {
let tolgee: TolgeeInstance;
beforeEach(async () => {
fetch.enableMocks();
tolgee = Tolgee().use(DevTools()).use(FormatIcu()).init({
apiUrl: API_URL,
apiKey: API_KEY,
language: 'cs',
fallbackLanguage: 'en',
});
render(WrapperComponent, {
props: {
tolgee,
fallback: 'Loading...',
},
global: { plugins: [[VueTolgee, { tolgee }]] },
});
await waitFor(() => {
expect(screen.queryByTestId('hello_world').innerHTML).toContain(
'Ahoj světe!'
);
});
});
it('wraps translation correctly', async () => {
expect(screen.queryByTestId('hello_world').innerHTML).toContain(
'Ahoj světe!'
);
expect(screen.queryByTestId('hello_world')).toHaveAttribute('_tolgee');
});
it('works with no wrap', () => {
expect(screen.queryByTestId('hello_world_no_wrap').innerHTML).toContain(
'Ahoj světe!'
);
expect(screen.queryByTestId('hello_world_no_wrap')).not.toHaveAttribute(
'_tolgee'
);
});
it('works with parameters', () => {
expect(screen.queryByTestId('peter_dogs').innerHTML).toContain(
'Petr má 5 psů.'
);
expect(screen.queryByTestId('peter_dogs')).toHaveAttribute('_tolgee');
});
it('works with default value', async () => {
expect(screen.queryByTestId('non_existant').innerHTML).toContain(
'Non existant'
);
expect(screen.queryByTestId('non_existant')).toHaveAttribute('_tolgee');
});
it('works with language prop', () => {
expect(screen.queryByTestId('with_language_prop')).toContainHTML(
'Hello world!'
);
expect(screen.queryByTestId('with_language_prop')).toHaveAttribute(
'_tolgee'
);
});
it('works with slots', () => {
expect(screen.queryByTestId('peter_dogs_slot').innerHTML).toContain(
'Petr má 5 psů.'
);
expect(screen.queryByTestId('peter_dogs')).toHaveAttribute('_tolgee');
});
describe('language switch', () => {
beforeEach(async () => {
await tolgee.changeLanguage('en');
});
it('changes translation with tags', () => {
expect(screen.queryByTestId('hello_world').innerHTML).toContain(
'Hello world!'
);
expect(screen.queryByTestId('hello_world')).toHaveAttribute('_tolgee');
});
});
});