|
1 | 1 | /** @jest-environment jsdom */
|
2 |
| -import React from 'react'; |
3 | 2 | import { render } from '@testing-library/react';
|
| 3 | +import React from 'react'; |
4 | 4 | import {
|
5 | 5 | Trans,
|
6 | 6 | withTranslation,
|
@@ -309,7 +309,7 @@ describe('trans complex - count only in props', () => {
|
309 | 309 | // prettier-ignore
|
310 | 310 | return (
|
311 | 311 | <Trans i18nKey="transTest2" count={count}>
|
312 |
| - Hello <strong>{{ name }}</strong>, you have {{n: count}} message. Open <Link to="/msgs">here</Link>. |
| 312 | + Hello <strong>{{ name }}</strong>, you have {{ n: count }} message. Open <Link to="/msgs">here</Link>. |
313 | 313 | </Trans>
|
314 | 314 | );
|
315 | 315 | };
|
@@ -368,10 +368,108 @@ describe('trans complex v2 no extra pseudo elements for interpolation', () => {
|
368 | 368 | });
|
369 | 369 | });
|
370 | 370 |
|
| 371 | +describe('Trans with a count using an object property should be detected as a translation with a plural', () => { |
| 372 | + const TestComponent = () => { |
| 373 | + const obj = { anyProp: { a: 10 } }; |
| 374 | + // prettier-ignore |
| 375 | + return ( |
| 376 | + <Trans |
| 377 | + i18nKey="transPluralWithCountAsMemberExpression" |
| 378 | + count={obj.anyProp.a} |
| 379 | + > |
| 380 | + {{ count: obj.anyProp }} item. |
| 381 | + </Trans> |
| 382 | + ); |
| 383 | + }; |
| 384 | + |
| 385 | + it('should render correct content with the count', () => { |
| 386 | + const { container } = render(<TestComponent />); |
| 387 | + expect(container.firstChild).toMatchInlineSnapshot(` |
| 388 | +<div> |
| 389 | + 10 items |
| 390 | +</div> |
| 391 | +`); |
| 392 | + }); |
| 393 | +}); |
| 394 | + |
| 395 | +describe('Trans with a count using an conditionnal statement should be detected as a translation with a plural', () => { |
| 396 | + const TestComponent = () => { |
| 397 | + const obj = true; |
| 398 | + // prettier-ignore |
| 399 | + return ( |
| 400 | + <Trans |
| 401 | + i18nKey="transPluralWithCountAsMemberExpression" |
| 402 | + count={obj ? 3 : 10} |
| 403 | + > |
| 404 | + {{ count: 6 }} item. |
| 405 | + </Trans> |
| 406 | + ); |
| 407 | + }; |
| 408 | + |
| 409 | + it('should render correct content with the count', () => { |
| 410 | + const { container } = render(<TestComponent />); |
| 411 | + expect(container.firstChild).toMatchInlineSnapshot(` |
| 412 | +<div> |
| 413 | + 3 items |
| 414 | +</div> |
| 415 | +`); |
| 416 | + }); |
| 417 | +}); |
| 418 | + |
| 419 | +describe('Trans with a count using an Unary Expression statement should be detected as a translation with a plural', () => { |
| 420 | + const TestComponent = () => { |
| 421 | + const obj = { a: 3 }; |
| 422 | + // prettier-ignore |
| 423 | + return ( |
| 424 | + <Trans |
| 425 | + i18nKey="transPluralWithCountAsMemberExpression" |
| 426 | + count={obj?.a} |
| 427 | + > |
| 428 | + {{ count: 6 }} item. |
| 429 | + </Trans> |
| 430 | + ); |
| 431 | + }; |
| 432 | + |
| 433 | + it('should render correct content with the count', () => { |
| 434 | + const { container } = render(<TestComponent />); |
| 435 | + expect(container.firstChild).toMatchInlineSnapshot(` |
| 436 | +<div> |
| 437 | + 3 items |
| 438 | +</div> |
| 439 | +`); |
| 440 | + }); |
| 441 | +}); |
| 442 | + |
| 443 | +describe('Trans with a count using an Binary Expression statement should be detected as a translation with a plural', () => { |
| 444 | + const TestComponent = () => { |
| 445 | + const obj = { a: 3 }; |
| 446 | + // prettier-ignore |
| 447 | + return ( |
| 448 | + <Trans |
| 449 | + i18nKey="transPluralWithCountAsMemberExpression" |
| 450 | + count={obj.a - 1} |
| 451 | + > |
| 452 | + {{ count: 6 }} item. |
| 453 | + </Trans> |
| 454 | + ); |
| 455 | + }; |
| 456 | + |
| 457 | + it('should render correct content with the count', () => { |
| 458 | + const { container } = render(<TestComponent />); |
| 459 | + expect(container.firstChild).toMatchInlineSnapshot(` |
| 460 | +<div> |
| 461 | + 2 items |
| 462 | +</div> |
| 463 | +`); |
| 464 | + }); |
| 465 | +}); |
| 466 | + |
371 | 467 | describe('trans with t as prop', () => {
|
372 | 468 | const TestComponent = ({ t, cb }) => {
|
373 | 469 | const customT = (...args) => {
|
374 |
| - if (cb) cb(); |
| 470 | + if (cb) { |
| 471 | + cb(); |
| 472 | + } |
375 | 473 | return t(...args);
|
376 | 474 | };
|
377 | 475 | return (
|
@@ -415,7 +513,7 @@ describe('trans with empty content', () => {
|
415 | 513 | const TestComponent = () => <Trans />;
|
416 | 514 | it('should render an empty string', () => {
|
417 | 515 | const { container } = render(<TestComponent />);
|
418 |
| - expect(container.firstChild).toMatchInlineSnapshot(`<div />`); |
| 516 | + expect(container.firstChild).toMatchInlineSnapshot('<div />'); |
419 | 517 | });
|
420 | 518 | });
|
421 | 519 |
|
|
0 commit comments