Skip to content

Commit

Permalink
Fix check of node type
Browse files Browse the repository at this point in the history
In version 1.14.0 of enzyme-adapter-react-16 this check does not work.
  • Loading branch information
Benjaminsson committed Aug 27, 2019
1 parent 24d2c11 commit 38a45de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ export const applyMap = (json, options) => {
};

export const extractTypeName = node => {
const name = typeName(node);
const type = node.type.$$typeof;

if (name.$$typeof === Symbol.for('react.lazy')) {
if (type === Symbol.for('react.lazy')) {
return 'React.Lazy';
}

if (name.$$typeof === Symbol.for('react.memo')) {
if (type === Symbol.for('react.memo')) {
return 'React.Memo';
}

if (name === Symbol.for('react.suspense')) {
if (node.type === Symbol.for('react.suspense')) {
return 'React.Suspense';
}

return name;
return typeName(node);
};
14 changes: 12 additions & 2 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* eslint-env jest */

import React from 'react';
// import React from 'react';
import React, {memo, lazy, Suspense} from 'react';
import Enzyme, {shallow, mount, render} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import {isEnzymeWrapper} from '../src/utils';
import {isEnzymeWrapper, extractTypeName} from '../src/utils';

Enzyme.configure({adapter: new Adapter()});

const Lazy = lazy(() => Promise.resolve(<div>lazy div</div>));
const Memo = memo(() => <div>memoized div</div>);

it('returns true whenever it is an enzyme wrapper', () => {
expect(isEnzymeWrapper(shallow(<div />))).toBe(true);
expect(isEnzymeWrapper(mount(<div />))).toBe(true);
Expand All @@ -18,3 +22,9 @@ it('returns false whenever it is an enzyme wrapper', () => {
expect(isEnzymeWrapper('test')).toBe(false);
expect(isEnzymeWrapper(<div />)).toBe(false);
});

it('returns correct node type', () => {
expect(extractTypeName(<Lazy />)).toBe('React.Lazy');
expect(extractTypeName(<Memo />)).toBe('React.Memo');
expect(extractTypeName(<Suspense />)).toBe('React.Suspense');
})

0 comments on commit 38a45de

Please sign in to comment.