|
| 1 | +/* eslint-disable react/no-multi-comp */ |
1 | 2 | import React from 'react';
|
2 | 3 | import { assert } from 'chai';
|
3 | 4 | import { createMount } from '@material-ui/core/test-utils';
|
4 | 5 | import { Input } from '@material-ui/core';
|
5 | 6 | import { isMuiElement } from '@material-ui/core/utils/reactHelpers';
|
6 | 7 | import PropTypes from 'prop-types';
|
| 8 | +import consoleErrorMock from 'test/utils/consoleErrorMock'; |
7 | 9 | import withTheme from './withTheme';
|
8 | 10 | import ThemeProvider from './ThemeProvider';
|
9 | 11 |
|
@@ -52,6 +54,69 @@ describe('withTheme', () => {
|
52 | 54 | assert.strictEqual(isMuiElement(<ThemedInput />, ['Input']), true);
|
53 | 55 | });
|
54 | 56 |
|
| 57 | + describe('refs', () => { |
| 58 | + it('forwards ref to class components', () => { |
| 59 | + // eslint-disable-next-line react/prefer-stateless-function |
| 60 | + class TargetComponent extends React.Component { |
| 61 | + render() { |
| 62 | + return null; |
| 63 | + } |
| 64 | + } |
| 65 | + const ThemedTarget = withTheme(TargetComponent); |
| 66 | + |
| 67 | + const ref = React.createRef(); |
| 68 | + mount( |
| 69 | + <> |
| 70 | + <ThemedTarget ref={ref} /> |
| 71 | + </>, |
| 72 | + ); |
| 73 | + |
| 74 | + assert.instanceOf(ref.current, TargetComponent); |
| 75 | + }); |
| 76 | + |
| 77 | + it('forwards refs to React.forwardRef types', () => { |
| 78 | + const ThemedTarget = withTheme( |
| 79 | + React.forwardRef((props, ref) => <div {...props} ref={ref} />), |
| 80 | + ); |
| 81 | + |
| 82 | + const ref = React.createRef(); |
| 83 | + mount( |
| 84 | + <> |
| 85 | + <ThemedTarget ref={ref} /> |
| 86 | + </>, |
| 87 | + ); |
| 88 | + |
| 89 | + assert.strictEqual(ref.current.nodeName, 'DIV'); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('innerRef', () => { |
| 93 | + beforeEach(() => { |
| 94 | + consoleErrorMock.spy(); |
| 95 | + }); |
| 96 | + |
| 97 | + afterEach(() => { |
| 98 | + consoleErrorMock.reset(); |
| 99 | + PropTypes.resetWarningCache(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('is deprecated', () => { |
| 103 | + const ThemedDiv = withTheme('div'); |
| 104 | + |
| 105 | + mount( |
| 106 | + <> |
| 107 | + <ThemedDiv innerRef={React.createRef()} /> |
| 108 | + </>, |
| 109 | + ); |
| 110 | + |
| 111 | + assert.strictEqual(consoleErrorMock.callCount(), 1); |
| 112 | + assert.include( |
| 113 | + consoleErrorMock.args()[0][0], |
| 114 | + 'Warning: Failed prop type: Material-UI: The `innerRef` prop is deprecated', |
| 115 | + ); |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
55 | 120 | it('should throw is the import is invalid', () => {
|
56 | 121 | assert.throw(
|
57 | 122 | () => withTheme(undefined),
|
|
0 commit comments