Skip to content

Commit

Permalink
Merge pull request #6 from dabapps/classlike-guard
Browse files Browse the repository at this point in the history
Class-like guard (and drop < ES5 support)
  • Loading branch information
JakeSidSmith authored Jan 27, 2020
2 parents d5ec750 + 36290b0 commit a3501b9
Show file tree
Hide file tree
Showing 17 changed files with 1,619 additions and 1,134 deletions.
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
/node_modules/*
/coverage/*
/build/*
!/dist/*

.DS_Store
npm-debug.log*
2,694 changes: 1,581 additions & 1,113 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@dabapps/react-shallow-renderer",
"version": "1.0.1",
"version": "2.0.0",
"description": "A shallow renderer for React components",
"main": "dist/index.js",
"scripts": {
"dist": "./scripts/dist",
"dist": "rm -rf dist && tsc --project tsconfig.dist.json",
"prettier-check": "prettier --check '**/*.{ts,tsx,js,jsx}'",
"prettier": "prettier --write '**/*.{ts,tsx,js,jsx}'",
"lint": "npm run prettier-check && tslint --project tsconfig.json '{src,tests}/**/*.{ts,tsx}'",
Expand Down Expand Up @@ -37,12 +37,12 @@
"react-dom": ">=16"
},
"devDependencies": {
"@types/jest": "^24.0.11",
"jest": "^24.4.0",
"@types/jest": "^24.9.1",
"jest": "^24.9.0",
"prettier": "^1.16.4",
"ts-jest": "^24.0.0",
"tslint": "^5.13.1",
"tslint-config-dabapps": "github:dabapps/tslint-config-dabapps#v0.5.3",
"tslint": "^5.20.1",
"tslint-config-dabapps": "^0.6.2",
"typescript": "^3.3.3333"
},
"peerDependencies": {
Expand Down
5 changes: 0 additions & 5 deletions scripts/dist

This file was deleted.

8 changes: 6 additions & 2 deletions src/guards.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as React from 'react';

import {
contextSymbol,
elementSymbol,
Expand Down Expand Up @@ -35,15 +37,17 @@ export function isClass(node: ReactAnyNode): node is ReactClassNode {
return (
node.$$typeof === elementSymbol &&
typeof node.type === 'function' &&
MATCHES_CLASS.test(Object.toString.call(node.type))
(node.type instanceof React.Component ||
(node.type.prototype && 'render' in node.type.prototype) ||
MATCHES_CLASS.test(Object.toString.call(node.type)))
);
}

export function isFunction(node: ReactAnyNode): node is ReactFunctionNode {
return (
node.$$typeof === elementSymbol &&
typeof node.type === 'function' &&
!MATCHES_CLASS.test(Object.toString.call(node.type))
!isClass(node)
);
}

Expand Down
9 changes: 5 additions & 4 deletions src/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { elementSymbol } from './constants';
import {
isArrayOfChildren,
Expand Down Expand Up @@ -44,8 +45,8 @@ export class ReactShallowRenderer {
};
}

if (isFunction(node)) {
const rendered = node.type(node.props) as ReactAnyChildren;
if (isClass(node)) {
const rendered = new node.type(node.props).render() as ReactAnyChildren;
const children = this.resolveChildren(
([] as ReactAnyChildrenArray).concat(rendered)
);
Expand All @@ -57,8 +58,8 @@ export class ReactShallowRenderer {
return children;
}

if (isClass(node)) {
const rendered = new node.type(node.props).render() as ReactAnyChildren;
if (isFunction(node)) {
const rendered = node.type(node.props) as ReactAnyChildren;
const children = this.resolveChildren(
([] as ReactAnyChildrenArray).concat(rendered)
);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import {
contextSymbol,
elementSymbol,
Expand Down
1 change: 1 addition & 0 deletions tests/internal-methods.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { ReactAnyNode, ReactShallowRenderer } from '../src';

describe('ReactShallowRenderer', () => {
Expand Down
1 change: 1 addition & 0 deletions tests/to-json/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
Expand Down
1 change: 1 addition & 0 deletions tests/to-json/component-class.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
Expand Down
1 change: 1 addition & 0 deletions tests/to-json/component-function.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
Expand Down
1 change: 1 addition & 0 deletions tests/to-json/context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
Expand Down
8 changes: 5 additions & 3 deletions tests/to-json/forward-ref.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
Expand Down Expand Up @@ -33,9 +34,10 @@ describe('ReactShallowRenderer', () => {
</div>
));

const UnknownForwardRefComponent: React.FunctionComponent = React.forwardRef(
() => <p>Unknown name</p>
);
const UnknownForwardRefComponent: React.FunctionComponent = React.forwardRef<
unknown,
React.PropsWithChildren<{}>
>(() => <p>Unknown name</p>);

const ComponentWithUnknownForwardRefChild: React.FunctionComponent = () => (
<div>
Expand Down
1 change: 1 addition & 0 deletions tests/to-json/fragment.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
Expand Down
1 change: 1 addition & 0 deletions tests/to-json/memo.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';

import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
Expand Down
1 change: 1 addition & 0 deletions tests/to-json/portal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { ReactShallowRenderer } from '../../src';
import { elementSymbol } from '../../src/constants';
import { compare } from '../helpers/compare';
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"declaration": true,
"listEmittedFiles": true,
"sourceMap": true,
"target": "es5",
"target": "es6",
"module": "commonjs",
"rootDir": "./src/",
"outDir": "./dist/"
Expand Down

0 comments on commit a3501b9

Please sign in to comment.