Skip to content

Commit

Permalink
[test] Fix broken tests in react@next (#20472)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Apr 8, 2020
1 parent cc3b03b commit 3359d81
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ commands:
command: |
node scripts/use-react-dist-tag
# log a patch for maintainers who want to check out this change
git diff HEAD
git --no-pager diff HEAD
- restore_cache:
keys:
- v2-yarn-sha-{{ checksum "yarn.lock" }}
Expand Down Expand Up @@ -152,7 +152,7 @@ jobs:
command: |
node scripts/use-typescript-dist-tag
# log a patch for maintainers who want to check out this change
git diff HEAD
git --no-pager diff HEAD
- install_js
- run:
name: Tests TypeScript definitions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ describe('ThemeProvider', () => {
const text = () => ref.current.textContent;
function Test() {
const theme = useTheme();
themes.push(theme);
React.useEffect(() => {
themes.push(theme);
});

return (
<span ref={ref}>
Expand Down
10 changes: 7 additions & 3 deletions packages/material-ui/src/FormControl/FormControl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { expect } from 'chai';
import { spy } from 'sinon';
import { createMount, getClasses } from '@material-ui/core/test-utils';
import describeConformance from '../test-utils/describeConformance';
import { createClientRender } from 'test/utils/createClientRender';
import { act, createClientRender } from 'test/utils/createClientRender';
import Input from '../Input';
import Select from '../Select';
import FormControl from './FormControl';
Expand All @@ -16,7 +16,9 @@ describe('<FormControl />', () => {

function TestComponent(props) {
const context = useFormControl();
props.contextCallback(context);
React.useEffect(() => {
props.contextCallback(context);
});
return null;
}

Expand Down Expand Up @@ -101,7 +103,9 @@ describe('<FormControl />', () => {
);
expect(readContext.args[0][0]).to.have.property('focused', false);

container.querySelector('input').focus();
act(() => {
container.querySelector('input').focus();
});
expect(readContext.args[1][0]).to.have.property('focused', true);

setProps({ disabled: true });
Expand Down
19 changes: 12 additions & 7 deletions packages/material-ui/src/Paper/Paper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { chainPropTypes } from '@material-ui/utils';
import withStyles from '../styles/withStyles';

export const styles = (theme) => {
Expand Down Expand Up @@ -41,12 +42,6 @@ const Paper = React.forwardRef(function Paper(props, ref) {
...other
} = props;

if (process.env.NODE_ENV !== 'production') {
if (classes[`elevation${elevation}`] === undefined) {
console.error(`Material-UI: this elevation \`${elevation}\` is not implemented.`);
}
}

return (
<Component
className={clsx(
Expand Down Expand Up @@ -91,7 +86,17 @@ Paper.propTypes = {
* Shadow depth, corresponds to `dp` in the spec.
* It accepts values between 0 and 24 inclusive.
*/
elevation: PropTypes.number,
elevation: chainPropTypes(PropTypes.number, (props) => {
const { classes, elevation } = props;
// in case `withStyles` fails to inject we don't need this warning
if (classes === undefined) {
return null;
}
if (elevation != null && classes[`elevation${elevation}`] === undefined) {
return new Error(`Material-UI: this elevation \`${elevation}\` is not implemented.`);
}
return null;
}),
/**
* If `true`, rounded corners are disabled.
*/
Expand Down
45 changes: 27 additions & 18 deletions packages/material-ui/src/Paper/Paper.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { assert } from 'chai';
import { createMount, createShallow, getClasses } from '@material-ui/core/test-utils';
import * as PropTypes from 'prop-types';
import describeConformance from '../test-utils/describeConformance';
import Paper from './Paper';
import { createMuiTheme, ThemeProvider } from '../styles';
Expand All @@ -11,14 +12,6 @@ describe('<Paper />', () => {
let shallow;
let classes;

beforeEach(() => {
consoleErrorMock.spy();
});

afterEach(() => {
consoleErrorMock.reset();
});

before(() => {
mount = createMount({ strict: true });
shallow = createShallow({ dive: true });
Expand Down Expand Up @@ -77,16 +70,6 @@ describe('<Paper />', () => {
);
});

it('warns if the given `elevation` is not implemented in the theme', () => {
mount(<Paper elevation={25} />);

assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(
consoleErrorMock.messages()[0],
'Material-UI: this elevation `25` is not implemented.',
);
});

it('allows custom elevations via theme.shadows', () => {
const theme = createMuiTheme();
theme.shadows.push('20px 20px');
Expand All @@ -98,4 +81,30 @@ describe('<Paper />', () => {

assert.strictEqual(wrapper.find('div[data-testid="paper"]').hasClass('custom-elevation'), true);
});

describe('warnings', () => {
beforeEach(() => {
consoleErrorMock.spy();
PropTypes.resetWarningCache();
});

afterEach(() => {
consoleErrorMock.reset();
});

it('warns if the given `elevation` is not implemented in the theme', () => {
PropTypes.checkPropTypes(
Paper.Naked.propTypes,
{ classes: { elevation24: 'elevation-24', elevation26: 'elevation-26' }, elevation: 25 },
'prop',
'MockedPaper',
);

assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(
consoleErrorMock.messages()[0],
'Material-UI: this elevation `25` is not implemented.',
);
});
});
});
21 changes: 17 additions & 4 deletions packages/material-ui/src/Popover/Popover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { assert, expect } from 'chai';
import { spy, stub, useFakeTimers } from 'sinon';
import { createMount, findOutermostIntrinsic, getClasses } from '@material-ui/core/test-utils';
import * as PropTypes from 'prop-types';
import describeConformance from '../test-utils/describeConformance';
import consoleErrorMock from 'test/utils/consoleErrorMock';
import Grow from '../Grow';
Expand Down Expand Up @@ -460,25 +461,37 @@ describe('<Popover />', () => {
describe('warnings', () => {
beforeEach(() => {
consoleErrorMock.spy();
PropTypes.resetWarningCache();
});

afterEach(() => {
consoleErrorMock.reset();
});

it('should warn if anchorEl is not valid', () => {
const otherWrapper = mount(<Popover open />);
assert.strictEqual(otherWrapper.find(Modal).props().container, undefined);
PropTypes.checkPropTypes(
Popover.Naked.propTypes,
{ classes: {}, open: true },
'prop',
'MockedPopover',
);

assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(consoleErrorMock.messages()[0], 'It should be an Element instance');
});

it('warns if a component for the Paper is used that cant hold a ref', () => {
mount(<Popover {...defaultProps} PaperProps={{ component: () => <div />, elevation: 4 }} />);
PropTypes.checkPropTypes(
Popover.Naked.propTypes,
{ ...defaultProps, classes: {}, PaperProps: { component: () => <div />, elevation: 4 } },
'prop',
'MockedPopover',
);

assert.strictEqual(consoleErrorMock.callCount(), 1);
assert.include(
consoleErrorMock.messages()[0],
'Warning: Failed prop type: Invalid prop `PaperProps.component` supplied to `ForwardRef(Popover)`. Expected an element type that can hold a ref.',
'Warning: Failed prop type: Invalid prop `PaperProps.component` supplied to `MockedPopover`. Expected an element type that can hold a ref.',
);
});

Expand Down
5 changes: 4 additions & 1 deletion scripts/use-react-dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ async function main(distTag) {
packageJson.resolutions[reactPackageName] = version;
});

// CircleCI seemingly times out if it has a newline diff at the end
// https://github.com/enzymejs/enzyme/issues/2358
packageJson.devDependencies['enzyme-adapter-react-16'] = 'npm:@eps1lon/enzyme-adapter-react-next';

// add newline for clean diff
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}${os.EOL}`);
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/use-typescript-dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function main(distTag) {
packageJson.devDependencies.typescript = version;
packageJson.resolutions['**/dtslint/typescript'] = version;

// CircleCI seemingly times out if it has a newline diff at the end
// add newline for clean diff
fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}${os.EOL}`);
}

Expand Down

0 comments on commit 3359d81

Please sign in to comment.