Skip to content

Commit a4b65bb

Browse files
committed
[test] More accurate ref forwarding test
Test where the ref will actually be attached to. This means every component described by this test has to be able to hold a ref
1 parent b794ec1 commit a4b65bb

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

packages/material-ui/src/Typography/Typography.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('<Typography />', () => {
2222
classes,
2323
inheritComponentName: 'p',
2424
mount,
25-
forwardRef: true,
25+
refInstanceof: window.HTMLParagraphElement,
2626
});
2727
});
2828

packages/material-ui/src/test-utils/describeConformance.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,36 +77,25 @@ function testPropsSpread(element, options) {
7777
}
7878

7979
/**
80-
* Some Material-UI components can forward their refs via `innerRef`
80+
* Tests that the `ref` of a component will return the correct instance
8181
*
82-
* TODO: Use `ref` once WithStylesTest is no longer required
82+
* This is determined by a given constructor i.e. a React.Component or HTMLElement for
83+
* components that forward their ref and attach it to a host component.
8384
*
8485
* @param {React.ReactElement} element
8586
* @param {Object} options
86-
* @param {boolean} options.forwardRef - If `true` then this component forwards `ref`
87+
* @param {FunctionConstructor} options.refInstanceof - `ref` will be an instanceof this constructor.
8788
*/
88-
function testRefForwarding(element, options) {
89-
const { forwardRef, mount } = options;
89+
function testRef(element, options) {
90+
const { refInstanceof, mount } = options;
9091

91-
describe('prop: innerRef', () => {
92-
before(() => {
93-
// just to swallow possible error messages from attaching refs to function components
94-
consoleErrorMock.spy();
95-
});
96-
97-
after(() => {
98-
consoleErrorMock.reset();
99-
});
100-
101-
it(`does ${!forwardRef ? 'not' : ''} forward ref`, () => {
92+
describe('ref', () => {
93+
it(`attaches the ref to an instance of ${refInstanceof}`, () => {
10294
const ref = React.createRef();
95+
// TODO use `ref` once `WithStylesTest` is removed
10396
mount(React.cloneElement(element, { innerRef: ref }));
10497

105-
if (!forwardRef) {
106-
assert.strictEqual(ref.current, null);
107-
} else {
108-
assert.ok(ref.current);
109-
}
98+
assert.instanceOf(ref.current, refInstanceof);
11099
});
111100
});
112101
}
@@ -115,7 +104,7 @@ const fullSuite = {
115104
class: testClassName,
116105
componentProp: testComponentProp,
117106
propsSpread: testPropsSpread,
118-
refForwarding: testRefForwarding,
107+
refForwarding: testRef,
119108
};
120109

121110
/**
@@ -127,7 +116,7 @@ const fullSuite = {
127116
* @param {string} options.classes - see testClassName
128117
* @param {string} options.inheritComponentName - see testPropsSpread
129118
* @param {function} options.mount - Should be a return value from createMount
130-
* @param {boolean} options.forwardRef - see test testRefForwarding
119+
* @param {boolean} options.refInstanceof - see test testRef
131120
* @param {string?} options.testComponentPropWith - see test testComponentProp
132121
*/
133122
export default function describeConformance(minimalElement, options) {

0 commit comments

Comments
 (0)