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