Skip to content

Commit

Permalink
test: add test that exhibits the behavior causing necolas#1665
Browse files Browse the repository at this point in the history
  • Loading branch information
imnotjames committed Sep 21, 2020
1 parent 4a70300 commit 10aadfc
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,36 @@ describe('components/Picker', () => {
const { container } = render(picker);
expect(findSelect(container).value).toBe('22');
});

test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<Picker ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<Picker ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<Picker ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<Picker ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<Picker ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<Picker ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-env jasmine, jest */
/* eslint-disable react/jsx-no-bind */

import React from 'react';
import { render } from '@testing-library/react';
import TouchableOpacity from '../';

describe('components/Pressable', () => {
test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<TouchableOpacity ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableOpacity ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
31 changes: 31 additions & 0 deletions packages/react-native-web/src/exports/Text/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,35 @@ describe('components/Text', () => {
});

test('prop "numberOfLines"', () => {});

test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<Text ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<Text ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<Text ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<Text ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<Text ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<Text ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,35 @@ describe('components/TextInput', () => {
const input = findInput(container);
expect(input.value).toEqual(value);
});

test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<TextInput ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<TextInput ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<TextInput ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TextInput ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<TextInput ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TextInput ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-env jasmine, jest */
/* eslint-disable react/jsx-no-bind */

import React from 'react';
import { render } from '@testing-library/react';
import TouchableHighlight from '../';
import View from '../../View';

describe('components/TouchableHighlight', () => {
test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<TouchableHighlight ref={refMock}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalled();

rerender(<TouchableHighlight ref={otherRefMock}><View /></TouchableHighlight>)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableHighlight ref={refMock}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableHighlight ref={refMock}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableHighlight ref={refMock} testID={'foo'}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableHighlight ref={refMock} testID={'bar'}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-env jasmine, jest */
/* eslint-disable react/jsx-no-bind */

import React from 'react';
import { render } from '@testing-library/react';
import TouchableOpacity from '../';

describe('components/TouchableOpacity', () => {
test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<TouchableOpacity ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableOpacity ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-env jasmine, jest */
/* eslint-disable react/jsx-no-bind */

import React from 'react';
import { render } from '@testing-library/react';
import TouchableWithoutFeedback from '../';
import View from '../../View';

describe('components/TouchableWithoutFeedback', () => {
test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<TouchableWithoutFeedback ref={refMock}><View /></TouchableWithoutFeedback>);
expect(refMock).toHaveBeenCalled();

rerender(<TouchableWithoutFeedback ref={otherRefMock}><View /></TouchableWithoutFeedback>)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableWithoutFeedback ref={refMock}><View /></TouchableWithoutFeedback>);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableWithoutFeedback ref={refMock}><View /></TouchableWithoutFeedback>);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableWithoutFeedback ref={refMock} testID={'foo'}><View /></TouchableWithoutFeedback>);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableWithoutFeedback ref={refMock} testID={'bar'}><View /></TouchableWithoutFeedback>);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
31 changes: 31 additions & 0 deletions packages/react-native-web/src/exports/View/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ describe('components/View', () => {
);
expect(console.error).toBeCalled();
});

test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<View ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<View ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<View ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<View ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<View ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<View ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});

test('prop "pointerEvents"', () => {
Expand Down

0 comments on commit 10aadfc

Please sign in to comment.