Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jul 9, 2018
1 parent c8826a7 commit e9e63a2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
case Fragment: return 'Fragment';
case StrictMode: return 'StrictMode';
case Profiler: return 'Profiler';
case Portal: return 'Portal';

default: {
const $$typeofType = type && type.$$typeof;
Expand All @@ -370,7 +371,6 @@ class ReactSixteenAdapter extends EnzymeAdapter {
const name = type.render.displayName || functionName(type.render);
return name ? `ForwardRef(${name})` : 'ForwardRef';
}
case Portal: return 'Portal';
default: return displayNameOfNode(node);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/enzyme-test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@
"eslint-plugin-jsx-a11y": "^6.1.0",
"eslint-plugin-react": "^7.10.0"
}
}
}
67 changes: 66 additions & 1 deletion packages/enzyme-test-suite/test/Adapter-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { configure, shallow } from 'enzyme';

import './_helpers/setupAdapters';
import Adapter from './_helpers/adapter';
import { renderToString } from './_helpers/react-compat';
import { renderToString, createContext, createPortal, forwardRef } from './_helpers/react-compat';
import { REACT013, REACT16 } from './_helpers/version';
import { itIf, describeWithDOM } from './_helpers';

Expand Down Expand Up @@ -801,4 +801,69 @@ describe('Adapter', () => {
},
}));
});

it.only('should determine valid element types', () => {
const renderer = adapter.createRenderer({ mode: 'shallow' });

class Component extends React.Component {
render() { return null }
}
const ArrowFunction = () => null

expect(adapter.isValidElementType('div')).to.equal(true);
expect(adapter.isValidElementType(Component)).to.equal(true);
expect(adapter.isValidElementType(ArrowFunction )).to.equal(true);

if (createPortal) {
expect(adapter.isValidElementType(createPortal(<div />, { nodeType: 1 }))).to.equal(false);
}

if (createContext) {
const Context = createContext({ })
expect(adapter.isValidElementType(Context.Consumer)).to.equal(true);
expect(adapter.isValidElementType(Context.Provider)).to.equal(true);
}

if (forwardRef) {
expect(adapter.isValidElementType(forwardRef(() => null))).to.equal(true);
}
});

it('should provide node displayNames', () => {
const getDisplayName = el =>
adapter.displayNameOfNode(adapter.elementToNode(el))

class Component extends React.Component {
render() { return null }
}
class Something extends React.Component {
render() { return null }
}
Something.displayName = 'MyComponent'

const ArrowFunction = () => null

expect(getDisplayName(<div/>)).to.equal('div');
expect(getDisplayName(<Component />)).to.equal('Component');
expect(getDisplayName(<Something />)).to.equal('MyComponent');
expect(getDisplayName(<ArrowFunction />)).to.equal('ArrowFunction');

if (createPortal) {
expect(getDisplayName(createPortal(<div />, { nodeType: 1 }))).to.equal('Portal');
}

if (createContext) {
const Context = createContext({ })
expect(getDisplayName(<Context.Consumer />)).to.equal('ContextConsumer');
expect(getDisplayName(<Context.Provider />)).to.equal('ContextProvider');
}

if (forwardRef) {
const ForwaredRef = forwardRef(() => null);
const NamedForwardedRef = forwardRef(function Named() { return null });

expect(getDisplayName(<ForwaredRef />)).to.equal('forwardRef');
expect(getDisplayName(<NamedForwardedRef />)).to.equal('forwardRef(Named)');
}
});
});
4 changes: 3 additions & 1 deletion packages/enzyme-test-suite/test/_helpers/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let createClass;
let renderToString;
let createPortal;
let createContext;
let forwardRef;

if (is('>=15.5 || ^16.0.0-alpha || ^16.3.0-alpha')) {
// eslint-disable-next-line import/no-extraneous-dependencies
Expand All @@ -32,9 +33,10 @@ if (is('^16.0.0-alpha || ^16.3.0-alpha')) {
}

if (is('^16.3.0-0')) {
({ createContext } = require('react'));
({ createContext, forwardRef } = require('react'));
} else {
createContext = null;
forwardRef = null;
}

export {
Expand Down

0 comments on commit e9e63a2

Please sign in to comment.