| 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, fireEvent} from '@testing-library/react'; |
| 9 | import * as React from 'react'; |
| 10 | import {expectLogsAndClear, log} from './expectLogs'; |
| 11 | |
| 12 | function Counter(props) { |
| 13 | 'use memo'; |
| 14 | let value = props.value; |
| 15 | let a = value++; |
| 16 | expect(a).toBe(props.value); // postfix |
| 17 | let b = ++value; |
| 18 | expect(b).toBe(props.value + 2); // previous postfix operation + prefix operation |
| 19 | let c = ++value; |
| 20 | expect(c).toBe(props.value + 3); |
| 21 | let d = value--; |
| 22 | expect(d).toBe(props.value + 3); |
| 23 | let e = --value; |
| 24 | expect(e).toBe(props.value + 1); |
| 25 | let f = --value; |
| 26 | expect(f).toBe(props.value); |
| 27 | expect(value).toBe(props.value); |
| 28 | return <span>{value}</span>; |
| 29 | } |
| 30 | |
| 31 | test('use-state', async () => { |
| 32 | const {asFragment, rerender} = render(<Counter value={0} />); |
| 33 | expect(asFragment()).toMatchInlineSnapshot(` |
| 34 | <DocumentFragment> |
| 35 | <span> |
| 36 | 0 |
| 37 | </span> |
| 38 | </DocumentFragment> |
| 39 | `); |
| 40 | |
| 41 | rerender(<Counter value={1} />); |
| 42 | expect(asFragment()).toMatchInlineSnapshot(` |
| 43 | <DocumentFragment> |
| 44 | <span> |
| 45 | 1 |
| 46 | </span> |
| 47 | </DocumentFragment> |
| 48 | `); |
| 49 | }); |