| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {render, screen} from '@testing-library/react'; |
| b69ab31 | | | 9 | import {I18nSupport, t, T} from '../i18n'; |
| b69ab31 | | | 10 | import '../i18n/en/common.json'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | jest.mock( |
| b69ab31 | | | 13 | '../i18n/en/common.json', |
| b69ab31 | | | 14 | () => ({ |
| b69ab31 | | | 15 | translate_me: 'this was translated', |
| b69ab31 | | | 16 | plural_one: 'There is one apple', |
| b69ab31 | | | 17 | plural_other: 'There are $count apples', |
| b69ab31 | | | 18 | replace_and_plural_one: 'There is one $type apple', |
| b69ab31 | | | 19 | replace_and_plural_other: 'There are $count $type apples', |
| b69ab31 | | | 20 | }), |
| b69ab31 | | | 21 | {virtual: true}, |
| b69ab31 | | | 22 | ); |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | describe('i18n', () => { |
| b69ab31 | | | 25 | describe('<T>', () => { |
| b69ab31 | | | 26 | it('can render translations', () => { |
| b69ab31 | | | 27 | render( |
| b69ab31 | | | 28 | <I18nSupport> |
| b69ab31 | | | 29 | <T>translate_me</T> |
| b69ab31 | | | 30 | </I18nSupport>, |
| b69ab31 | | | 31 | ); |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | expect(screen.getByText('this was translated')).toBeInTheDocument(); |
| b69ab31 | | | 34 | expect(screen.queryByText('translate_me')).not.toBeInTheDocument(); |
| b69ab31 | | | 35 | }); |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | it('can render plurals: singular', () => { |
| b69ab31 | | | 38 | render( |
| b69ab31 | | | 39 | <I18nSupport> |
| b69ab31 | | | 40 | <T count={1}>plural</T> |
| b69ab31 | | | 41 | </I18nSupport>, |
| b69ab31 | | | 42 | ); |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | expect(screen.getByText('There is one apple')).toBeInTheDocument(); |
| b69ab31 | | | 45 | expect(screen.queryByText('plural')).not.toBeInTheDocument(); |
| b69ab31 | | | 46 | }); |
| b69ab31 | | | 47 | |
| b69ab31 | | | 48 | it('can render plurals: plural', () => { |
| b69ab31 | | | 49 | render( |
| b69ab31 | | | 50 | <I18nSupport> |
| b69ab31 | | | 51 | <T count={2}>plural</T> |
| b69ab31 | | | 52 | </I18nSupport>, |
| b69ab31 | | | 53 | ); |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | expect(screen.getByText('There are 2 apples')).toBeInTheDocument(); |
| b69ab31 | | | 56 | expect(screen.queryByText('plural')).not.toBeInTheDocument(); |
| b69ab31 | | | 57 | }); |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | it('can replace with strings', () => { |
| b69ab31 | | | 60 | render( |
| b69ab31 | | | 61 | <I18nSupport> |
| b69ab31 | | | 62 | <T replace={{apples: 'oranges'}}>compare apples and bananas</T> |
| b69ab31 | | | 63 | </I18nSupport>, |
| b69ab31 | | | 64 | ); |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | expect(screen.getByText('oranges and bananas', {exact: false})).toBeInTheDocument(); |
| b69ab31 | | | 67 | expect(screen.queryByText('apples', {exact: false})).not.toBeInTheDocument(); |
| b69ab31 | | | 68 | }); |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | it('can replace with components', () => { |
| b69ab31 | | | 71 | render( |
| b69ab31 | | | 72 | <I18nSupport> |
| b69ab31 | | | 73 | <T replace={{apples: <b data-testid="orange">oranges</b>}}>compare apples and bananas</T> |
| b69ab31 | | | 74 | </I18nSupport>, |
| b69ab31 | | | 75 | ); |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | expect(screen.getByTestId('orange')).toBeInTheDocument(); |
| b69ab31 | | | 78 | expect(screen.queryByText('apples', {exact: false})).not.toBeInTheDocument(); |
| b69ab31 | | | 79 | }); |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | it('can replace multiple times', () => { |
| b69ab31 | | | 82 | render( |
| b69ab31 | | | 83 | <I18nSupport> |
| b69ab31 | | | 84 | <T replace={{apples: <b data-testid="orange">oranges</b>, bananas: 'grapefruit'}}> |
| b69ab31 | | | 85 | compare apples and bananas |
| b69ab31 | | | 86 | </T> |
| b69ab31 | | | 87 | </I18nSupport>, |
| b69ab31 | | | 88 | ); |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | expect(screen.getByTestId('orange')).toBeInTheDocument(); |
| b69ab31 | | | 91 | expect(screen.getByText('and grapefruit', {exact: false})).toBeInTheDocument(); |
| b69ab31 | | | 92 | expect(screen.queryByText('apples', {exact: false})).not.toBeInTheDocument(); |
| b69ab31 | | | 93 | expect(screen.queryByText('bananas', {exact: false})).not.toBeInTheDocument(); |
| b69ab31 | | | 94 | }); |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | it('can replace with plurals: singular', () => { |
| b69ab31 | | | 97 | render( |
| b69ab31 | | | 98 | <I18nSupport> |
| b69ab31 | | | 99 | <T replace={{$type: 'cool'}} count={1}> |
| b69ab31 | | | 100 | replace_and_plural |
| b69ab31 | | | 101 | </T> |
| b69ab31 | | | 102 | </I18nSupport>, |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | expect(screen.queryByText('There is one cool apple')).toBeInTheDocument(); |
| b69ab31 | | | 106 | }); |
| b69ab31 | | | 107 | |
| b69ab31 | | | 108 | it('can replace with plurals: plural', () => { |
| b69ab31 | | | 109 | render( |
| b69ab31 | | | 110 | <I18nSupport> |
| b69ab31 | | | 111 | <T replace={{$type: 'cool'}} count={2}> |
| b69ab31 | | | 112 | replace_and_plural |
| b69ab31 | | | 113 | </T> |
| b69ab31 | | | 114 | </I18nSupport>, |
| b69ab31 | | | 115 | ); |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | expect(screen.queryByText('There are 2 cool apples')).toBeInTheDocument(); |
| b69ab31 | | | 118 | }); |
| b69ab31 | | | 119 | }); |
| b69ab31 | | | 120 | |
| b69ab31 | | | 121 | describe('t()', () => { |
| b69ab31 | | | 122 | it('can render translations', () => { |
| b69ab31 | | | 123 | expect(t('translate_me')).toEqual('this was translated'); |
| b69ab31 | | | 124 | }); |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | it('can render plurals: singular', () => { |
| b69ab31 | | | 127 | expect(t('plural', {count: 1})).toEqual('There is one apple'); |
| b69ab31 | | | 128 | }); |
| b69ab31 | | | 129 | |
| b69ab31 | | | 130 | it('can render plurals: plural', () => { |
| b69ab31 | | | 131 | expect(t('plural', {count: 2})).toEqual('There are 2 apples'); |
| b69ab31 | | | 132 | }); |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | it('can replace with strings', () => { |
| b69ab31 | | | 135 | expect(t('compare apples and bananas', {replace: {apples: 'oranges'}})).toEqual( |
| b69ab31 | | | 136 | 'compare oranges and bananas', |
| b69ab31 | | | 137 | ); |
| b69ab31 | | | 138 | }); |
| b69ab31 | | | 139 | |
| b69ab31 | | | 140 | it('can replace multiple times', () => { |
| b69ab31 | | | 141 | expect( |
| b69ab31 | | | 142 | t('compare apples and bananas', {replace: {apples: 'oranges', bananas: 'grapefruit'}}), |
| b69ab31 | | | 143 | ).toEqual('compare oranges and grapefruit'); |
| b69ab31 | | | 144 | }); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | it('can replace with plurals: singular', () => { |
| b69ab31 | | | 147 | expect(t('replace_and_plural', {replace: {$type: 'cool'}, count: 1})).toEqual( |
| b69ab31 | | | 148 | 'There is one cool apple', |
| b69ab31 | | | 149 | ); |
| b69ab31 | | | 150 | }); |
| b69ab31 | | | 151 | |
| b69ab31 | | | 152 | it('can replace with plurals: plural', () => { |
| b69ab31 | | | 153 | expect(t('replace_and_plural', {replace: {$type: 'cool'}, count: 2})).toEqual( |
| b69ab31 | | | 154 | 'There are 2 cool apples', |
| b69ab31 | | | 155 | ); |
| b69ab31 | | | 156 | }); |
| b69ab31 | | | 157 | }); |
| b69ab31 | | | 158 | }); |