test(ct): react rerender (#16597)

This commit is contained in:
sand4rt 2022-08-17 07:01:37 +02:00 committed by GitHub
parent 3cd8d6e513
commit 007e197e3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 0 deletions

View file

@ -0,0 +1,19 @@
import { useRef } from "react"
type CounterProps = {
count?: number;
onClick?(props: string): void;
children?: any;
}
let _remountCount = 1;
export default function Counter(props: CounterProps) {
const remountCount = useRef(_remountCount++);
return <div onClick={() => props.onClick?.('hello')}>
<div id="props">{ props.count }</div>
<div id="remount-count">{ remountCount.current }</div>
{ props.children }
</div>
}

View file

@ -6,6 +6,7 @@ import Button from './components/Button';
import DefaultChildren from './components/DefaultChildren';
import MultipleChildren from './components/MultipleChildren';
import MultiRoot from './components/MultiRoot';
import Counter from './components/Counter';
test.use({ viewport: { width: 500, height: 500 } });
@ -14,6 +15,41 @@ test('props should work', async ({ mount }) => {
await expect(component).toContainText('Submit');
});
test('renderer updates props without remounting', async ({ mount }) => {
const component = await mount(<Counter count={9001} />)
await expect(component.locator('#props')).toContainText('9001')
await component.rerender(<Counter count={1337} />)
await expect(component).not.toContainText('9001')
await expect(component.locator('#props')).toContainText('1337')
await expect(component.locator('#remount-count')).toContainText('1')
});
test('renderer updates callbacks without remounting', async ({ mount }) => {
const component = await mount(<Counter />)
const messages: string[] = []
await component.rerender(<Counter onClick={message => {
messages.push(message)
}} />)
await component.click();
expect(messages).toEqual(['hello'])
await expect(component.locator('#remount-count')).toContainText('1')
});
test('renderer updates slots without remounting', async ({ mount }) => {
const component = await mount(<Counter>Default Slot</Counter>)
await expect(component).toContainText('Default Slot')
await component.rerender(<Counter>Test Slot</Counter>)
await expect(component).not.toContainText('Default Slot')
await expect(component).toContainText('Test Slot')
await expect(component.locator('#remount-count')).toContainText('1')
});
test('callback should work', async ({ mount }) => {
const messages: string[] = []
const component = await mount(<Button title="Submit" onClick={data => {