├── packages ├── enzyme │ ├── .npmrc │ ├── .eslintignore │ ├── mount.js │ ├── render.js │ ├── shallow.js │ ├── ReactWrapper.js │ ├── ShallowWrapper.js │ ├── .babelrc │ ├── src │ │ ├── shallow.js │ │ ├── mount.js │ │ ├── getAdapter.js │ │ ├── index.js │ │ ├── configuration.js │ │ └── render.js │ ├── .eslintrc │ └── withDom.js ├── enzyme-adapter-utils │ ├── .npmrc │ ├── .eslintignore │ ├── src │ │ ├── index.js │ │ ├── RootFinder.jsx │ │ ├── createRenderWrapper.jsx │ │ └── wrapWithSimpleWrapper.jsx │ ├── .babelrc │ └── .eslintrc ├── enzyme-example-mocha │ ├── .npmrc │ ├── .eslintignore │ ├── test │ │ ├── mocha.opts │ │ └── .setup.js │ ├── src │ │ ├── Foo.jsx │ │ └── Foo.spec.jsx │ ├── .nycrc │ ├── README.md │ ├── .eslintrc │ ├── .babelrc │ ├── .gitignore │ └── LICENSE ├── enzyme-shallow-equal │ ├── .npmrc │ ├── .eslintignore │ ├── .eslintrc │ ├── README.md │ ├── .babelrc │ └── src │ │ └── index.js ├── enzyme-test-suite │ ├── .npmrc │ ├── .eslintignore │ ├── test │ │ ├── _helpers │ │ │ ├── beforeEach.js │ │ │ ├── selectors.js │ │ │ ├── realArrowFunction.js │ │ │ ├── untranspiledArrowFunction.js │ │ │ ├── untranspiledSloppyReturnThis.js │ │ │ ├── version.js │ │ │ ├── describeMethods.js │ │ │ ├── describeLifecycles.js │ │ │ ├── describeHooks.js │ │ │ ├── getLoadedLazyComponent.js │ │ │ ├── adapter.js │ │ │ └── setupAdapters.js │ │ └── shared │ │ │ ├── methods │ │ │ ├── getNodes.jsx │ │ │ ├── getNode.jsx │ │ │ ├── childAt.jsx │ │ │ ├── last.jsx │ │ │ ├── first.jsx │ │ │ ├── tap.jsx │ │ │ ├── every.jsx │ │ │ ├── someWhere.jsx │ │ │ ├── everyWhere.jsx │ │ │ ├── not.jsx │ │ │ ├── key.jsx │ │ │ ├── getElements.jsx │ │ │ ├── some.jsx │ │ │ ├── _method.template │ │ │ ├── forEach.jsx │ │ │ ├── isEmpty.jsx │ │ │ ├── flatMap.jsx │ │ │ ├── at.jsx │ │ │ ├── deprecatedInstanceProperties.jsx │ │ │ ├── root.jsx │ │ │ ├── unmount.jsx │ │ │ ├── filter.jsx │ │ │ ├── get.jsx │ │ │ └── map.jsx │ │ │ ├── lifecycles │ │ │ ├── componentWillUnmount.jsx │ │ │ └── componentDidMount.jsx │ │ │ └── hooks │ │ │ ├── useRef.jsx │ │ │ ├── _hook.template │ │ │ ├── useImperativeHandle.jsx │ │ │ ├── useDebugValue.jsx │ │ │ └── useLayoutEffect.jsx │ └── .babelrc ├── enzyme-adapter-react-13 │ ├── .npmrc │ ├── .eslintignore │ ├── src │ │ ├── index.js │ │ ├── ReactThirteenMapNativeEventNames.js │ │ └── ReactThirteenElementToTree.js │ ├── .babelrc │ └── .eslintrc ├── enzyme-adapter-react-14 │ ├── .npmrc │ ├── .eslintignore │ ├── src │ │ └── index.js │ ├── .babelrc │ └── .eslintrc ├── enzyme-adapter-react-15.4 │ ├── .npmrc │ ├── .eslintignore │ ├── src │ │ └── index.js │ ├── .babelrc │ └── .eslintrc ├── enzyme-adapter-react-15 │ ├── .npmrc │ ├── .eslintignore │ ├── src │ │ └── index.js │ ├── .babelrc │ └── .eslintrc ├── enzyme-adapter-react-16 │ ├── .npmrc │ ├── .eslintignore │ ├── src │ │ └── index.js │ ├── .babelrc │ └── .eslintrc ├── enzyme-adapter-react-helper │ ├── .npmrc │ ├── .eslintignore │ ├── .babelrc │ ├── .eslintrc │ └── src │ │ ├── ifReact.js │ │ ├── safeSFC.jsx │ │ └── getAdapterForReactVersion.js ├── enzyme-adapter-react-16.1 │ ├── .npmrc │ ├── src │ │ └── index.js │ ├── .eslintignore │ ├── .babelrc │ └── .eslintrc ├── enzyme-adapter-react-16.2 │ ├── .npmrc │ ├── src │ │ └── index.js │ ├── .eslintignore │ ├── .babelrc │ └── .eslintrc └── enzyme-adapter-react-16.3 │ ├── .npmrc │ ├── src │ └── index.js │ ├── .eslintignore │ ├── .babelrc │ └── .eslintrc ├── .lgtm ├── .npmrc ├── MAINTAINERS ├── lerna.json ├── test └── mocha.opts ├── .github ├── workflows │ ├── require-allow-edits.yml │ ├── rebase.yml │ └── node-pretest.yml ├── FUNDING.yml └── ISSUE_TEMPLATE │ └── Feature_request.md ├── install-relevant-react.sh ├── .babelrc ├── docs ├── api │ ├── ReactWrapper │ │ ├── length.md │ │ ├── key.md │ │ ├── isEmpty.md │ │ ├── isEmptyRender.md │ │ ├── getDOMNode.md │ │ ├── first.md │ │ ├── is.md │ │ ├── last.md │ │ ├── parent.md │ │ ├── get.md │ │ ├── prop.md │ │ ├── tap.md │ │ ├── filter.md │ │ ├── state.md │ │ ├── render.md │ │ ├── exists.md │ │ ├── getElement.md │ │ ├── findWhere.md │ │ ├── childAt.md │ │ ├── context.md │ │ ├── at.md │ │ ├── hostNodes.md │ │ ├── getElements.md │ │ ├── filterWhere.md │ │ ├── not.md │ │ ├── closest.md │ │ ├── children.md │ │ ├── parents.md │ │ ├── text.md │ │ ├── name.md │ │ ├── every.md │ │ ├── forEach.md │ │ ├── hasClass.md │ │ ├── some.md │ │ ├── html.md │ │ ├── everyWhere.md │ │ ├── invoke.md │ │ ├── someWhere.md │ │ ├── equals.md │ │ ├── ref.md │ │ ├── map.md │ │ ├── type.md │ │ ├── getWrappingComponent.md │ │ ├── slice.md │ │ ├── unmount.md │ │ ├── update.md │ │ ├── find.md │ │ ├── reduce.md │ │ ├── reduceRight.md │ │ ├── setContext.md │ │ ├── props.md │ │ ├── mount.md │ │ └── instance.md │ ├── ShallowWrapper │ │ ├── length.md │ │ ├── key.md │ │ ├── isEmpty.md │ │ ├── isEmptyRender.md │ │ ├── is.md │ │ ├── first.md │ │ ├── last.md │ │ ├── get.md │ │ ├── parent.md │ │ ├── tap.md │ │ ├── filter.md │ │ ├── state.md │ │ ├── render.md │ │ ├── exists.md │ │ ├── getElement.md │ │ ├── findWhere.md │ │ ├── context.md │ │ ├── childAt.md │ │ ├── at.md │ │ ├── getElements.md │ │ ├── hostNodes.md │ │ ├── filterWhere.md │ │ ├── not.md │ │ ├── closest.md │ │ ├── text.md │ │ ├── children.md │ │ ├── parents.md │ │ ├── invoke.md │ │ ├── every.md │ │ ├── unmount.md │ │ ├── forEach.md │ │ ├── hasClass.md │ │ ├── some.md │ │ ├── everyWhere.md │ │ ├── someWhere.md │ │ ├── name.md │ │ ├── equals.md │ │ ├── type.md │ │ ├── html.md │ │ ├── map.md │ │ ├── getWrappingComponent.md │ │ ├── shallow.md │ │ ├── dive.md │ │ ├── slice.md │ │ ├── find.md │ │ ├── update.md │ │ ├── reduce.md │ │ ├── reduceRight.md │ │ ├── setContext.md │ │ └── instance.md │ └── README.md ├── GLOSSARY.md ├── guides.md ├── guides │ ├── systemjs.md │ ├── webpack.md │ ├── browserify.md │ ├── mocha.md │ ├── lab.md │ └── tape-ava.md ├── installation │ ├── README.md │ ├── react-013.md │ ├── react-16.md │ ├── react-15.md │ └── react-014.md └── future.md ├── book.json ├── .nycrc ├── .gitignore ├── LICENSE.md └── since.js /packages/enzyme/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-adapter-utils/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-shallow-equal/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-test-suite/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-13/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-14/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15.4/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-helper/.npmrc: -------------------------------------------------------------------------------- 1 | ../../.npmrc -------------------------------------------------------------------------------- /packages/enzyme-adapter-utils/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-shallow-equal/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-test-suite/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /.lgtm: -------------------------------------------------------------------------------- 1 | approvals = 1 2 | pattern = "(?i):shipit:|:\\+1:|LGTM" 3 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-13/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-14/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15.4/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.1/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.2/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.3/.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-helper/.eslintignore: -------------------------------------------------------------------------------- 1 | ../../.eslintignore -------------------------------------------------------------------------------- /packages/enzyme/mount.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./build/mount'); 2 | -------------------------------------------------------------------------------- /packages/enzyme/render.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./build/render'); 2 | -------------------------------------------------------------------------------- /packages/enzyme/shallow.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./build/shallow'); 2 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | update-notifier=false 3 | legacy-peer-deps=true 4 | -------------------------------------------------------------------------------- /MAINTAINERS: -------------------------------------------------------------------------------- 1 | ljharb 2 | lelandrichardson 3 | blainekasten 4 | Aweary 5 | nfcampos 6 | -------------------------------------------------------------------------------- /packages/enzyme/ReactWrapper.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./build/ReactWrapper'); 2 | -------------------------------------------------------------------------------- /packages/enzyme/ShallowWrapper.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./build/ShallowWrapper'); 2 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.1/src/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./ReactSixteenOneAdapter'); 2 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.2/src/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./ReactSixteenTwoAdapter'); 2 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.3/src/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./ReactSixteenThreeAdapter'); 2 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.1/.eslintignore: -------------------------------------------------------------------------------- 1 | _book/ 2 | build/ 3 | node_modules/ 4 | **/node_modules/ 5 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.2/.eslintignore: -------------------------------------------------------------------------------- 1 | _book/ 2 | build/ 3 | node_modules/ 4 | **/node_modules/ 5 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.3/.eslintignore: -------------------------------------------------------------------------------- 1 | _book/ 2 | build/ 3 | node_modules/ 4 | **/node_modules/ 5 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require test/.setup.js 2 | --compilers js:@babel/register 3 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-utils/src/index.js: -------------------------------------------------------------------------------- 1 | /* eslint global-require: 0 */ 2 | module.exports = require('./Utils'); 3 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-13/src/index.js: -------------------------------------------------------------------------------- 1 | /* eslint global-require: 0 */ 2 | module.exports = require('./ReactThirteenAdapter'); 3 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-14/src/index.js: -------------------------------------------------------------------------------- 1 | /* eslint global-require: 0 */ 2 | module.exports = require('./ReactFourteenAdapter'); 3 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15/src/index.js: -------------------------------------------------------------------------------- 1 | /* eslint global-require: 0 */ 2 | module.exports = require('./ReactFifteenAdapter'); 3 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16/src/index.js: -------------------------------------------------------------------------------- 1 | /* eslint global-require: 0 */ 2 | module.exports = require('./ReactSixteenAdapter'); 3 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15.4/src/index.js: -------------------------------------------------------------------------------- 1 | /* eslint global-require: 0 */ 2 | module.exports = require('./ReactFifteenFourAdapter'); 3 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/src/Foo.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | const Foo = () =>
; 4 | 5 | export default Foo; 6 | -------------------------------------------------------------------------------- /lerna.json: -------------------------------------------------------------------------------- 1 | { 2 | "lerna": "2.0.0", 3 | "packages": [ 4 | "packages/*" 5 | ], 6 | "version": "independent", 7 | "independent": true 8 | } 9 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/.nycrc: -------------------------------------------------------------------------------- 1 | { 2 | "reporter": [ 3 | "lcov", 4 | "text-summary" 5 | ], 6 | "extension": [ 7 | ".jsx" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/README.md: -------------------------------------------------------------------------------- 1 | # enzyme-example-mocha 2 | Example project with React + Enzyme + Mocha 3 | 4 | # npm scripts 5 | ``` 6 | npm test 7 | 8 | ``` 9 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require ./packages/enzyme/withDom.js ./packages/enzyme-test-suite/build/_helpers/setupAdapters.js ./packages/enzyme-test-suite/build/_helpers/beforeEach.js 2 | -------------------------------------------------------------------------------- /packages/enzyme-shallow-equal/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "extends": "airbnb-base", 4 | "parser": "@babel/eslint-parser", 5 | "ignorePatterns": ["build/"], 6 | } 7 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/beforeEach.js: -------------------------------------------------------------------------------- 1 | import sinon from 'sinon-sandbox'; 2 | 3 | beforeEach(() => { // eslint-disable-line mocha/no-top-level-hooks 4 | sinon.restore(); 5 | }); 6 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/selectors.js: -------------------------------------------------------------------------------- 1 | export const getElementPropSelector = (prop) => (x) => x.props[prop]; 2 | 3 | export const getWrapperPropSelector = (prop) => (x) => x.prop(prop); 4 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/realArrowFunction.js: -------------------------------------------------------------------------------- 1 | try { 2 | // eslint-disable-next-line global-require 3 | module.exports = require('./untranspiledArrowFunction'); 4 | } catch (e) { 5 | module.exports = (x) => () => x; 6 | } 7 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/untranspiledArrowFunction.js: -------------------------------------------------------------------------------- 1 | // this file is ignored in babelrc, specifically so that tests will be able to run 2 | // on a real arrow function that lacks a .prototype 3 | module.exports = (x) => () => x; 4 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "env": { 7 | "node": true, 8 | "mocha": true, 9 | }, 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-shallow-equal/README.md: -------------------------------------------------------------------------------- 1 | # enzyme-shallow-equal 2 | 3 | This is the implementation of shallowEqual that enzyme uses. 4 | 5 | It's adapted from https://github.com/facebook/react/blob/144328fe81719e916b946e22660479e31561bb0b/packages/shared/shallowEqual.js#L36-L68 6 | -------------------------------------------------------------------------------- /.github/workflows/require-allow-edits.yml: -------------------------------------------------------------------------------- 1 | name: Require “Allow Edits” 2 | 3 | on: [pull_request_target] 4 | 5 | jobs: 6 | _: 7 | name: "Require “Allow Edits”" 8 | 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: ljharb/require-allow-edits@main 13 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | } 10 | -------------------------------------------------------------------------------- /packages/enzyme/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /install-relevant-react.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | REACT="${REACT:-${1:-16}}" 4 | 5 | echo "installing React $REACT" 6 | 7 | if [ "$REACT" = "0.13" ]; then 8 | npm run env: -- 13 9 | elif [ "$REACT" = "0.14" ]; then 10 | npm run env: -- 14 11 | else 12 | npm run env: -- "${REACT}" 13 | fi 14 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-utils/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-shallow-equal/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-13/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-14/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15.4/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.1/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.2/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.3/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-helper/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["airbnb", { "transformRuntime": false }], 4 | ], 5 | "plugins": [ 6 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 7 | ["add-module-exports"], 8 | ], 9 | "sourceMaps": "both", 10 | } 11 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["airbnb"], 3 | "plugins": [ 4 | ["transform-replace-object-assign", { "moduleSpecifier": "object.assign" }], 5 | ["add-module-exports"], 6 | ], 7 | "ignore": [ 8 | "packages/enzyme-test-suite/test/_helpers/untranspiled*", 9 | ], 10 | "sourceMaps": "both", 11 | } 12 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-helper/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "rules": { 7 | "max-len": 0, 8 | }, 9 | "settings": { 10 | "react": { 11 | "version": "detect", 12 | }, 13 | }, 14 | } 15 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/untranspiledSloppyReturnThis.js: -------------------------------------------------------------------------------- 1 | /* eslint 2 | prefer-spread: 0, 3 | func-names: 0, 4 | prefer-rest-params: 0, 5 | */ 6 | module.exports = function (fn) { 7 | return function () { 8 | return fn.apply(null, [this].concat(Array.prototype.slice.call(arguments))); 9 | }; 10 | }; 11 | -------------------------------------------------------------------------------- /.github/workflows/rebase.yml: -------------------------------------------------------------------------------- 1 | name: Automatic Rebase 2 | 3 | on: [pull_request_target] 4 | 5 | jobs: 6 | _: 7 | name: "Automatic Rebase" 8 | 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: actions/checkout@v3 13 | - uses: ljharb/rebase@master 14 | env: 15 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 16 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Dependency directory 12 | node_modules 13 | 14 | # Optional npm cache directory 15 | .npm 16 | 17 | # Optional REPL history 18 | .node_repl_history 19 | 20 | # Coverage 21 | .coverage 22 | .nyc_output 23 | -------------------------------------------------------------------------------- /packages/enzyme/src/shallow.js: -------------------------------------------------------------------------------- 1 | import ShallowWrapper from './ShallowWrapper'; 2 | 3 | /** 4 | * Shallow renders a react component and provides a testing wrapper around it. 5 | * 6 | * @param node 7 | * @returns {ShallowWrapper} 8 | */ 9 | export default function shallow(node, options) { 10 | return new ShallowWrapper(node, null, options); 11 | } 12 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/length.md: -------------------------------------------------------------------------------- 1 | # `.length => number` 2 | 3 | Returns the number of React nodes enclosed in this wrapper. 4 | 5 | 6 | 7 | #### Returns 8 | 9 | `Number`: count of nodes in the list in this wrapper. 10 | 11 | 12 | 13 | #### Example 14 | 15 | 16 | ```jsx 17 | const wrapper = mount(
); 18 | expect(wrapper.length).to.equal(1); 19 | ``` 20 | -------------------------------------------------------------------------------- /packages/enzyme/src/mount.js: -------------------------------------------------------------------------------- 1 | import ReactWrapper from './ReactWrapper'; 2 | 3 | /** 4 | * Mounts and renders a react component into the document and provides a testing wrapper around it. 5 | * 6 | * @param node 7 | * @returns {ReactWrapper} 8 | */ 9 | export default function mount(node, options) { 10 | return new ReactWrapper(node, null, options); 11 | } 12 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/length.md: -------------------------------------------------------------------------------- 1 | # `.length => number` 2 | 3 | Returns the number of React nodes enclosed in this wrapper. 4 | 5 | 6 | 7 | #### Returns 8 | 9 | `Number`: count of nodes in the list in this wrapper. 10 | 11 | 12 | 13 | #### Example 14 | 15 | 16 | ```jsx 17 | const wrapper = shallow(
); 18 | expect(wrapper.length).to.equal(1); 19 | ``` 20 | -------------------------------------------------------------------------------- /packages/enzyme/src/getAdapter.js: -------------------------------------------------------------------------------- 1 | import validateAdapter from './validateAdapter'; 2 | import { get } from './configuration'; 3 | 4 | export default function getAdapter(options = {}) { 5 | if (options.adapter) { 6 | validateAdapter(options.adapter); 7 | return options.adapter; 8 | } 9 | const { adapter } = get(); 10 | validateAdapter(adapter); 11 | return adapter; 12 | } 13 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-helper/src/ifReact.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { intersects } from 'semver'; 3 | 4 | export default function ifReact(range, yes, no) { 5 | if (typeof yes !== 'function') { throw new TypeError('"yes" must be a function'); } 6 | if (typeof no !== 'function') { throw new TypeError('"no" must be a function'); } 7 | return intersects(range, React.version) ? yes : no; 8 | } 9 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-utils/src/RootFinder.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import PropTypes from 'prop-types'; 3 | 4 | export default class RootFinder extends React.Component { 5 | render() { 6 | const { children } = this.props; 7 | return children; 8 | } 9 | } 10 | RootFinder.propTypes = { 11 | children: PropTypes.node, 12 | }; 13 | RootFinder.defaultProps = { 14 | children: null, 15 | }; 16 | -------------------------------------------------------------------------------- /packages/enzyme/src/index.js: -------------------------------------------------------------------------------- 1 | export { default as ReactWrapper } from './ReactWrapper'; 2 | export { default as ShallowWrapper } from './ShallowWrapper'; 3 | export { default as EnzymeAdapter } from './EnzymeAdapter'; 4 | 5 | export { default as mount } from './mount'; 6 | export { default as shallow } from './shallow'; 7 | export { default as render } from './render'; 8 | export { merge as configure } from './configuration'; 9 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/key.md: -------------------------------------------------------------------------------- 1 | # `.key() => String` 2 | 3 | Returns the key value for the node of the current wrapper. It must be a single-node wrapper. 4 | 5 | #### Example 6 | 7 | 8 | ```jsx 9 | const wrapper = mount(( 10 |
    11 | {['foo', 'bar'].map((s) =>
  • {s}
  • )} 12 |
13 | )).find('li'); 14 | expect(wrapper.at(0).key()).to.equal('foo'); 15 | expect(wrapper.at(1).key()).to.equal('bar'); 16 | ``` 17 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/key.md: -------------------------------------------------------------------------------- 1 | # `.key() => String` 2 | 3 | Returns the key value for the node of the current wrapper. It must be a single-node wrapper. 4 | 5 | #### Example 6 | 7 | 8 | ```jsx 9 | const wrapper = shallow(( 10 |
    11 | {['foo', 'bar'].map((s) =>
  • {s}
  • )} 12 |
13 | )).find('li'); 14 | expect(wrapper.at(0).key()).to.equal('foo'); 15 | expect(wrapper.at(1).key()).to.equal('bar'); 16 | ``` 17 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/isEmpty.md: -------------------------------------------------------------------------------- 1 | # `.isEmpty() => Boolean` 2 | **Deprecated**: Use [`.exists()`](exists.md) instead. 3 | 4 | Returns whether or not the wrapper is empty. 5 | 6 | 7 | #### Returns 8 | 9 | `Boolean`: whether or not the wrapper is empty. 10 | 11 | 12 | #### Example 13 | 14 | 15 | ```jsx 16 | const wrapper = mount(
); 17 | expect(wrapper.find('.other-class').isEmpty()).to.equal(true); 18 | ``` 19 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/isEmpty.md: -------------------------------------------------------------------------------- 1 | # `.isEmpty() => Boolean` 2 | **Deprecated**: Use [`.exists()`](exists.md) instead. 3 | 4 | Returns whether or not the wrapper is empty. 5 | 6 | 7 | #### Returns 8 | 9 | `Boolean`: whether or not the wrapper is empty. 10 | 11 | 12 | #### Example 13 | 14 | 15 | ```jsx 16 | const wrapper = shallow(
); 17 | expect(wrapper.find('.other-class').isEmpty()).to.equal(true); 18 | ``` 19 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/isEmptyRender.md: -------------------------------------------------------------------------------- 1 | # `.isEmptyRender() => Boolean` 2 | 3 | Returns whether or not the wrapper would ultimately render only the allowed falsy values: `false` or `null`. 4 | 5 | #### Returns 6 | 7 | `Boolean`: whether the return is falsy 8 | 9 | #### Example 10 | 11 | ```jsx 12 | function Foo() { 13 | return null; 14 | } 15 | 16 | const wrapper = mount(); 17 | expect(wrapper.isEmptyRender()).to.equal(true); 18 | ``` 19 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/isEmptyRender.md: -------------------------------------------------------------------------------- 1 | # `.isEmptyRender() => Boolean` 2 | 3 | Returns whether or not the wrapper would ultimately render only the allowed falsy values: `false` or `null`. 4 | 5 | #### Returns 6 | 7 | `Boolean`: whether the return is falsy 8 | 9 | #### Example 10 | 11 | ```jsx 12 | function Foo() { 13 | return null; 14 | } 15 | 16 | const wrapper = shallow(); 17 | expect(wrapper.isEmptyRender()).to.equal(true); 18 | ``` 19 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-utils/src/createRenderWrapper.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export default function createRenderWrapper(node, context, childContextTypes) { 4 | class ContextWrapper extends React.Component { 5 | getChildContext() { 6 | return context; 7 | } 8 | 9 | render() { 10 | return node; 11 | } 12 | } 13 | ContextWrapper.childContextTypes = childContextTypes; 14 | return ContextWrapper; 15 | } 16 | -------------------------------------------------------------------------------- /docs/GLOSSARY.md: -------------------------------------------------------------------------------- 1 | # selector 2 | 3 | A Selector in enzyme is similar to a CSS selector, but can be a number of other things as well in 4 | order to easily specify a criteria by which you want to find nodes in an enzyme wrapper. See the 5 | [Selector page](/docs/api/selector.md) for more information. 6 | 7 | # wrapper 8 | 9 | A wrapper refers to the enzyme wrapper class that provides the API. 10 | 11 | 12 | # predicate 13 | 14 | A function that returns true or false 15 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/getNodes.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeGetNodes({ 5 | Wrap, 6 | WrapperName, 7 | }) { 8 | describe('.getNodes()', () => { 9 | it('throws', () => { 10 | const wrapper = Wrap(
); 11 | expect(() => wrapper.getNodes()).to.throw( 12 | `${WrapperName}::getNodes() is no longer supported.`, 13 | ); 14 | }); 15 | }); 16 | } 17 | -------------------------------------------------------------------------------- /packages/enzyme/src/configuration.js: -------------------------------------------------------------------------------- 1 | import validateAdapter from './validateAdapter'; 2 | 3 | let configuration = {}; 4 | 5 | export function get() { 6 | return { ...configuration }; 7 | } 8 | 9 | export function merge(extra) { 10 | if (extra.adapter) { 11 | validateAdapter(extra.adapter); 12 | } 13 | Object.assign(configuration, extra); 14 | } 15 | 16 | export function reset(replacementConfig = {}) { 17 | configuration = {}; 18 | merge(replacementConfig); 19 | } 20 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-utils/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "overrides": [ 7 | { 8 | "files": ["src/createMountWrapper.*"], 9 | "rules": { 10 | "no-underscore-dangle": 0, 11 | }, 12 | }, 13 | ], 14 | "rules": { 15 | "max-len": 0, 16 | }, 17 | "settings": { 18 | "react": { 19 | "version": "detect", 20 | }, 21 | }, 22 | } 23 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.3/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "rules": { 7 | "max-len": 0, 8 | "react/no-deprecated": 0, 9 | "react/no-find-dom-node": 0, 10 | "react/no-multi-comp": 0, 11 | "no-underscore-dangle": 0, 12 | "class-methods-use-this": 0 13 | }, 14 | "settings": { 15 | "react": { 16 | "version": "16.3.0", 17 | }, 18 | }, 19 | } 20 | -------------------------------------------------------------------------------- /docs/guides.md: -------------------------------------------------------------------------------- 1 | # enzyme Guides 2 | 3 | - [*Using enzyme with Browserify*](guides/browserify.md) 4 | - [*Using enzyme with Webpack*](guides/webpack.md) 5 | - [*Using enzyme with JSDOM*](guides/jsdom.md) 6 | - [*Using enzyme with Jest*](guides/jest.md) 7 | - [*Using enzyme with Karma*](guides/karma.md) 8 | - [*Using enzyme with Mocha*](guides/mocha.md) 9 | - [*Using enzyme with React Native*](guides/react-native.md) 10 | - [*Using enzyme with Lab*](guides/lab.md) 11 | - [*Using enzyme with Tape and AVA*](guides/tape-ava.md) 12 | -------------------------------------------------------------------------------- /docs/guides/systemjs.md: -------------------------------------------------------------------------------- 1 | # Using enzyme with SystemJS 2 | 3 | If you are using a test runner that runs code in a browser-based environment, 4 | you may be using [SystemJS]() in order to bundle your React code. 5 | 6 | Prior to enzyme 3.0 there were some issues with conditional requires that were used 7 | to maintain backwards compatibility with React versions. With enzyme 3.0+, this 8 | should no longer be an issue. If it is, please file a GitHub issue or make a PR 9 | to this documentation with instructions on how to set it up. 10 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.1/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "rules": { 7 | "max-classes-per-file": 0, 8 | "max-len": 0, 9 | "react/no-deprecated": 0, 10 | "react/no-find-dom-node": 0, 11 | "react/no-multi-comp": 0, 12 | "no-underscore-dangle": 0, 13 | "class-methods-use-this": 0 14 | }, 15 | "settings": { 16 | "react": { 17 | "version": "16.1.0", 18 | }, 19 | }, 20 | } 21 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16.2/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "rules": { 7 | "max-classes-per-file": 0, 8 | "max-len": 0, 9 | "react/no-deprecated": 0, 10 | "react/no-find-dom-node": 0, 11 | "react/no-multi-comp": 0, 12 | "no-underscore-dangle": 0, 13 | "class-methods-use-this": 0 14 | }, 15 | "settings": { 16 | "react": { 17 | "version": "16.2.0", 18 | }, 19 | }, 20 | } 21 | -------------------------------------------------------------------------------- /book.json: -------------------------------------------------------------------------------- 1 | { 2 | "gitbook": "3.2.2", 3 | "title": "Enzyme", 4 | "description": "React Testing", 5 | "plugins": [ 6 | "edit-link", 7 | "github", 8 | "-search", 9 | "codeblock-disable-glossary", 10 | "collapsible-menu", 11 | "anchors" 12 | ], 13 | "pluginsConfig": { 14 | "edit-link": { 15 | "base": "https://github.com/enzymejs/enzyme/tree/master", 16 | "label": "Edit This Page" 17 | }, 18 | "github": { 19 | "url": "https://github.com/enzymejs/enzyme/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/getDOMNode.md: -------------------------------------------------------------------------------- 1 | # `.getDOMNode() => DOMComponent` 2 | 3 | Returns the outer most DOMComponent of the current wrapper. 4 | 5 | Notes: 6 | - can only be called on a wrapper of a single node. 7 | - will raise if called on a wrapper of a stateless functional component. 8 | 9 | 10 | #### Returns 11 | 12 | `DOMComponent`: The retrieved DOM component. 13 | 14 | 15 | 16 | #### Examples 17 | 18 | ```jsx 19 | const wrapper = mount(); 20 | expect(wrapper.getDOMNode()).to.have.property('className'); 21 | ``` 22 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-14/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "rules": { 7 | "max-len": 0, 8 | "react/no-find-dom-node": 0, 9 | "react/no-multi-comp": 0, 10 | "no-underscore-dangle": 0, 11 | "class-methods-use-this": 0, 12 | "react/no-render-return-value": 0, // react 14's `render` returns a value we use 13 | }, 14 | "settings": { 15 | "react": { 16 | "version": "0.14.0", 17 | }, 18 | }, 19 | } 20 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "rules": { 7 | "max-len": 0, 8 | "react/no-find-dom-node": 0, 9 | "react/no-multi-comp": 0, 10 | "no-underscore-dangle": 0, 11 | "class-methods-use-this": 0, 12 | "react/no-render-return-value": 0, // react 15's `render` returns a value we use 13 | }, 14 | "settings": { 15 | "react": { 16 | "version": "15.5.0", 17 | }, 18 | }, 19 | } 20 | -------------------------------------------------------------------------------- /docs/guides/webpack.md: -------------------------------------------------------------------------------- 1 | # Using enzyme with Webpack 2 | 3 | If you are using a test runner that runs code in a browser-based environment, you may be using 4 | [webpack](https://webpack.js.org/) in order to bundle your React code. 5 | 6 | Prior to enzyme 3.0 there were some issues with conditional requires that were used 7 | to maintain backwards compatibility with React versions. With enzyme 3.0+, this 8 | should no longer be an issue. If it is, please file a GitHub issue or make a PR 9 | to this documentation with instructions on how to set it up. 10 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-15.4/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "ignorePatterns": ["build/"], 5 | "root": true, 6 | "rules": { 7 | "max-len": 0, 8 | "react/no-find-dom-node": 0, 9 | "react/no-multi-comp": 0, 10 | "no-underscore-dangle": 0, 11 | "class-methods-use-this": 0, 12 | "react/no-render-return-value": 0, // react 15's `render` returns a value we use 13 | }, 14 | "settings": { 15 | "react": { 16 | "version": "15.4.0", 17 | }, 18 | }, 19 | } 20 | -------------------------------------------------------------------------------- /docs/guides/browserify.md: -------------------------------------------------------------------------------- 1 | # Using enzyme with Browserify 2 | 3 | If you are using a test runner that runs code in a browser-based environment, you may be using 4 | [browserify](https://browserify.org) in order to bundle your React code. 5 | 6 | Prior to enzyme 3.0 there were some issues with conditional requires that were used 7 | to maintain backwards compatibility with React versions. With enzyme 3.0+, this 8 | should no longer be an issue. If it is, please file a GitHub issue or make a PR 9 | to this documentation with instructions on how to set it up. 10 | -------------------------------------------------------------------------------- /docs/installation/README.md: -------------------------------------------------------------------------------- 1 | # Installation 2 | 3 | enzyme should be installed using npm: 4 | 5 | ```bash 6 | npm i --save-dev enzyme 7 | ``` 8 | 9 | enzyme can be used with your test runner of choice. All examples in the documentation will be 10 | provided using [mocha](https://mochajs.org/) and [BDD style chai](http://chaijs.com/api/bdd/), 11 | although neither library is a dependency of enzyme. 12 | 13 | {% include "./react-16.md" %} 14 | 15 | {% include "./react-15.md" %} 16 | 17 | {% include "./react-014.md" %} 18 | 19 | {% include "./react-013.md" %} 20 | -------------------------------------------------------------------------------- /docs/api/README.md: -------------------------------------------------------------------------------- 1 | # API Reference 2 | 3 | 4 | 5 | ### [Shallow Rendering](shallow.md) 6 | 7 | ```jsx 8 | import { shallow } from 'enzyme'; 9 | 10 | const wrapper = shallow(); 11 | // ... 12 | ``` 13 | 14 | 15 | ### [Full Rendering](mount.md) 16 | 17 | ```jsx 18 | import { mount } from 'enzyme'; 19 | 20 | const wrapper = mount(); 21 | // ... 22 | ``` 23 | 24 | 25 | ### [Static Rendering](render.md) 26 | 27 | ```jsx 28 | import { render } from 'enzyme'; 29 | 30 | const wrapper = render(); 31 | // ... 32 | ``` 33 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/getNode.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeGetNode({ 5 | Wrap, 6 | WrapperName, 7 | isShallow, 8 | }) { 9 | describe('.getNode()', () => { 10 | it('throws', () => { 11 | const wrapper = Wrap(
); 12 | expect(() => wrapper.getNode()).to.throw( 13 | `${WrapperName}::getNode() is no longer supported. Use ${WrapperName}::${isShallow ? 'getElement' : 'instance'}() instead`, 14 | ); 15 | }); 16 | }); 17 | } 18 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-13/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "rules": { 7 | "max-classes-per-file": 0, 8 | "max-len": 0, 9 | "react/no-find-dom-node": 0, 10 | "react/no-multi-comp": 0, 11 | "no-underscore-dangle": 0, 12 | "class-methods-use-this": 0, 13 | "react/no-render-return-value": 0, // react 13's `render` returns a value we use 14 | }, 15 | "settings": { 16 | "react": { 17 | "version": "0.13.0", 18 | }, 19 | }, 20 | } 21 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/first.md: -------------------------------------------------------------------------------- 1 | # `.first() => ReactWrapper` 2 | 3 | Reduce the set of matched nodes to the first in the set, just like `.at(0)`. 4 | 5 | 6 | #### Returns 7 | 8 | `ReactWrapper`: A new wrapper that wraps the first node in the set. 9 | 10 | 11 | #### Examples 12 | 13 | ```jsx 14 | const wrapper = mount(); 15 | expect(wrapper.find(Foo).first().props().foo).to.equal('bar'); 16 | ``` 17 | 18 | 19 | #### Related Methods 20 | 21 | - [`.at(index) => ReactWrapper`](at.md) - retrieve a wrapper node at given index 22 | - [`.last() => ReactWrapper`](last.md) 23 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/is.md: -------------------------------------------------------------------------------- 1 | # `.is(selector) => Boolean` 2 | 3 | Returns whether or not the single wrapped node matches the provided selector. It must be a single-node wrapper. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | 11 | #### Returns 12 | 13 | `Boolean`: whether or not the wrapped node matches the provided selector. 14 | 15 | 16 | #### Example 17 | 18 | 19 | ```jsx 20 | const wrapper = mount(
); 21 | expect(wrapper.is('.some-class')).to.equal(true); 22 | ``` 23 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/last.md: -------------------------------------------------------------------------------- 1 | # `.last() => ReactWrapper` 2 | 3 | Reduce the set of matched nodes to the last in the set, just like `.at(length - 1)`. 4 | 5 | 6 | #### Returns 7 | 8 | `ReactWrapper`: A new wrapper that wraps the last node in the set. 9 | 10 | 11 | #### Examples 12 | 13 | ```jsx 14 | const wrapper = mount(); 15 | expect(wrapper.find(Foo).last().props().foo).to.equal('bar'); 16 | ``` 17 | 18 | 19 | #### Related Methods 20 | 21 | - [`.at(index) => ReactWrapper`](at.md) - retrieve a wrapper node by index 22 | - [`.first() => ReactWrapper`](first.md) 23 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/is.md: -------------------------------------------------------------------------------- 1 | # `.is(selector) => Boolean` 2 | 3 | Returns whether or not the single wrapped node matches the provided selector. It must be a single-node wrapper. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | 11 | #### Returns 12 | 13 | `Boolean`: whether or not the wrapped node matches the provided selector. 14 | 15 | 16 | #### Example 17 | 18 | 19 | ```jsx 20 | const wrapper = shallow(
); 21 | expect(wrapper.is('.some-class')).to.equal(true); 22 | ``` 23 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/first.md: -------------------------------------------------------------------------------- 1 | # `.first() => ShallowWrapper` 2 | 3 | Reduce the set of matched nodes to the first in the set, just like `.at(0)`. 4 | 5 | 6 | #### Returns 7 | 8 | `ShallowWrapper`: A new wrapper that wraps the first node in the set. 9 | 10 | 11 | #### Examples 12 | 13 | ```jsx 14 | const wrapper = shallow(); 15 | expect(wrapper.find(Foo).first().props().foo).to.equal('bar'); 16 | ``` 17 | 18 | 19 | #### Related Methods 20 | 21 | - [`.at(index) => ShallowWrapper`](at.md) - retrieve a wrapper node at given index 22 | - [`.last() => ShallowWrapper`](last.md) 23 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/last.md: -------------------------------------------------------------------------------- 1 | # `.last() => ShallowWrapper` 2 | 3 | Reduce the set of matched nodes to the last in the set, just like `.at(length - 1)`. 4 | 5 | 6 | #### Returns 7 | 8 | `ShallowWrapper`: A new wrapper that wraps the last node in the set. 9 | 10 | 11 | #### Examples 12 | 13 | ```jsx 14 | const wrapper = shallow(); 15 | expect(wrapper.find(Foo).last().props().foo).to.equal('bar'); 16 | ``` 17 | 18 | 19 | #### Related Methods 20 | 21 | - [`.at(index) => ShallowWrapper`](at.md) - retrieve a wrapper node by index 22 | - [`.first() => ShallowWrapper`](first.md) 23 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/test/.setup.js: -------------------------------------------------------------------------------- 1 | import { JSDOM } from 'jsdom'; 2 | import { expect } from 'chai'; 3 | const jsdom = new JSDOM(''); 4 | const { window } = jsdom; 5 | 6 | function copyProps(src, target) { 7 | Object.defineProperties(target, { 8 | ...Object.getOwnPropertyDescriptors(src), 9 | ...Object.getOwnPropertyDescriptors(target), 10 | }); 11 | } 12 | 13 | global.expect = expect; 14 | global.window = window; 15 | global.document = window.document; 16 | global.navigator = { 17 | userAgent: 'node.js', 18 | }; 19 | copyProps(window, global); 20 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/parent.md: -------------------------------------------------------------------------------- 1 | # `.parent() => ReactWrapper` 2 | 3 | Returns a wrapper with the direct parent of the node in the current wrapper. 4 | 5 | 6 | #### Returns 7 | 8 | `ReactWrapper`: A new wrapper that wraps the resulting nodes. 9 | 10 | 11 | #### Examples 12 | 13 | ```jsx 14 | const wrapper = mount(); 15 | expect(wrapper.find('ul').parent().is('div')).to.equal(true); 16 | ``` 17 | 18 | #### Related Methods 19 | 20 | - [`.parents([selector]) => ReactWrapper`](parents.md) 21 | - [`.children([selector]) => ReactWrapper`](children.md) 22 | - [`.closest(selector) => ReactWrapper`](closest.md) 23 | -------------------------------------------------------------------------------- /.nycrc: -------------------------------------------------------------------------------- 1 | { 2 | "extension": [ 3 | ".js", 4 | ".jsx" 5 | ], 6 | "include": [ 7 | "*", 8 | "packages/*/*", 9 | "packages/*/src" 10 | ], 11 | "exclude": [ 12 | "packages/enzyme-test-suite", 13 | "_book", 14 | "coverage", 15 | "packages/*/node_modules" 16 | ], 17 | "require": [ 18 | ], 19 | "reporter": [ 20 | "text", 21 | "html", 22 | "json", 23 | "lcov" 24 | ], 25 | "all": false, 26 | "check-coverage": false, 27 | "statements": 100, 28 | "branches": 100, 29 | "lines": 100, 30 | "functions": 100, 31 | "sourceMap": true, 32 | "instrument": true 33 | } 34 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/get.md: -------------------------------------------------------------------------------- 1 | # `.get(index) => ReactElement` 2 | 3 | Returns the node at a given index of the current wrapper. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `index` (`Number`): A zero-based integer indicating which node to retrieve. 9 | 10 | 11 | #### Returns 12 | 13 | `ReactElement`: The retrieved node. 14 | 15 | 16 | #### Examples 17 | 18 | ```jsx 19 | const wrapper = mount(); 20 | expect(wrapper.find(Foo).get(0).props.foo).to.equal('bar'); 21 | ``` 22 | 23 | 24 | #### Related Methods 25 | 26 | - [`.at(index) => ReactWrapper`](at.md) - same, but returns the React node in a single-node wrapper. 27 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [ljharb] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: npm/enzyme 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/get.md: -------------------------------------------------------------------------------- 1 | # `.get(index) => ReactElement` 2 | 3 | Returns the node at a given index of the current wrapper. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `index` (`Number`): A zero-based integer indicating which node to retrieve. 9 | 10 | 11 | #### Returns 12 | 13 | `ReactElement`: The retrieved node. 14 | 15 | 16 | #### Examples 17 | 18 | ```jsx 19 | const wrapper = shallow(); 20 | expect(wrapper.find(Foo).get(0).props.foo).to.equal('bar'); 21 | ``` 22 | 23 | 24 | #### Related Methods 25 | 26 | - [`.at(index) => ShallowWrapper`](at.md) - same, but returns the React node in a single-node wrapper. 27 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/parent.md: -------------------------------------------------------------------------------- 1 | # `.parent() => ShallowWrapper` 2 | 3 | Returns a wrapper with the direct parent of the node in the current wrapper. 4 | 5 | 6 | #### Returns 7 | 8 | `ShallowWrapper`: A new wrapper that wraps the resulting nodes. 9 | 10 | 11 | #### Examples 12 | 13 | ```jsx 14 | const wrapper = shallow(); 15 | expect(wrapper.find('ul').parent().is('div')).to.equal(true); 16 | ``` 17 | 18 | #### Related Methods 19 | 20 | - [`.parents([selector]) => ShallowWrapper`](parents.md) 21 | - [`.children([selector]) => ShallowWrapper`](children.md) 22 | - [`.closest(selector) => ShallowWrapper`](closest.md) 23 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/version.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import semver from 'semver'; 3 | 4 | export const VERSION = React.version; 5 | 6 | export function is(range) { 7 | if (/&&/.test(range)) { 8 | throw new RangeError('&& may not work properly in ranges, apparently'); 9 | } 10 | return semver.satisfies(VERSION, range); 11 | } 12 | 13 | export const REACT16 = is('16'); 14 | 15 | // The shallow renderer in react 16 does not yet support batched updates. When it does, 16 | // we should be able to go un-skip all of the tests that are skipped with this flag. 17 | export const BATCHING = !REACT16; 18 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/childAt.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeChildAt({ 5 | Wrap, 6 | }) { 7 | describe('.childAt(index)', () => { 8 | it('gets a wrapped node at the specified index', () => { 9 | const wrapper = Wrap(( 10 |
11 |
12 |
13 |
14 | )); 15 | 16 | expect(wrapper.childAt(0).hasClass('bar')).to.equal(true); 17 | expect(wrapper.childAt(1).hasClass('baz')).to.equal(true); 18 | }); 19 | }); 20 | } 21 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/last.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeLast({ 5 | Wrap, 6 | }) { 7 | describe('.last()', () => { 8 | it('returns the last node in the current set', () => { 9 | const wrapper = Wrap(( 10 |
11 |
12 |
13 |
14 |
15 |
16 | )); 17 | expect(wrapper.find('.bar').last().hasClass('baz')).to.equal(true); 18 | }); 19 | }); 20 | } 21 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-16/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "rules": { 7 | "max-classes-per-file": 0, 8 | "max-len": 0, 9 | "import/no-extraneous-dependencies": 2, 10 | "import/no-unresolved": 2, 11 | "import/extensions": 2, 12 | "react/no-deprecated": 0, 13 | "react/no-find-dom-node": 0, 14 | "react/no-multi-comp": 0, 15 | "no-underscore-dangle": 0, 16 | "class-methods-use-this": 0 17 | }, 18 | "settings": { 19 | "react": { 20 | "version": "16", 21 | }, 22 | }, 23 | } 24 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/first.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeFirst({ 5 | Wrap, 6 | }) { 7 | describe('.first()', () => { 8 | it('returns the first node in the current set', () => { 9 | const wrapper = Wrap(( 10 |
11 |
12 |
13 |
14 |
15 |
16 | )); 17 | expect(wrapper.find('.bar').first().hasClass('baz')).to.equal(true); 18 | }); 19 | }); 20 | } 21 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/prop.md: -------------------------------------------------------------------------------- 1 | # `.prop(key) => Any` 2 | 3 | Returns the prop value for the root node of the wrapper with the provided key. It must be a single-node wrapper. 4 | 5 | #### Arguments 6 | 7 | 1. `key` (`String`): The prop name, that is, `this.props[key]` or `props[key]` for the root node of the wrapper. 8 | 9 | 10 | #### Example 11 | 12 | 13 | ```jsx 14 | const wrapper = mount(); 15 | expect(wrapper.prop('foo')).to.equal(10); 16 | ``` 17 | 18 | 19 | #### Related Methods 20 | 21 | - [`.props() => Object`](props.md) 22 | - [`.state([key]) => Any`](state.md) 23 | - [`.context([key]) => Any`](context.md) 24 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/tap.md: -------------------------------------------------------------------------------- 1 | # `.tap(intercepter) => Self` 2 | 3 | Invokes intercepter and returns itself. intercepter is called with itself. 4 | This is helpful when debugging nodes in method chains. 5 | 6 | #### Arguments 7 | 8 | 1. `intercepter` (`Self`): the current ReactWrapper instance. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Self`: the current ReactWrapper instance. 15 | 16 | 17 | 18 | #### Example 19 | 20 | 21 | ```jsx 22 | const result = mount(( 23 |
    24 |
  • xxx
  • 25 |
  • yyy
  • 26 |
  • zzz
  • 27 |
28 | )).find('li') 29 | .tap((n) => console.log(n.debug())) 30 | .map((n) => n.text()); 31 | ``` 32 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/tap.md: -------------------------------------------------------------------------------- 1 | # `.tap(intercepter) => Self` 2 | 3 | Invokes intercepter and returns itself. intercepter is called with itself. 4 | This is helpful when debugging nodes in method chains. 5 | 6 | #### Arguments 7 | 8 | 1. `intercepter` (`Self`): the current ShallowWrapper instance. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Self`: the current ShallowWrapper instance. 15 | 16 | 17 | 18 | #### Example 19 | 20 | 21 | ```jsx 22 | const result = shallow(( 23 |
    24 |
  • xxx
  • 25 |
  • yyy
  • 26 |
  • zzz
  • 27 |
28 | )).find('li') 29 | .tap((n) => console.log(n.debug())) 30 | .map((n) => n.text()); 31 | ``` 32 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 🙏 Feature request 3 | about: Suggest an idea for this project 4 | 5 | --- 6 | 7 | **Is your feature request related to a problem? Please describe.** 8 | A clear and concise description of what the problem is. e.g. I'm always frustrated when [...] 9 | 10 | **Describe the solution you'd like** 11 | A clear and concise description of what you want to happen. 12 | 13 | **Describe alternatives you've considered** 14 | A clear and concise description of any alternative solutions or features you've considered. 15 | 16 | **Additional context** 17 | Add any other context or screenshots about the feature request here. 18 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/filter.md: -------------------------------------------------------------------------------- 1 | # `.filter(selector) => ReactWrapper` 2 | 3 | Returns a new wrapper with only the nodes of the current wrapper that match the provided selector. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `ReactWrapper`: A new wrapper that wraps the filtered nodes. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = mount(); 22 | expect(wrapper.find('.foo').filter('.bar')).to.have.lengthOf(1); 23 | ``` 24 | 25 | #### Related Methods 26 | 27 | - [`.filterWhere(predicate) => ReactWrapper`](filterWhere.md) 28 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/filter.md: -------------------------------------------------------------------------------- 1 | # `.filter(selector) => ShallowWrapper` 2 | 3 | Returns a new wrapper with only the nodes of the current wrapper that match the provided selector. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `ShallowWrapper`: A new wrapper that wraps the filtered nodes. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = shallow(); 22 | expect(wrapper.find('.foo').filter('.bar')).to.have.lengthOf(1); 23 | ``` 24 | 25 | #### Related Methods 26 | 27 | - [`.filterWhere(predicate) => ShallowWrapper`](filterWhere.md) 28 | -------------------------------------------------------------------------------- /.github/workflows/node-pretest.yml: -------------------------------------------------------------------------------- 1 | name: 'Tests: pretest/posttest' 2 | 3 | on: [pull_request, push] 4 | 5 | jobs: 6 | pretest: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - uses: actions/checkout@v3 11 | - uses: ljharb/actions/node/install@main 12 | with: 13 | skip-ls-check: true 14 | - run: sh install-relevant-react.sh 15 | - run: npx lerna bootstrap 16 | - run: npm run build 17 | - run: npm run pretest 18 | 19 | # posttest: 20 | # runs-on: ubuntu-latest 21 | 22 | # steps: 23 | # - uses: actions/checkout@v3 24 | # - uses: ljharb/actions/node/install@main 25 | # - run: npm run build 26 | # - run: npm run posttest 27 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/state.md: -------------------------------------------------------------------------------- 1 | # `.state([key]) => Any` 2 | 3 | Returns the state hash for the root node of the wrapper. Optionally pass in a prop name and it 4 | will return just that value. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `key` (`String` [optional]): If provided, the return value will be the `this.state[key]` of the 10 | root component instance. 11 | 12 | 13 | 14 | #### Example 15 | 16 | 17 | ```jsx 18 | const wrapper = mount(); 19 | expect(wrapper.state().foo).to.equal(10); 20 | expect(wrapper.state('foo')).to.equal(10); 21 | ``` 22 | 23 | 24 | #### Related Methods 25 | 26 | - [`.props() => Object`](props.md) 27 | - [`.prop(key) => Any`](prop.md) 28 | - [`.context([key]) => Any`](context.md) 29 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/render.md: -------------------------------------------------------------------------------- 1 | # `.render() => CheerioWrapper` 2 | 3 | Returns a CheerioWrapper around the rendered HTML of the single node's subtree. 4 | It must be a single-node wrapper. 5 | 6 | 7 | #### Returns 8 | 9 | `CheerioWrapper`: The resulting Cheerio object 10 | 11 | 12 | #### Examples 13 | 14 | ```jsx 15 | function Foo() { 16 | return (
); 17 | } 18 | ``` 19 | 20 | ```jsx 21 | function Bar() { 22 | return ( 23 |
24 | 25 |
26 | ); 27 | } 28 | ``` 29 | 30 | ```jsx 31 | const wrapper = mount(); 32 | expect(wrapper.find('.in-foo')).to.have.lengthOf(1); 33 | expect(wrapper.render().find('.in-foo')).to.have.lengthOf(1); 34 | ``` 35 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/state.md: -------------------------------------------------------------------------------- 1 | # `.state([key]) => Any` 2 | 3 | Returns the state hash for the root node of the wrapper. Optionally pass in a prop name and it 4 | will return just that value. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `key` (`String` [optional]): If provided, the return value will be the `this.state[key]` of the 10 | root component instance. 11 | 12 | 13 | 14 | #### Example 15 | 16 | 17 | ```jsx 18 | const wrapper = shallow(); 19 | expect(wrapper.state().foo).to.equal(10); 20 | expect(wrapper.state('foo')).to.equal(10); 21 | ``` 22 | 23 | 24 | #### Related Methods 25 | 26 | - [`.props() => Object`](props.md) 27 | - [`.prop(key) => Any`](prop.md) 28 | - [`.context([key]) => Any`](context.md) 29 | -------------------------------------------------------------------------------- /docs/guides/mocha.md: -------------------------------------------------------------------------------- 1 | # Using enzyme with Mocha 2 | 3 | enzyme was originally designed to work with Mocha, so getting it up and running with Mocha should 4 | be no problem at all. Simply install it and start using it: 5 | 6 | ```bash 7 | npm i --save-dev enzyme 8 | ``` 9 | 10 | ```jsx 11 | import React from 'react'; 12 | import { expect } from 'chai'; 13 | import { mount } from 'enzyme'; 14 | import { spy } from 'sinon'; 15 | import Foo from './src/Foo'; 16 | 17 | spy(Foo.prototype, 'componentDidMount'); 18 | 19 | describe('', () => { 20 | it('calls componentDidMount', () => { 21 | const wrapper = mount(); 22 | expect(Foo.prototype.componentDidMount).to.have.property('callCount', 1); 23 | }); 24 | }); 25 | ``` -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/tap.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | import sinon from 'sinon-sandbox'; 4 | 5 | export default function describeTap({ 6 | Wrap, 7 | }) { 8 | describe('.tap()', () => { 9 | it('calls the passed function with current Wrapper and returns itself', () => { 10 | const spy = sinon.spy(); 11 | const wrapper = Wrap(( 12 |
    13 |
  • xxx
  • 14 |
  • yyy
  • 15 |
  • zzz
  • 16 |
17 | )).find('li'); 18 | const result = wrapper.tap(spy); 19 | expect(spy.calledWith(wrapper)).to.equal(true); 20 | expect(result).to.equal(wrapper); 21 | }); 22 | }); 23 | } 24 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/exists.md: -------------------------------------------------------------------------------- 1 | # `.exists([selector]) => Boolean` 2 | 3 | Returns whether or not any nodes exist in the wrapper. Or, if a selector is passed in, whether that selector has any matches in the wrapper. 4 | 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): The selector to check existence for. 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `Boolean`: whether or not any nodes are on the list, or the selector had any matches. 16 | 17 | 18 | 19 | #### Example 20 | 21 | 22 | ```jsx 23 | const wrapper = mount(
); 24 | expect(wrapper.exists('.some-class')).to.equal(true); 25 | expect(wrapper.find('.other-class').exists()).to.equal(false); 26 | ``` 27 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/render.md: -------------------------------------------------------------------------------- 1 | # `.render() => CheerioWrapper` 2 | 3 | Returns a CheerioWrapper around the rendered HTML of the single node's subtree. 4 | It must be a single-node wrapper. 5 | 6 | 7 | #### Returns 8 | 9 | `CheerioWrapper`: The resulting Cheerio object 10 | 11 | 12 | #### Examples 13 | 14 | ```jsx 15 | function Foo() { 16 | return (
); 17 | } 18 | ``` 19 | 20 | ```jsx 21 | function Bar() { 22 | return ( 23 |
24 | 25 |
26 | ); 27 | } 28 | ``` 29 | 30 | ```jsx 31 | const wrapper = shallow(); 32 | expect(wrapper.find('.in-foo')).to.have.lengthOf(0); 33 | expect(wrapper.render().find('.in-foo')).to.have.lengthOf(1); 34 | ``` 35 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/getElement.md: -------------------------------------------------------------------------------- 1 | # `.getElement() => ReactElement` 2 | 3 | Returns the wrapped ReactElement. 4 | 5 | If the current wrapper is wrapping the root component, returns the root component's latest render output. 6 | 7 | 8 | #### Returns 9 | 10 | `ReactElement`: The retrieved ReactElement. 11 | 12 | 13 | 14 | #### Examples 15 | 16 | ```jsx 17 | const element = ( 18 |
19 | 20 | 21 |
22 | ); 23 | 24 | function MyComponent() { 25 | return element; 26 | } 27 | 28 | const wrapper = mount(); 29 | expect(wrapper.getElement()).to.equal(element); 30 | ``` 31 | 32 | 33 | 34 | #### Related Methods 35 | 36 | - [`.getElements() => Array`](getElements.md) 37 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/exists.md: -------------------------------------------------------------------------------- 1 | # `.exists([selector]) => Boolean` 2 | 3 | Returns whether or not any nodes exist in the wrapper. Or, if a selector is passed in, whether that selector has any matches in the wrapper. 4 | 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): The selector to check existence for. 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `Boolean`: whether or not any nodes are on the list, or the selector had any matches. 16 | 17 | 18 | 19 | #### Example 20 | 21 | 22 | ```jsx 23 | const wrapper = shallow(
); 24 | expect(wrapper.exists('.some-class')).to.equal(true); 25 | expect(wrapper.find('.other-class').exists()).to.equal(false); 26 | ``` 27 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/every.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeEvery({ 5 | Wrap, 6 | }) { 7 | describe('.every(selector)', () => { 8 | it('returns if every node matches a selector', () => { 9 | const wrapper = Wrap(( 10 |
11 |
12 |
13 |
14 |
15 | )); 16 | expect(wrapper.find('.foo').every('.foo')).to.equal(true); 17 | expect(wrapper.find('.foo').every('.qoo')).to.equal(false); 18 | expect(wrapper.find('.foo').every('.bar')).to.equal(false); 19 | }); 20 | }); 21 | } 22 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/getElement.md: -------------------------------------------------------------------------------- 1 | # `.getElement() => ReactElement` 2 | 3 | Returns the wrapped ReactElement. 4 | 5 | If the current wrapper is wrapping the root component, returns the root component's latest render output. 6 | 7 | 8 | #### Returns 9 | 10 | `ReactElement`: The retrieved ReactElement. 11 | 12 | 13 | 14 | #### Examples 15 | 16 | ```jsx 17 | const element = ( 18 |
19 | 20 | 21 |
22 | ); 23 | 24 | function MyComponent() { 25 | return element; 26 | } 27 | 28 | const wrapper = shallow(); 29 | expect(wrapper.getElement()).to.equal(element); 30 | ``` 31 | 32 | 33 | 34 | #### Related Methods 35 | 36 | - [`.getElements() => Array`](getElements.md) 37 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/src/Foo.spec.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import Enzyme, { shallow, mount } from 'enzyme'; 3 | import Adapter from 'enzyme-adapter-react-16'; 4 | import { expect } from 'chai'; 5 | import Foo from './Foo'; 6 | 7 | Enzyme.configure({ adapter: new Adapter() }); 8 | 9 | describe('A suite', () => { 10 | it('contains spec with an expectation', () => { 11 | expect(shallow().contains(
)).to.equal(true); 12 | }); 13 | 14 | it('contains spec with an expectation', () => { 15 | expect(shallow().is('.foo')).to.equal(true); 16 | }); 17 | 18 | it('contains spec with an expectation', () => { 19 | expect(mount().find('.foo').length).to.equal(1); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/findWhere.md: -------------------------------------------------------------------------------- 1 | # `.findWhere(fn) => ReactWrapper` 2 | 3 | Finds every node in the render tree that returns true for the provided predicate function. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `predicate` (`ReactWrapper => Boolean`): A predicate function called with the passed in wrapped 9 | nodes. 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `ReactWrapper`: A new wrapper that wraps the found nodes. 16 | 17 | 18 | 19 | #### Example 20 | 21 | ```jsx 22 | const wrapper = mount(); 23 | const complexComponents = wrapper.findWhere((n) => typeof n.type() !== 'string'); 24 | expect(complexComponents).to.have.lengthOf(8); 25 | ``` 26 | 27 | 28 | #### Related Methods 29 | 30 | - [`.find(selector) => ReactWrapper`](find.md) 31 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/findWhere.md: -------------------------------------------------------------------------------- 1 | # `.findWhere(fn) => ShallowWrapper` 2 | 3 | Finds every node in the render tree that returns true for the provided predicate function. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `predicate` (`ShallowWrapper => Boolean`): A predicate function called with the passed in wrapped 9 | nodes. 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `ShallowWrapper`: A new wrapper that wraps the found nodes. 16 | 17 | 18 | 19 | #### Example 20 | 21 | ```jsx 22 | const wrapper = shallow(); 23 | const complexComponents = wrapper.findWhere((n) => n.type() !== 'string'); 24 | expect(complexComponents).to.have.lengthOf(8); 25 | ``` 26 | 27 | 28 | #### Related Methods 29 | 30 | - [`.find(selector) => ShallowWrapper`](find.md) 31 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/childAt.md: -------------------------------------------------------------------------------- 1 | # `.childAt(index) => ReactWrapper` 2 | 3 | Returns a new wrapper with child at the specified index. 4 | 5 | #### Arguments 6 | 7 | 1. `index` (`number`): A zero-based integer indicating which node to retrieve. 8 | 9 | 10 | #### Returns 11 | 12 | `ReactWrapper`: A new wrapper that wraps the resulting node. 13 | 14 | 15 | 16 | #### Examples 17 | 18 | ```jsx 19 | const wrapper = mount(); 20 | expect(wrapper.find('ul').childAt(0).type()).to.equal('li'); 21 | ``` 22 | 23 | #### Related Methods 24 | 25 | - [`.parents([selector]) => ReactWrapper`](parents.md) 26 | - [`.parent() => ReactWrapper`](parent.md) 27 | - [`.closest(selector) => ReactWrapper`](closest.md) 28 | - [`.children([selector]) => ReactWrapper`](children.md) 29 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/context.md: -------------------------------------------------------------------------------- 1 | # `.context([key]) => Any` 2 | 3 | Returns the context hash for the root node of the wrapper. Optionally pass in a prop name and it 4 | will return just that value. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `key` (`String` [optional]): If provided, the return value will be the `this.context[key]` of the 10 | root component instance. 11 | 12 | 13 | 14 | #### Example 15 | 16 | 17 | ```jsx 18 | const wrapper = mount( 19 | , 20 | { context: { foo: 10 } }, 21 | ); 22 | 23 | expect(wrapper.context().foo).to.equal(10); 24 | expect(wrapper.context('foo')).to.equal(10); 25 | ``` 26 | 27 | 28 | #### Related Methods 29 | 30 | - [`.state([key]) => Any`](state.md) 31 | - [`.props() => Object`](props.md) 32 | - [`.prop(key) => Any`](prop.md) 33 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/context.md: -------------------------------------------------------------------------------- 1 | # `.context([key]) => Any` 2 | 3 | Returns the context hash for the root node of the wrapper. Optionally pass in a prop name and it 4 | will return just that value. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `key` (`String` [optional]): If provided, the return value will be the `this.context[key]` of the 10 | root component instance. 11 | 12 | 13 | 14 | #### Example 15 | 16 | 17 | ```jsx 18 | const wrapper = shallow( 19 | , 20 | { context: { foo: 10 } }, 21 | ); 22 | expect(wrapper.context().foo).to.equal(10); 23 | expect(wrapper.context('foo')).to.equal(10); 24 | ``` 25 | 26 | 27 | #### Related Methods 28 | 29 | - [`.props() => Object`](props.md) 30 | - [`.prop(key) => Any`](prop.md) 31 | - [`.state([key]) => Any`](state.md) 32 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/at.md: -------------------------------------------------------------------------------- 1 | # `.at(index) => ReactWrapper` 2 | 3 | Returns a wrapper around the node at a given index of the current wrapper. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `index` (`Number`): A zero-based integer indicating which node to retrieve. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `ReactWrapper`: A new wrapper that wraps the retrieved node. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = mount(); 22 | expect(wrapper.find(Foo).at(0).props().foo).to.equal('bar'); 23 | ``` 24 | 25 | 26 | 27 | #### Related Methods 28 | 29 | - [`.get(index) => ReactElement`](get.md) - same, but returns the React node itself, with no wrapper. 30 | - [`.first() => ReactWrapper`](first.md) - same as at(0) 31 | - [`.last() => ReactWrapper`](last.md) 32 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/hostNodes.md: -------------------------------------------------------------------------------- 1 | # `.hostNodes() => ReactWrapper` 2 | 3 | Returns a new wrapper with only host nodes. 4 | When using `react-dom`, host nodes are HTML elements rather than custom React components, e.g. `
` versus ``. 5 | 6 | 7 | #### Returns 8 | 9 | `ReactWrapper`: A new wrapper that wraps the filtered nodes. 10 | 11 | 12 | #### Examples 13 | 14 | The following code takes a wrapper with two nodes, one a `` React component, and the other a ``, and filters out the React component. 15 | 16 | ```jsx 17 | const wrapper = mount(( 18 |
19 | 20 | 21 |
22 | )); 23 | const twoNodes = wrapper.find('.foo'); 24 | expect(twoNodes.hostNodes()).to.have.lengthOf(1); 25 | ``` 26 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/childAt.md: -------------------------------------------------------------------------------- 1 | # `.childAt(index) => ShallowWrapper` 2 | 3 | Returns a new wrapper with child at the specified index. 4 | 5 | #### Arguments 6 | 7 | 1. `index` (`number`): A zero-based integer indicating which node to retrieve. 8 | 9 | 10 | #### Returns 11 | 12 | `ShallowWrapper`: A new wrapper that wraps the resulting node. 13 | 14 | 15 | 16 | #### Examples 17 | 18 | ```jsx 19 | const wrapper = shallow(); 20 | expect(wrapper.find('ul').childAt(0).type()).to.equal('li'); 21 | ``` 22 | 23 | #### Related Methods 24 | 25 | - [`.parents([selector]) => ShallowWrapper`](parents.md) 26 | - [`.parent() => ShallowWrapper`](parent.md) 27 | - [`.closest(selector) => ShallowWrapper`](closest.md) 28 | - [`.children([selector]) => ShallowWrapper`](children.md) 29 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/getElements.md: -------------------------------------------------------------------------------- 1 | # `.getElements() => Array` 2 | 3 | Returns the wrapped ReactElements 4 | 5 | If the current wrapper is wrapping the root component, returns the root component's latest render output wrapped in an array. 6 | 7 | 8 | #### Returns 9 | 10 | `Array`: The retrieved ReactElements. 11 | 12 | 13 | 14 | #### Examples 15 | 16 | ```jsx 17 | const one = ; 18 | const two = ; 19 | 20 | function Test() { 21 | return ( 22 |
23 | {one} 24 | {two} 25 |
26 | ); 27 | } 28 | 29 | const wrapper = mount(); 30 | expect(wrapper.find('span').getElements()).to.deep.equal([one, two]); 31 | ``` 32 | 33 | 34 | 35 | #### Related Methods 36 | 37 | - [`.getElement() => ReactElement`](getElement.md) 38 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/at.md: -------------------------------------------------------------------------------- 1 | # `.at(index) => ShallowWrapper` 2 | 3 | Returns a wrapper around the node at a given index of the current wrapper. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `index` (`Number`): A zero-based integer indicating which node to retrieve. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `ShallowWrapper`: A new wrapper that wraps the retrieved node. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = shallow(); 22 | expect(wrapper.find(Foo).at(0).props().foo).to.equal('bar'); 23 | ``` 24 | 25 | 26 | 27 | #### Related Methods 28 | 29 | - [`.get(index) => ReactElement`](get.md) - same, but returns the React node itself, with no wrapper. 30 | - [`.first() => ShallowWrapper`](first.md) - same as at(0) 31 | - [`.last() => ShallowWrapper`](last.md) 32 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/getElements.md: -------------------------------------------------------------------------------- 1 | # `.getElements() => Array` 2 | 3 | Returns the wrapped ReactElements 4 | 5 | If the current wrapper is wrapping the root component, returns the root component's latest render output wrapped in an array. 6 | 7 | 8 | #### Returns 9 | 10 | `Array`: The retrieved ReactElements. 11 | 12 | 13 | 14 | #### Examples 15 | 16 | ```jsx 17 | const one = ; 18 | const two = ; 19 | 20 | function Test() { 21 | return ( 22 |
23 | {one} 24 | {two} 25 |
26 | ); 27 | } 28 | 29 | const wrapper = shallow(); 30 | expect(wrapper.find('span').getElements()).to.deep.equal([one, two]); 31 | ``` 32 | 33 | 34 | 35 | #### Related Methods 36 | 37 | - [`.getElement() => ReactElement`](getElement.md) 38 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/hostNodes.md: -------------------------------------------------------------------------------- 1 | # `.hostNodes() => ShallowWrapper` 2 | 3 | Returns a new wrapper with only host nodes. 4 | When using `react-dom`, host nodes are HTML elements rather than custom React components, e.g. `
` versus ``. 5 | 6 | 7 | #### Returns 8 | 9 | `ShallowWrapper`: A new wrapper that wraps the filtered nodes. 10 | 11 | 12 | #### Examples 13 | 14 | The following code takes a wrapper with two nodes, one a `` React component, and the other a ``, and filters out the React component. 15 | 16 | ```jsx 17 | const wrapper = shallow(( 18 |
19 | 20 | 21 |
22 | )); 23 | const twoNodes = wrapper.find('.foo'); 24 | expect(twoNodes.hostNodes()).to.have.lengthOf(1); 25 | ``` 26 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/filterWhere.md: -------------------------------------------------------------------------------- 1 | # `.filterWhere(fn) => ReactWrapper` 2 | 3 | Returns a new wrapper with only the nodes of the current wrapper that, when passed into the 4 | provided predicate function, return true. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `predicate` (`ReactWrapper => Boolean`): A predicate function that is passed a wrapped node. 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `ReactWrapper`: A new wrapper that wraps the filtered nodes. 16 | 17 | 18 | 19 | #### Example 20 | 21 | ```jsx 22 | const wrapper = mount(); 23 | const complexComponents = wrapper.find('.foo').filterWhere((n) => typeof n.type() !== 'string'); 24 | expect(complexComponents).to.have.lengthOf(4); 25 | ``` 26 | 27 | 28 | #### Related Methods 29 | 30 | - [`.filter(selector) => ReactWrapper`](filter.md) 31 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/filterWhere.md: -------------------------------------------------------------------------------- 1 | # `.filterWhere(fn) => ShallowWrapper` 2 | 3 | Returns a new wrapper with only the nodes of the current wrapper that, when passed into the 4 | provided predicate function, return true. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `predicate` (`ShallowWrapper => Boolean`): A predicate function that is passed a wrapped node. 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `ShallowWrapper`: A new wrapper that wraps the filtered nodes. 16 | 17 | 18 | 19 | #### Example 20 | 21 | ```jsx 22 | const wrapper = shallow(); 23 | const complexFoo = wrapper.find('.foo').filterWhere((n) => typeof n.type() !== 'string'); 24 | expect(complexFoo).to.have.lengthOf(4); 25 | ``` 26 | 27 | 28 | #### Related Methods 29 | 30 | - [`.filter(selector) => ShallowWrapper`](filter.md) 31 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/someWhere.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeSomeWhere({ 5 | Wrap, 6 | }) { 7 | describe('.someWhere(predicate)', () => { 8 | it('returns if a node matches a predicate', () => { 9 | const wrapper = Wrap(( 10 |
11 |
12 |
13 |
14 |
15 | )); 16 | const foo = wrapper.find('.foo'); 17 | expect(foo.someWhere((n) => n.hasClass('qoo'))).to.equal(true); 18 | expect(foo.someWhere((n) => n.hasClass('foo'))).to.equal(true); 19 | expect(foo.someWhere((n) => n.hasClass('bar'))).to.equal(false); 20 | }); 21 | }); 22 | } 23 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/not.md: -------------------------------------------------------------------------------- 1 | # `.not(selector) => ReactWrapper` 2 | 3 | Returns a new wrapper with only the nodes of the current wrapper that don't match the provided 4 | selector. 5 | 6 | This method is effectively the negation or inverse of [`filter`](filter.md). 7 | 8 | 9 | #### Arguments 10 | 11 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 12 | 13 | 14 | 15 | #### Returns 16 | 17 | `ReactWrapper`: A new wrapper that wraps the filtered nodes. 18 | 19 | 20 | 21 | #### Examples 22 | 23 | ```jsx 24 | const wrapper = mount(); 25 | expect(wrapper.find('.foo').not('.bar')).to.have.lengthOf(1); 26 | ``` 27 | 28 | #### Related Methods 29 | 30 | - [`.filterWhere(predicate) => ReactWrapper`](filterWhere.md) 31 | - [`.filter(selector) => ReactWrapper`](filter.md) 32 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/everyWhere.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeEveryWhere({ 5 | Wrap, 6 | }) { 7 | describe('.everyWhere(predicate)', () => { 8 | it('returns if every node matches a predicate', () => { 9 | const wrapper = Wrap(( 10 |
11 |
12 |
13 |
14 |
15 | )); 16 | const foo = wrapper.find('.foo'); 17 | expect(foo.everyWhere((n) => n.hasClass('foo'))).to.equal(true); 18 | expect(foo.everyWhere((n) => n.hasClass('qoo'))).to.equal(false); 19 | expect(foo.everyWhere((n) => n.hasClass('bar'))).to.equal(false); 20 | }); 21 | }); 22 | } 23 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/not.md: -------------------------------------------------------------------------------- 1 | # `.not(selector) => ShallowWrapper` 2 | 3 | Returns a new wrapper with only the nodes of the current wrapper that don't match the provided 4 | selector. 5 | 6 | This method is effectively the negation or inverse of [`filter`](filter.md). 7 | 8 | 9 | #### Arguments 10 | 11 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 12 | 13 | 14 | 15 | #### Returns 16 | 17 | `ShallowWrapper`: A new wrapper that wraps the filtered nodes. 18 | 19 | 20 | 21 | #### Examples 22 | 23 | ```jsx 24 | const wrapper = shallow(); 25 | expect(wrapper.find('.foo').not('.bar')).to.have.lengthOf(1); 26 | ``` 27 | 28 | #### Related Methods 29 | 30 | - [`.filterWhere(predicate) => ShallowWrapper`](filterWhere.md) 31 | - [`.filter(selector) => ShallowWrapper`](filter.md) 32 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/not.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeNot({ 5 | Wrap, 6 | }) { 7 | describe('.not(selector)', () => { 8 | it('filters to things not matching a selector', () => { 9 | const wrapper = Wrap(( 10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | )); 18 | 19 | expect(wrapper.find('.foo').not('.bar')).to.have.lengthOf(1); 20 | expect(wrapper.find('.baz').not('.foo')).to.have.lengthOf(2); 21 | expect(wrapper.find('.foo').not('div')).to.have.lengthOf(0); 22 | }); 23 | }); 24 | } 25 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/closest.md: -------------------------------------------------------------------------------- 1 | # `.closest(selector) => ReactWrapper` 2 | 3 | Returns a wrapper of the first element that matches the selector by traversing up through the 4 | wrapped node's ancestors in the tree, starting with itself. It must be a single-node wrapper. 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | #### Returns 11 | 12 | `ReactWrapper`: A new wrapper that wraps the resulting node. 13 | 14 | 15 | 16 | #### Examples 17 | 18 | ```jsx 19 | const wrapper = mount(); 20 | expect(wrapper.find(Foo).closest('.bar')).to.have.lengthOf(1); 21 | ``` 22 | 23 | #### Related Methods 24 | 25 | - [`.children([selector]) => ReactWrapper`](children.md) 26 | - [`.parent() => ReactWrapper`](parent.md) 27 | - [`.parents([selector]) => ReactWrapper`](parents.md) 28 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/describeMethods.js: -------------------------------------------------------------------------------- 1 | export default function describeMethods({ 2 | Wrap, 3 | Wrapper, 4 | }, ...methods) { 5 | const WrapperName = Wrapper.name; 6 | const isShallow = WrapperName === 'ShallowWrapper'; 7 | const isMount = WrapperName === 'ReactWrapper'; 8 | const hasDOM = isMount; 9 | const makeDOMElement = () => (hasDOM ? global.document.createElement('div') : { nodeType: 1 }); 10 | 11 | methods.forEach((method) => { 12 | // eslint-disable-next-line global-require, import/no-dynamic-require 13 | require(`../shared/methods/${method}`)({ 14 | Wrap, 15 | WrapRendered: isShallow ? Wrap : (...args) => Wrap(...args).children(), 16 | Wrapper, 17 | WrapperName, 18 | isShallow, 19 | isMount, 20 | hasDOM, 21 | makeDOMElement, 22 | }); 23 | }); 24 | } 25 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/closest.md: -------------------------------------------------------------------------------- 1 | # `.closest(selector) => ShallowWrapper` 2 | 3 | Returns a wrapper of the first element that matches the selector by traversing up through the 4 | wrapped node's ancestors in the tree, starting with itself. It must be a single-node wrapper. 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | #### Returns 11 | 12 | `ShallowWrapper`: A new wrapper that wraps the resulting node. 13 | 14 | 15 | 16 | #### Examples 17 | 18 | ```jsx 19 | const wrapper = shallow(); 20 | expect(wrapper.find(Foo).closest('.bar')).to.have.lengthOf(1); 21 | ``` 22 | 23 | #### Related Methods 24 | 25 | - [`.children([selector]) => ShallowWrapper`](children.md) 26 | - [`.parent() => ShallowWrapper`](parent.md) 27 | - [`.parents([selector]) => ShallowWrapper`](parents.md) 28 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/lifecycles/componentWillUnmount.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import sinon from 'sinon-sandbox'; 3 | import { expect } from 'chai'; 4 | 5 | export default function describeCWU({ 6 | Wrap, 7 | }) { 8 | describe('componentWillUnmount', () => { 9 | it('calls componentWillUnmount', () => { 10 | const spy = sinon.spy(); 11 | class Foo extends React.Component { 12 | componentWillUnmount() { 13 | spy('componentWillUnmount'); 14 | } 15 | 16 | render() { 17 | spy('render'); 18 | return
foo
; 19 | } 20 | } 21 | const wrapper = Wrap(); 22 | wrapper.unmount(); 23 | expect(spy.args).to.deep.equal([ 24 | ['render'], 25 | ['componentWillUnmount'], 26 | ]); 27 | }); 28 | }); 29 | } 30 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/text.md: -------------------------------------------------------------------------------- 1 | # `.text() => String` 2 | 3 | Returns a string of the rendered text of the current render tree. This function should be 4 | looked at with skepticism if being used to test what the actual HTML output of the component 5 | will be. If that is what you would like to test, use enzyme's `render` function instead. 6 | 7 | Note: can only be called on a wrapper of a single node. 8 | 9 | 10 | #### Returns 11 | 12 | `String`: The resulting string 13 | 14 | 15 | #### Examples 16 | 17 | ```jsx 18 | const wrapper = shallow(
important
); 19 | expect(wrapper.text()).to.equal('important'); 20 | ``` 21 | 22 | ```jsx 23 | const wrapper = shallow(
important
); 24 | expect(wrapper.text()).to.equal('important'); 25 | ``` 26 | 27 | 28 | #### Related Methods 29 | 30 | [`.html() => String`](html.md) 31 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/key.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeKey({ 5 | Wrap, 6 | }) { 7 | describe('.key()', () => { 8 | it('returns the key of the node', () => { 9 | const wrapper = Wrap(( 10 |
    11 | {['foo', 'bar', ''].map((s) =>
  • {s}
  • )} 12 |
13 | )).find('li'); 14 | expect(wrapper.at(0).key()).to.equal('foo'); 15 | expect(wrapper.at(1).key()).to.equal('bar'); 16 | expect(wrapper.at(2).key()).to.equal(''); 17 | }); 18 | 19 | it('returns null when no key is specified', () => { 20 | const wrapper = Wrap(( 21 |
    22 |
  • foo
  • 23 |
24 | )).find('li'); 25 | expect(wrapper.key()).to.equal(null); 26 | }); 27 | }); 28 | } 29 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/describeLifecycles.js: -------------------------------------------------------------------------------- 1 | export default function describeLifecycles({ 2 | Wrap, 3 | Wrapper, 4 | }, ...lifecycles) { 5 | const WrapperName = Wrapper.name; 6 | const isShallow = WrapperName === 'ShallowWrapper'; 7 | const isMount = WrapperName === 'ReactWrapper'; 8 | const hasDOM = isMount; 9 | const makeDOMElement = () => (hasDOM ? global.document.createElement('div') : { nodeType: 1 }); 10 | 11 | lifecycles.forEach((lifecycle) => { 12 | // eslint-disable-next-line global-require, import/no-dynamic-require 13 | require(`../shared/lifecycles/${lifecycle}`)({ 14 | Wrap, 15 | WrapRendered: isShallow ? Wrap : (...args) => Wrap(...args).children(), 16 | Wrapper, 17 | WrapperName, 18 | isShallow, 19 | isMount, 20 | hasDOM, 21 | makeDOMElement, 22 | }); 23 | }); 24 | } 25 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/children.md: -------------------------------------------------------------------------------- 1 | # `.children([selector]) => ReactWrapper` 2 | 3 | Returns a new wrapper with all of the children of the node(s) in the current wrapper. Optionally, a 4 | selector can be provided and it will filter the children by this selector 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): A selector to filter the children by. 10 | 11 | 12 | #### Returns 13 | 14 | `ReactWrapper`: A new wrapper that wraps the resulting nodes. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = mount(); 22 | expect(wrapper.find('ul').children()).to.have.lengthOf(items.length); 23 | ``` 24 | 25 | #### Related Methods 26 | 27 | - [`.parents([selector]) => ReactWrapper`](parents.md) 28 | - [`.parent() => ReactWrapper`](parent.md) 29 | - [`.closest(selector) => ReactWrapper`](closest.md) 30 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/children.md: -------------------------------------------------------------------------------- 1 | # `.children([selector]) => ShallowWrapper` 2 | 3 | Returns a new wrapper with all of the children of the node(s) in the current wrapper. Optionally, a 4 | selector can be provided and it will filter the children by this selector 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): A selector to filter the children by. 10 | 11 | 12 | #### Returns 13 | 14 | `ShallowWrapper`: A new wrapper that wraps the resulting nodes. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = shallow(); 22 | expect(wrapper.find('ul').children()).to.have.lengthOf(items.length); 23 | ``` 24 | 25 | #### Related Methods 26 | 27 | - [`.parents([selector]) => ShallowWrapper`](parents.md) 28 | - [`.parent() => ShallowWrapper`](parent.md) 29 | - [`.closest(selector) => ShallowWrapper`](closest.md) 30 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/describeHooks.js: -------------------------------------------------------------------------------- 1 | import { is } from './version'; 2 | 3 | export default function describeHooks({ 4 | Wrap, 5 | Wrapper, 6 | }, ...hooks) { 7 | const WrapperName = Wrapper.name; 8 | const isShallow = WrapperName === 'ShallowWrapper'; 9 | const isMount = WrapperName === 'ReactWrapper'; 10 | const hasDOM = isMount; 11 | const makeDOMElement = () => (hasDOM ? global.document.createElement('div') : { nodeType: 1 }); 12 | 13 | hooks.forEach((hook) => { 14 | // eslint-disable-next-line global-require, import/no-dynamic-require 15 | require(`../shared/hooks/${hook}`)({ 16 | hasHooks: is('>= 16.8'), 17 | Wrap, 18 | WrapRendered: isShallow ? Wrap : (...args) => Wrap(...args).children(), 19 | Wrapper, 20 | WrapperName, 21 | isShallow, 22 | isMount, 23 | hasDOM, 24 | makeDOMElement, 25 | }); 26 | }); 27 | } 28 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/parents.md: -------------------------------------------------------------------------------- 1 | # `.parents([selector]) => ReactWrapper` 2 | 3 | Returns a wrapper around all of the parents/ancestors of the single node in the wrapper. Does not include the node itself. 4 | Optionally, a selector can be provided and it will filter the parents by this selector. It must be a single-node wrapper. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): The selector to filter the parents by. 10 | 11 | 12 | #### Returns 13 | 14 | `ReactWrapper`: A new wrapper that wraps the resulting nodes. 15 | 16 | 17 | #### Examples 18 | 19 | ```jsx 20 | const wrapper = mount(); 21 | expect(wrapper.find('ul').parents()).to.have.lengthOf(2); 22 | ``` 23 | 24 | #### Related Methods 25 | 26 | - [`.children([selector]) => ReactWrapper`](children.md) 27 | - [`.parent() => ReactWrapper`](parent.md) 28 | - [`.closest(selector) => ReactWrapper`](closest.md) 29 | -------------------------------------------------------------------------------- /packages/enzyme-shallow-equal/src/index.js: -------------------------------------------------------------------------------- 1 | import is from 'object-is'; 2 | import has from 'has'; 3 | 4 | // adapted from https://github.com/facebook/react/blob/144328fe81719e916b946e22660479e31561bb0b/packages/shared/shallowEqual.js#L36-L68 5 | export default function shallowEqual(objA, objB) { 6 | if (is(objA, objB)) { 7 | return true; 8 | } 9 | 10 | if (!objA || !objB || typeof objA !== 'object' || typeof objB !== 'object') { 11 | return false; 12 | } 13 | 14 | const keysA = Object.keys(objA); 15 | const keysB = Object.keys(objB); 16 | 17 | if (keysA.length !== keysB.length) { 18 | return false; 19 | } 20 | 21 | keysA.sort(); 22 | keysB.sort(); 23 | 24 | // Test for A's keys different from B. 25 | for (let i = 0; i < keysA.length; i += 1) { 26 | if (!has(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { 27 | return false; 28 | } 29 | } 30 | 31 | return true; 32 | } 33 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/text.md: -------------------------------------------------------------------------------- 1 | # `.text() => String` 2 | 3 | Returns a string of the rendered text of the current render tree. This function should be 4 | looked at with skepticism if being used to test what the actual HTML output of the component 5 | will be. If that is what you would like to test, use enzyme's `render` function instead. 6 | 7 | Note: can only be called on a wrapper of a single node. 8 | 9 | 10 | #### Returns 11 | 12 | `String`: The resulting string 13 | 14 | 15 | #### Examples 16 | 17 | ```jsx 18 | const wrapper = mount(
important
); 19 | expect(wrapper.text()).to.equal('important'); 20 | ``` 21 | 22 | ```jsx 23 | function Foo() { 24 | return
This is
; 25 | } 26 | 27 | const wrapper = mount(
really important
); 28 | expect(wrapper.text()).to.equal('This is really important'); 29 | ``` 30 | 31 | 32 | #### Related Methods 33 | 34 | [`.html() => String`](html.md) 35 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/parents.md: -------------------------------------------------------------------------------- 1 | # `.parents([selector]) => ShallowWrapper` 2 | 3 | Returns a wrapper around all of the parents/ancestors of the single node in the wrapper. Does not include the node itself. 4 | Optionally, a selector can be provided and it will filter the parents by this selector. It must be a single-node wrapper. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `selector` ([`EnzymeSelector`](../selector.md) [optional]): The selector to filter the parents by. 10 | 11 | 12 | #### Returns 13 | 14 | `ShallowWrapper`: A new wrapper that wraps the resulting nodes. 15 | 16 | 17 | #### Examples 18 | 19 | ```jsx 20 | const wrapper = shallow(); 21 | expect(wrapper.find('ul').parents()).to.have.lengthOf(2); 22 | ``` 23 | 24 | #### Related Methods 25 | 26 | - [`.children([selector]) => ShallowWrapper`](children.md) 27 | - [`.parent() => ShallowWrapper`](parent.md) 28 | - [`.closest(selector) => ShallowWrapper`](closest.md) 29 | -------------------------------------------------------------------------------- /packages/enzyme/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "airbnb", 3 | "parser": "@babel/eslint-parser", 4 | "root": true, 5 | "ignorePatterns": ["build/"], 6 | "env": { 7 | "node": true, 8 | "mocha": true 9 | }, 10 | "rules": { 11 | "id-length": 0, 12 | "max-classes-per-file": 0, 13 | "new-cap": [2, { "capIsNewExceptions": ["AND"] }], 14 | "react/jsx-pascal-case": [2, { "allowAllCaps": true }], 15 | "react/no-find-dom-node": 1, 16 | "import/first": 0, 17 | "no-underscore-dangle": [2, { 18 | "allowAfterThis": true, 19 | "allow": [ 20 | "_context", 21 | "_currentElement", 22 | "_instance", 23 | "_reactInternalComponent", 24 | "_reactInternalInstance", 25 | "_renderedChildren", 26 | "_renderedComponent", 27 | "_renderedNodeType", 28 | "_state", 29 | "_store", 30 | "_stringText", 31 | ], 32 | }], 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/getElements.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | import { itIf } from '../../_helpers'; 4 | import { is } from '../../_helpers/version'; 5 | 6 | export default function describeGetElements({ 7 | Wrap, 8 | }) { 9 | describe('.getElements()', () => { 10 | // FIXME: figure out why this fails on 15.0, 15.1, 15.2 and 15.3 11 | itIf(!is('~15.0 || ~15.1 || ~15.2 || ~15.3'), 'returns the wrapped elements', () => { 12 | const one = ; 13 | const two = ; 14 | 15 | class Test extends React.Component { 16 | render() { 17 | return ( 18 |
19 | {one} 20 | {two} 21 |
22 | ); 23 | } 24 | } 25 | 26 | const wrapper = Wrap(); 27 | expect(wrapper.find('span').getElements()).to.deep.equal([one, two]); 28 | }); 29 | }); 30 | } 31 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/name.md: -------------------------------------------------------------------------------- 1 | # `.name() => String|null` 2 | 3 | Returns the name of the current node of this wrapper. If it's a composite component, this will be 4 | the name of the component. If it's a native DOM node, it will be a string of the tag name. If it's 5 | `null`, it will be `null`. 6 | 7 | The order of precedence on returning the name is: `type.displayName` -> `type.name` -> `type`. 8 | 9 | Note: can only be called on a wrapper of a single node. 10 | 11 | 12 | #### Returns 13 | 14 | `String|null`: The name of the current node 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = mount(
); 22 | expect(wrapper.name()).to.equal('div'); 23 | ``` 24 | 25 | ```jsx 26 | const wrapper = mount(); 27 | expect(wrapper.name()).to.equal('Foo'); 28 | ``` 29 | 30 | ```jsx 31 | Foo.displayName = 'A cool custom name'; 32 | const wrapper = mount(); 33 | expect(wrapper.name()).to.equal('A cool custom name'); 34 | ``` 35 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/invoke.md: -------------------------------------------------------------------------------- 1 | # `.invoke(invokePropName)(...args) => Any` 2 | 3 | Invokes a function prop. 4 | 5 | #### Arguments 6 | 7 | 1. `propName` (`String`): The function prop that is invoked 8 | 2. `...args` (`Any` [optional]): Arguments that is passed to the prop function 9 | 10 | This essentially calls wrapper.prop(propName)(...args). 11 | 12 | #### Returns 13 | 14 | `Any`: Returns the value from the prop function 15 | 16 | #### Example 17 | 18 | ```jsx 19 | class Foo extends React.Component { 20 | loadData() { 21 | return fetch(); 22 | } 23 | 24 | render() { 25 | return ( 26 |
27 | 33 |
34 | ); 35 | } 36 | } 37 | const wrapper = shallow(); 38 | wrapper.find('button').invoke('onClick')().then(() => { 39 | // expect() 40 | }); 41 | ``` 42 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/every.md: -------------------------------------------------------------------------------- 1 | # `.every(selector) => Boolean` 2 | 3 | Returns whether or not all of the nodes in the wrapper match the provided selector. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Boolean`: True if every node in the current wrapper matched the provided selector. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = mount(( 22 |
23 |
24 |
25 |
26 |
27 | )); 28 | expect(wrapper.find('.foo').every('.foo')).to.equal(true); 29 | expect(wrapper.find('.foo').every('.qoo')).to.equal(false); 30 | expect(wrapper.find('.foo').every('.bar')).to.equal(false); 31 | ``` 32 | 33 | #### Related Methods 34 | 35 | - [`.someWhere(predicate) => Boolean`](someWhere.md) 36 | - [`.everyWhere(predicate) => Boolean`](everyWhere.md) 37 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/every.md: -------------------------------------------------------------------------------- 1 | # `.every(selector) => Boolean` 2 | 3 | Returns whether or not all of the nodes in the wrapper match the provided selector. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Boolean`: True if every node in the current wrapper matched the provided selector. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = shallow(( 22 |
23 |
24 |
25 |
26 |
27 | )); 28 | expect(wrapper.find('.foo').every('.foo')).to.equal(true); 29 | expect(wrapper.find('.foo').every('.qoo')).to.equal(false); 30 | expect(wrapper.find('.foo').every('.bar')).to.equal(false); 31 | ``` 32 | 33 | #### Related Methods 34 | 35 | - [`.someWhere(predicate) => Boolean`](someWhere.md) 36 | - [`.everyWhere(predicate) => Boolean`](everyWhere.md) 37 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/unmount.md: -------------------------------------------------------------------------------- 1 | # `.unmount() => Self` 2 | 3 | A method that unmounts the component. This can be used to simulate a component going through 4 | an unmount/mount lifecycle. 5 | 6 | #### Returns 7 | 8 | `ShallowWrapper`: Returns itself. 9 | 10 | 11 | 12 | #### Example 13 | 14 | ```jsx 15 | import PropTypes from 'prop-types'; 16 | import sinon from 'sinon'; 17 | 18 | const spy = sinon.spy(); 19 | 20 | class Foo extends React.Component { 21 | constructor(props) { 22 | super(props); 23 | this.componentWillUnmount = spy; 24 | } 25 | 26 | render() { 27 | const { id } = this.props; 28 | return ( 29 |
30 | {id} 31 |
32 | ); 33 | } 34 | } 35 | Foo.propTypes = { 36 | id: PropTypes.string.isRequired, 37 | }; 38 | const wrapper = shallow(); 39 | expect(spy).to.have.property('callCount', 0); 40 | wrapper.unmount(); 41 | expect(spy).to.have.property('callCount', 1); 42 | ``` 43 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/hooks/useRef.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | import { 5 | describeIf, 6 | } from '../../_helpers'; 7 | 8 | import { 9 | useRef, 10 | } from '../../_helpers/react-compat'; 11 | 12 | export default function describeUseRef({ 13 | hasHooks, 14 | Wrap, 15 | }) { 16 | describeIf(hasHooks, 'hooks: useRef', () => { 17 | function ComponentUsingRef() { 18 | const id = useRef(Math.floor(100 * Math.random())); 19 | return ( 20 |
{id.current}
21 | ); 22 | } 23 | 24 | it('`current` should be the same between two renders', () => { 25 | const wrapper = Wrap(); 26 | 27 | const childBefore = wrapper.find('div').prop('children'); 28 | wrapper.setProps({ foo: 'bar' }); 29 | const childAfter = wrapper.find('div').prop('children'); 30 | 31 | expect(childBefore).to.equal(childAfter); 32 | }); 33 | }); 34 | } 35 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/hooks/_hook.template: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { expect } from 'chai'; 4 | import wrap from 'mocha-wrap'; 5 | import sinon from 'sinon-sandbox'; 6 | import { Portal } from 'react-is'; 7 | 8 | import { render } from 'enzyme'; 9 | import getAdapter from 'enzyme/build/getAdapter'; 10 | 11 | import { 12 | describeIf, 13 | itIf, 14 | } from '../../_helpers'; 15 | import { 16 | is, 17 | REACT16, 18 | } from '../../_helpers/version'; 19 | 20 | import { 21 | useCallback, 22 | useContext, 23 | useEffect, 24 | useLayoutEffect, 25 | useMemo, 26 | useReducer, 27 | useState, 28 | act, 29 | } from '../../_helpers/react-compat'; 30 | 31 | export default function describe$Hook({ 32 | hasHooks, 33 | Wrap, 34 | WrapRendered, 35 | Wrapper, 36 | WrapperName, 37 | isShallow, 38 | isMount, 39 | makeDOMElement, 40 | }) { 41 | describeIf(hasHooks, 'hooks: $Hook', () => { 42 | }); 43 | } 44 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/forEach.md: -------------------------------------------------------------------------------- 1 | # `.forEach(fn) => Self` 2 | 3 | Iterates through each node of the current wrapper and executes the provided function with a 4 | wrapper around the corresponding node passed in as the first argument. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `fn` (`Function ( ReactWrapper node, Number index )`): A callback to be run for every node in the collection. 10 | Should expect a ReactWrapper as the first argument, and will be run with a context of the original 11 | instance. 12 | 13 | 14 | 15 | #### Returns 16 | 17 | `ReactWrapper`: Returns itself. 18 | 19 | 20 | 21 | #### Example 22 | 23 | ```jsx 24 | const wrapper = mount(( 25 |
26 |
27 |
28 |
29 |
30 | )); 31 | 32 | wrapper.find('.foo').forEach((node) => { 33 | expect(node.hasClass('foo')).to.equal(true); 34 | }); 35 | ``` 36 | 37 | 38 | #### Related Methods 39 | 40 | - [`.map(fn) => ReactWrapper`](map.md) 41 | -------------------------------------------------------------------------------- /docs/future.md: -------------------------------------------------------------------------------- 1 | # Future Work 2 | 3 | Discussion of additional features and support for enzyme should be initiated by opening a 4 | [GitHub issue](https://github.com/enzymejs/enzyme/issues). 5 | 6 | There are several things we'd like to address with enzyme that often get asked. Here are a couple 7 | of projects that we plan on addressing in the near future: 8 | 9 | 10 | #### Improved event simulation and propagation support 11 | 12 | Event simulation is limited for Shallow rendering. Event propagation is not supported, and one must 13 | supply their own event objects. We would like to provide tools to more fully simulate real 14 | interaction. 15 | 16 | 17 | ### Improved Keyboard + Mouse Simulation 18 | 19 | Many react components involve simulating form input or complex mouse interaction. Simulating this 20 | using the event simulation API that enzyme provides is cumbersome and impractical. We are looking for 21 | an expressive way to solve this problem, even if it is a library that lives outside of enzyme. 22 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/forEach.md: -------------------------------------------------------------------------------- 1 | # `.forEach(fn) => Self` 2 | 3 | Iterates through each node of the current wrapper and executes the provided function with a 4 | wrapper around the corresponding node passed in as the first argument. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `fn` (`Function ( ShallowWrapper node, Number index )`): A callback to be run for every node in the collection. 10 | Should expect a ShallowWrapper as the first argument, and will be run with a context of the original 11 | instance. 12 | 13 | 14 | 15 | #### Returns 16 | 17 | `ShallowWrapper`: Returns itself. 18 | 19 | 20 | 21 | #### Example 22 | 23 | ```jsx 24 | const wrapper = shallow(( 25 |
26 |
27 |
28 |
29 |
30 | )); 31 | 32 | wrapper.find('.foo').forEach((node) => { 33 | expect(node.hasClass('foo')).to.equal(true); 34 | }); 35 | ``` 36 | 37 | 38 | #### Related Methods 39 | 40 | - [`.map(fn) => ShallowWrapper`](map.md) 41 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/hasClass.md: -------------------------------------------------------------------------------- 1 | # `.hasClass(className) => Boolean` 2 | 3 | Returns whether or not the wrapped node has a `className` prop including the passed in class name. It must be a single-node wrapper. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `className` (`String` | `RegExp`): A single class name or a regex expression. 9 | 10 | 11 | #### Returns 12 | 13 | `Boolean`: whether or not the wrapped node has the class. 14 | 15 | 16 | #### Example 17 | 18 | 19 | ```jsx 20 | const wrapper = shallow(); 21 | expect(wrapper.find('.my-button').hasClass('disabled')).to.equal(true); 22 | ``` 23 | 24 | ```jsx 25 | // Searching using RegExp works fine when classes were injected by a jss decorator 26 | const wrapper = shallow(); 27 | expect(wrapper.find('.my-button').hasClass(/(ComponentName)-(other)-(\d+)/)).to.equal(true); 28 | ``` 29 | 30 | ### Common Gotchas 31 | 32 | - `.hasClass()` expects a class name, NOT a CSS selector. `.hasClass('.foo')` should be 33 | `.hasClass('foo')` 34 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/hasClass.md: -------------------------------------------------------------------------------- 1 | # `.hasClass(className) => Boolean` 2 | 3 | Returns whether or not the wrapped node has a `className` prop including the passed in class name. It must be a single-node wrapper. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `className` (`String` | `RegExp`): A single class name or a regex expression. 9 | 10 | 11 | #### Returns 12 | 13 | `Boolean`: whether or not the wrapped node has found the class name. 14 | 15 | 16 | #### Examples 17 | 18 | 19 | ```jsx 20 | const wrapper = mount(); 21 | expect(wrapper.find('.my-button').hasClass('disabled')).to.equal(true); 22 | ``` 23 | 24 | ```jsx 25 | // Searching using RegExp works fine when classes were injected by a jss decorator 26 | const wrapper = mount(); 27 | expect(wrapper.find('.my-button').hasClass(/(ComponentName)-(other)-(\d+)/)).to.equal(true); 28 | ``` 29 | 30 | ### Common Gotchas 31 | 32 | - `.hasClass()` expects a class name, NOT a CSS selector. `.hasClass('.foo')` should be 33 | `.hasClass('foo')` 34 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/some.md: -------------------------------------------------------------------------------- 1 | # `.some(selector) => Boolean` 2 | 3 | Returns whether or not any of the nodes in the wrapper match the provided selector. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Boolean`: True if at least one of the nodes in the current wrapper matched the provided selector. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = mount(( 22 |
23 |
24 |
25 |
26 |
27 | )); 28 | expect(wrapper.find('.foo').some('.qoo')).to.equal(true); 29 | expect(wrapper.find('.foo').some('.foo')).to.equal(true); 30 | expect(wrapper.find('.foo').some('.bar')).to.equal(false); 31 | ``` 32 | 33 | #### Related Methods 34 | 35 | - [`.someWhere(predicate) => Boolean`](someWhere.md) 36 | - [`.every(selector) => Boolean`](every.md) 37 | - [`.everyWhere(predicate) => Boolean`](everyWhere.md) 38 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/some.md: -------------------------------------------------------------------------------- 1 | # `.some(selector) => Boolean` 2 | 3 | Returns whether or not any of the nodes in the wrapper match the provided selector. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `selector` ([`EnzymeSelector`](../selector.md)): The selector to match. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Boolean`: True if at least one of the nodes in the current wrapper matched the provided selector. 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = shallow(( 22 |
23 |
24 |
25 |
26 |
27 | )); 28 | expect(wrapper.find('.foo').some('.qoo')).to.equal(true); 29 | expect(wrapper.find('.foo').some('.foo')).to.equal(true); 30 | expect(wrapper.find('.foo').some('.bar')).to.equal(false); 31 | ``` 32 | 33 | #### Related Methods 34 | 35 | - [`.someWhere(predicate) => Boolean`](someWhere.md) 36 | - [`.every(selector) => Boolean`](every.md) 37 | - [`.everyWhere(predicate) => Boolean`](everyWhere.md) 38 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/html.md: -------------------------------------------------------------------------------- 1 | # `.html() => String` 2 | 3 | Returns a string of the rendered HTML markup of the current render tree. See also [`.debug()`](debug.md) 4 | 5 | Note: can only be called on a wrapper of a single node. 6 | 7 | 8 | #### Returns 9 | 10 | `String`: The resulting HTML string 11 | 12 | 13 | 14 | #### Examples 15 | 16 | ```jsx 17 | function Foo() { 18 | return (
); 19 | } 20 | ``` 21 | 22 | ```jsx 23 | function Bar() { 24 | return ( 25 |
26 | 27 |
28 | ); 29 | } 30 | ``` 31 | 32 | ```jsx 33 | const wrapper = mount(); 34 | expect(wrapper.html()).to.equal('
'); 35 | expect(wrapper.find(Foo).html()).to.equal('
'); 36 | ``` 37 | 38 | ```jsx 39 | const wrapper = mount(
important
); 40 | expect(wrapper.html()).to.equal('
important
'); 41 | ``` 42 | 43 | 44 | #### Related Methods 45 | 46 | [`.text() => String`](text.md) 47 | -------------------------------------------------------------------------------- /packages/enzyme/src/render.js: -------------------------------------------------------------------------------- 1 | import getAdapter from './getAdapter'; 2 | import { loadCheerioRoot } from './Utils'; 3 | 4 | /** 5 | * Renders a react component into static HTML and provides a cheerio wrapper around it. This is 6 | * somewhat asymmetric with `mount` and `shallow`, which don't use any external libraries, but 7 | * Cheerio's API is pretty close to what we actually want and has a significant amount of utility 8 | * that would be recreating the wheel if we didn't use it. 9 | * 10 | * I think there are a lot of good use cases to use `render` instead of `shallow` or `mount`, and 11 | * thus I'd like to keep this API in here even though it's not really "ours". 12 | * 13 | * @param node 14 | * @param options 15 | * @returns {Cheerio} 16 | */ 17 | 18 | export default function render(node, options = {}) { 19 | const adapter = getAdapter(options); 20 | const renderer = adapter.createRenderer({ mode: 'string', ...options }); 21 | const html = renderer.render(node, options.context); 22 | return loadCheerioRoot(html); 23 | } 24 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/everyWhere.md: -------------------------------------------------------------------------------- 1 | # `.everyWhere(fn) => Boolean` 2 | 3 | Returns whether or not all of the nodes in the wrapper pass the provided predicate function. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `predicate` (`ReactWrapper => Boolean`): A predicate function to match the nodes. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Boolean`: True if every node in the current wrapper passed the predicate function. 15 | 16 | 17 | 18 | #### Example 19 | 20 | ```jsx 21 | const wrapper = mount(( 22 |
23 |
24 |
25 |
26 |
27 | )); 28 | expect(wrapper.find('.foo').everyWhere((n) => n.hasClass('foo'))).to.equal(true); 29 | expect(wrapper.find('.foo').everyWhere((n) => n.hasClass('qoo'))).to.equal(false); 30 | expect(wrapper.find('.foo').everyWhere((n) => n.hasClass('bar'))).to.equal(false); 31 | ``` 32 | 33 | 34 | #### Related Methods 35 | 36 | - [`.some(selector) => Boolean`](some.md) 37 | - [`.every(selector) => Boolean`](every.md) 38 | 39 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/invoke.md: -------------------------------------------------------------------------------- 1 | # `.invoke(propName)(...args) => Any` 2 | 3 | Invokes a function prop. 4 | Note that in React 16.8+, `.invoke` will wrap your handler with [`ReactTestUtils.act`](https://reactjs.org/docs/test-utils.html#act) and call `.update()` automatically. 5 | 6 | #### Arguments 7 | 8 | 1. `propName` (`String`): The function prop that is invoked 9 | 2. `...args` (`Any` [optional]): Arguments that is passed to the prop function 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `Any`: Returns the value from the prop function 16 | 17 | #### Example 18 | 19 | ```jsx 20 | class Foo extends React.Component { 21 | loadData() { 22 | return fetch(); 23 | } 24 | 25 | render() { 26 | return ( 27 |
28 | 34 |
35 | ); 36 | } 37 | } 38 | const wrapper = mount(); 39 | wrapper.find('button').invoke('onClick')().then(() => { 40 | // expect() 41 | }); 42 | ``` 43 | -------------------------------------------------------------------------------- /docs/guides/lab.md: -------------------------------------------------------------------------------- 1 | # Using enzyme with Lab and Code 2 | 3 | [Lab](https://github.com/hapijs/lab) is a simple test utility for node & part of the [Hapi.js](https://github.com/hapijs/hapi) framework universe. Lab's initial code borrowed heavily from [Mocha](https://github.com/mochajs/mocha). [Code](https://github.com/hapijs/code) is Lab's standard assertion library and was created as a direct rewrite of [Chai](https://github.com/chaijs). 4 | 5 | 6 | # Example Test: enzyme + Lab + Code 7 | 8 | ```jsx 9 | import { shallow, mount, render } from 'enzyme'; 10 | import React from 'react'; 11 | 12 | const Code = require('code'); 13 | const Lab = require('lab'); 14 | 15 | const lab = Lab.script(); 16 | export { lab }; 17 | 18 | lab.suite('A suite', () => { 19 | lab.test('calls componentDidMount', (done) => { 20 | const wrapper = mount(); 21 | Code.expect(Foo.prototype.componentDidMount.callCount).to.equal(1); 22 | done(); 23 | }); 24 | }); 25 | ``` 26 | 27 | 28 | ## Example Projects 29 | 30 | - [enzyme-example-lab](https://github.com/gattermeier/enzyme-example-lab) 31 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/some.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeSome({ 5 | Wrap, 6 | WrapperName, 7 | }) { 8 | describe('.some(selector)', () => { 9 | it('returns if a node matches a selector', () => { 10 | const wrapper = Wrap(( 11 |
12 |
13 |
14 |
15 |
16 | )); 17 | const foo = wrapper.find('.foo'); 18 | expect(foo.some('.qoo')).to.equal(true); 19 | expect(foo.some('.foo')).to.equal(true); 20 | expect(foo.some('.bar')).to.equal(false); 21 | }); 22 | 23 | it('throws if called on root', () => { 24 | const wrapper = Wrap(( 25 |
26 |
27 |
28 | )); 29 | expect(() => wrapper.some('.foo')).to.throw( 30 | Error, 31 | `${WrapperName}::some() can not be called on the root`, 32 | ); 33 | }); 34 | }); 35 | } 36 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/_method.template: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import PropTypes from 'prop-types'; 3 | import { expect } from 'chai'; 4 | import wrap from 'mocha-wrap'; 5 | import sinon from 'sinon-sandbox'; 6 | import { Portal } from 'react-is'; 7 | 8 | import { render } from 'enzyme'; 9 | import getAdapter from 'enzyme/build/getAdapter'; 10 | import { 11 | ITERATOR_SYMBOL, 12 | sym, 13 | } from 'enzyme/build/Utils'; 14 | 15 | import { 16 | describeIf, 17 | itIf, 18 | } from '../../_helpers'; 19 | import realArrowFunction from '../../_helpers/realArrowFunction'; 20 | import { getElementPropSelector, getWrapperPropSelector } from '../../_helpers/selectors'; 21 | import { 22 | is, 23 | REACT16, 24 | } from '../../_helpers/version'; 25 | 26 | import { 27 | createClass, 28 | createPortal, 29 | createRef, 30 | Fragment, 31 | } from '../../_helpers/react-compat'; 32 | 33 | export default function describe$Method({ 34 | Wrap, 35 | WrapRendered, 36 | Wrapper, 37 | WrapperName, 38 | isShallow, 39 | isMount, 40 | makeDOMElement, 41 | }) { 42 | } 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | .nyc_output 16 | 17 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (https://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 28 | node_modules 29 | 30 | # Jetbrains IDEs 31 | .idea 32 | 33 | /build 34 | _book 35 | 36 | # Only apps should have lockfiles 37 | npm-shrinkwrap.json 38 | package-lock.json 39 | yarn.lock 40 | 41 | .DS_Store 42 | ._.DS_Store 43 | 44 | packages/*/build/ 45 | 46 | # Temporarily copied 47 | packages/*/LICENSE.md 48 | packages/enzyme/README.md 49 | packages/enzyme-adapter-react-*/README.md 50 | packages/enzyme-adapter-utils*/README.md 51 | 52 | .npmignore 53 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/everyWhere.md: -------------------------------------------------------------------------------- 1 | # `.everyWhere(fn) => Boolean` 2 | 3 | Returns whether or not all of the nodes in the wrapper pass the provided predicate function. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `predicate` (`ShallowWrapper => Boolean`): A predicate function to match the nodes. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Boolean`: True if every node in the current wrapper passed the predicate function. 15 | 16 | 17 | 18 | #### Example 19 | 20 | ```jsx 21 | const wrapper = shallow(( 22 |
23 |
24 |
25 |
26 |
27 | )); 28 | expect(wrapper.find('.foo').everyWhere((n) => n.hasClass('foo'))).to.equal(true); 29 | expect(wrapper.find('.foo').everyWhere((n) => n.hasClass('qoo'))).to.equal(false); 30 | expect(wrapper.find('.foo').everyWhere((n) => n.hasClass('bar'))).to.equal(false); 31 | ``` 32 | 33 | 34 | #### Related Methods 35 | 36 | - [`.some(selector) => Boolean`](some.md) 37 | - [`.every(selector) => Boolean`](every.md) 38 | - [`.everyWhere(predicate) => Boolean`](everyWhere.md) 39 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-utils/src/wrapWithSimpleWrapper.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { intersects } from 'semver'; 3 | import { or, explicitNull } from 'airbnb-prop-types'; 4 | import PropTypes from 'prop-types'; 5 | 6 | const propTypes = { 7 | children: or([explicitNull().isRequired, PropTypes.node.isRequired]), 8 | }; 9 | 10 | const defaultProps = { 11 | children: undefined, 12 | }; 13 | 14 | const Wrapper = (intersects('>= 0.14', React.version) 15 | // eslint-disable-next-line prefer-arrow-callback 16 | ? () => Object.assign(function SimpleSFCWrapper({ children }) { 17 | return children; 18 | }, { propTypes, defaultProps }) 19 | : () => { 20 | class SimpleClassWrapper extends React.Component { 21 | render() { 22 | const { children } = this.props; 23 | return children; 24 | } 25 | } 26 | SimpleClassWrapper.propTypes = propTypes; 27 | SimpleClassWrapper.defaultProps = defaultProps; 28 | return SimpleClassWrapper; 29 | } 30 | )(); 31 | 32 | export default function wrap(element) { 33 | return {element}; 34 | } 35 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/someWhere.md: -------------------------------------------------------------------------------- 1 | # `.someWhere(fn) => Boolean` 2 | 3 | Returns whether or not any of the nodes in the wrapper pass the provided predicate function. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `predicate` (`ReactWrapper => Boolean`): A predicate function to match the nodes. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Boolean`: True if at least one of the nodes in the current wrapper passed the predicate function. 15 | 16 | 17 | 18 | #### Example 19 | 20 | ```jsx 21 | const wrapper = mount(( 22 |
23 |
24 |
25 |
26 |
27 | )); 28 | expect(wrapper.find('.foo').someWhere((n) => n.hasClass('qoo'))).to.equal(true); 29 | expect(wrapper.find('.foo').someWhere((n) => n.hasClass('foo'))).to.equal(true); 30 | expect(wrapper.find('.foo').someWhere((n) => n.hasClass('bar'))).to.equal(false); 31 | ``` 32 | 33 | 34 | #### Related Methods 35 | 36 | - [`.some(selector) => Boolean`](some.md) 37 | - [`.every(selector) => Boolean`](every.md) 38 | - [`.everyWhere(predicate) => Boolean`](everyWhere.md) 39 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/someWhere.md: -------------------------------------------------------------------------------- 1 | # `.someWhere(fn) => Boolean` 2 | 3 | Returns whether or not any of the nodes in the wrapper pass the provided predicate function. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `predicate` (`ShallowWrapper => Boolean`): A predicate function to match the nodes. 9 | 10 | 11 | 12 | #### Returns 13 | 14 | `Boolean`: True if at least one of the nodes in the current wrapper passed the predicate function. 15 | 16 | 17 | 18 | #### Example 19 | 20 | ```jsx 21 | const wrapper = shallow(( 22 |
23 |
24 |
25 |
26 |
27 | )); 28 | expect(wrapper.find('.foo').someWhere((n) => n.hasClass('qoo'))).to.equal(true); 29 | expect(wrapper.find('.foo').someWhere((n) => n.hasClass('foo'))).to.equal(true); 30 | expect(wrapper.find('.foo').someWhere((n) => n.hasClass('bar'))).to.equal(false); 31 | ``` 32 | 33 | 34 | #### Related Methods 35 | 36 | - [`.some(selector) => Boolean`](some.md) 37 | - [`.every(selector) => Boolean`](every.md) 38 | - [`.everyWhere(predicate) => Boolean`](everyWhere.md) 39 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/name.md: -------------------------------------------------------------------------------- 1 | # `.name() => String|null` 2 | 3 | Returns the name of the current node of this wrapper. If it's a composite component, this will be 4 | the name of the top-most rendered component. If it's a native DOM node, it will be a string of the 5 | tag name. If it's `null`, it will be `null`. 6 | 7 | The order of precedence on returning the name is: `type.displayName` -> `type.name` -> `type`. 8 | 9 | Note: can only be called on a wrapper of a single node. 10 | 11 | 12 | #### Returns 13 | 14 | `String|null`: The name of the current node 15 | 16 | 17 | 18 | #### Examples 19 | 20 | ```jsx 21 | const wrapper = shallow(
); 22 | expect(wrapper.name()).to.equal('div'); 23 | ``` 24 | 25 | ```jsx 26 | function SomeWrappingComponent() { 27 | return ; 28 | } 29 | const wrapper = shallow(); 30 | expect(wrapper.name()).to.equal('Foo'); 31 | ``` 32 | 33 | ```jsx 34 | Foo.displayName = 'A cool custom name'; 35 | function SomeWrappingComponent() { 36 | return ; 37 | } 38 | const wrapper = shallow(); 39 | expect(wrapper.name()).to.equal('A cool custom name'); 40 | ``` 41 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Airbnb, Inc. and contributors 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/equals.md: -------------------------------------------------------------------------------- 1 | # `.equals(node) => Boolean` 2 | 3 | Returns whether or not the current wrapper root node render tree looks like the one passed in. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `node` (`ReactElement`): The node whose presence you are detecting in the current instance's 9 | render tree. 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `Boolean`: whether or not the current wrapper has a node anywhere in it's render tree that looks 16 | like the one passed in. 17 | 18 | 19 | 20 | #### Example 21 | 22 | 23 | ```jsx 24 | function MyComponent() { 25 | return
; 26 | } 27 | 28 | const wrapper = shallow(); 29 | expect(wrapper.equals(
)).to.equal(true); 30 | ``` 31 | 32 | 33 | #### Common Gotchas 34 | 35 | - `.equals()` expects a ReactElement, not a selector (like many other methods). Make sure that 36 | when you are calling it you are calling it with a ReactElement or a JSX expression. 37 | - Keep in mind that this method determines equality based on the equality of the node's children as 38 | well. 39 | - Following React's behavior, `.equals()` ignores properties whose values are `undefined`. 40 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/_helpers/getLoadedLazyComponent.js: -------------------------------------------------------------------------------- 1 | import { fakeDynamicImport } from 'enzyme-adapter-utils'; 2 | import { lazy } from './react-compat'; 3 | import { is, VERSION } from './version'; 4 | 5 | function fakeSyncThenable(result) { 6 | return { 7 | then(resolve) { 8 | return resolve({ default: result }); 9 | }, 10 | }; 11 | } 12 | 13 | export default function getLoadedLazyComponent(wrappedComponent) { 14 | if (is('>= 16.8')) { 15 | return lazy(() => fakeSyncThenable(wrappedComponent)); 16 | } 17 | if (is('>= 16.6')) { 18 | const LazyComponent = lazy(() => fakeDynamicImport(wrappedComponent)); 19 | /** 20 | * Before React v16.8 there's no public api to synchronously / await 21 | * loaded lazy component. 22 | * So we have to hack this by setting `_result` and `_status` implementation. 23 | */ 24 | /* eslint-disable no-underscore-dangle */ 25 | LazyComponent._result = wrappedComponent; 26 | /* eslint-disable no-underscore-dangle */ 27 | LazyComponent._status = 1; 28 | return LazyComponent; 29 | } 30 | throw Error(`Current React version ${VERSION} doesn't support \`lazy()\` api.`); 31 | } 32 | -------------------------------------------------------------------------------- /since.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const fs = require('fs'); 4 | const path = require('path'); 5 | const { spawnSync } = require('child_process'); 6 | const glob = require('glob-gitignore'); 7 | 8 | const packagesDir = path.join(__dirname, 'packages'); 9 | const docsDir = path.join(__dirname, 'docs'); 10 | 11 | const packages = (process.argv.length > 2 ? [process.argv[2]] : glob.sync('*', { cwd: packagesDir })) 12 | .map((name) => JSON.parse(fs.readFileSync(path.join(packagesDir, name, 'package.json')))) 13 | .filter((x) => !x.private && x.name !== 'enzyme-example-mocha'); 14 | 15 | packages.forEach((pkg) => { 16 | const tag = `${pkg.name === 'docs' ? 'enzyme' : pkg.name}@${pkg.version}`; 17 | const dir = path.join(packagesDir, pkg.name); 18 | const logArgs = ['--no-pager', 'log', '--oneline', '--grep=\\[\\(dev \\)\\?deps\\]', '--invert-grep', `${tag}..HEAD`, dir, ':!**/.eslintrc'].concat(pkg.name === 'enzyme' ? docsDir : []); 19 | const log = spawnSync('git', logArgs, { stdio: 'pipe' }); 20 | if (log.stdout.length > 0 || log.stderr.length > 0) { 21 | console.log(tag); 22 | spawnSync('git', logArgs, { stdio: 'inherit' }); 23 | console.log('\n'); 24 | } 25 | }); 26 | -------------------------------------------------------------------------------- /packages/enzyme-example-mocha/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Leland Richardson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/equals.md: -------------------------------------------------------------------------------- 1 | # `.equals(node) => Boolean` 2 | 3 | Returns whether or not the current wrapper root node render tree looks like the one passed in. 4 | 5 | 6 | #### Arguments 7 | 8 | 1. `node` (`ReactElement`): The node whose presence you are detecting in the current instance's 9 | render tree. 10 | 11 | 12 | 13 | #### Returns 14 | 15 | `Boolean`: whether or not the current wrapper has a node anywhere in it's render tree that looks 16 | like the one passed in. 17 | 18 | 19 | 20 | #### Example 21 | 22 | 23 | ```jsx 24 | function MyComponent() { 25 | return
; 26 | } 27 | 28 | const wrapper = mount().childAt(0); 29 | expect(wrapper.equals(
)).to.equal(true); 30 | ``` 31 | 32 | 33 | #### Common Gotchas 34 | 35 | - `.equals()` expects a ReactElement, not a selector (like many other methods). Make sure that 36 | when you are calling it you are calling it with a ReactElement or a JSX expression. 37 | - Keep in mind that this method determines equality based on the equality of the node's children as 38 | well. 39 | - Following React's behavior, `.equals()` ignores properties whose values are `undefined`. 40 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/type.md: -------------------------------------------------------------------------------- 1 | # `.type() => String | Function | null` 2 | 3 | Returns the type of the only node of this wrapper. 4 | If it's a React component, this will be the component constructor. 5 | If it's a native DOM node, it will be a string with the tag name. 6 | If it's `null`, it will be `null`. It must be a single-node wrapper. 7 | 8 | 9 | #### Returns 10 | 11 | `String | Function | null`: The type of the node 12 | 13 | 14 | #### Examples 15 | 16 | ```jsx 17 | function Foo() { 18 | return
; 19 | } 20 | const wrapper = shallow(); 21 | expect(wrapper.type()).to.equal('div'); 22 | ``` 23 | 24 | ```jsx 25 | function Foo() { 26 | return ( 27 |
28 | 29 |
30 | ); 31 | } 32 | const wrapper = shallow(); 33 | expect(wrapper.find('.btn').type()).to.equal('button'); 34 | ``` 35 | 36 | ```jsx 37 | function Foo() { 38 | return ; 39 | } 40 | const wrapper = shallow(); 41 | expect(wrapper.type()).to.equal(Bar); 42 | ``` 43 | 44 | ```jsx 45 | function Null() { 46 | return null; 47 | } 48 | const wrapper = shallow(); 49 | expect(wrapper.type()).to.equal(null); 50 | ``` 51 | -------------------------------------------------------------------------------- /packages/enzyme/withDom.js: -------------------------------------------------------------------------------- 1 | require('raf/polyfill'); 2 | 3 | /* eslint 4 | no-console: 0, 5 | prefer-template: 0, 6 | prefer-destructuring: 0, 7 | */ 8 | 9 | if (!global.document) { 10 | try { 11 | // eslint-disable-next-line global-require, import/no-extraneous-dependencies 12 | const jsdom = require('jsdom').jsdom; // could throw 13 | 14 | global.document = jsdom(''); 15 | global.window = global.document.defaultView; 16 | Object.keys(global.document.defaultView).forEach((property) => { 17 | if (typeof global[property] === 'undefined') { 18 | global[property] = global.document.defaultView[property]; 19 | } 20 | }); 21 | 22 | global.navigator = { 23 | userAgent: 'node.js', 24 | }; 25 | } catch (e) { 26 | // jsdom is not supported... 27 | if (e.message === "Cannot find module 'jsdom'") { 28 | console.error('[enzyme/withDom] Error: missing required module "jsdom"'); 29 | console.error('[enzyme/withDom] To fix this you must run:'); 30 | console.error('[enzyme/withDom] npm install jsdom --save-dev'); 31 | } else { 32 | console.error('[enzyme withDom] ' + (e.stack || e.message)); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/ref.md: -------------------------------------------------------------------------------- 1 | # `.ref(refName) => ReactComponent | HTMLElement` 2 | 3 | Returns the node that matches the provided reference name. 4 | 5 | 6 | NOTE: can only be called on a wrapper instance that is also the root instance. 7 | 8 | #### Arguments 9 | 10 | 1. `refName` (`String`): The ref attribute of the node 11 | 12 | 13 | #### Returns 14 | 15 | `ReactComponent | HTMLElement`: The node that matches the provided reference name. This can be a react component instance, or an HTML element instance. 16 | 17 | 18 | #### Examples 19 | 20 | 21 | ```jsx 22 | class Foo extends React.Component { 23 | render() { 24 | return ( 25 |
26 | First 27 | Second 28 | Third 29 |
30 | ); 31 | } 32 | } 33 | ``` 34 | 35 | ```jsx 36 | const wrapper = mount(); 37 | expect(wrapper.ref('secondRef').innerText).to.equal('Second'); 38 | ``` 39 | 40 | 41 | #### Related Methods 42 | 43 | - [`.find(selector) => ReactWrapper`](find.md) 44 | - [`.findWhere(predicate) => ReactWrapper`](findWhere.md) 45 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/html.md: -------------------------------------------------------------------------------- 1 | # `.html() => String` 2 | 3 | Returns a string of the rendered HTML markup of the entire current render tree (not just the shallow-rendered part). It uses [static rendering](../render.md) internally. To see only the shallow-rendered part use [`.debug()`](debug.md). 4 | 5 | Note: can only be called on a wrapper of a single node. 6 | 7 | 8 | #### Returns 9 | 10 | `String`: The resulting HTML string 11 | 12 | 13 | #### Examples 14 | 15 | ```jsx 16 | function Foo() { 17 | return (
); 18 | } 19 | ``` 20 | 21 | ```jsx 22 | function Bar() { 23 | return ( 24 |
25 | 26 |
27 | ); 28 | } 29 | ``` 30 | 31 | ```jsx 32 | const wrapper = shallow(); 33 | expect(wrapper.html()).to.equal('
'); 34 | expect(wrapper.find(Foo).html()).to.equal('
'); 35 | ``` 36 | 37 | ```jsx 38 | const wrapper = shallow(
important
); 39 | expect(wrapper.html()).to.equal('
important
'); 40 | ``` 41 | 42 | 43 | #### Related Methods 44 | 45 | - [`.text() => String`](text.md) 46 | - [`.debug() => String`](debug.md) 47 | -------------------------------------------------------------------------------- /docs/installation/react-013.md: -------------------------------------------------------------------------------- 1 | # Working with React 0.13 2 | 3 | If you are wanting to use enzyme with React 0.13, but don't already have React 0.13 installed, you 4 | should do so: 5 | 6 | ```bash 7 | npm i react@0.13 --save 8 | ``` 9 | 10 | Next, to get started with enzyme, you can simply install it with npm: 11 | 12 | ```bash 13 | npm i --save-dev enzyme enzyme-adapter-react-13 14 | ``` 15 | 16 | And then you're ready to go! In your test files you can simply `require` or `import` enzyme: 17 | 18 | ES6: 19 | ```js 20 | // setup file 21 | import { configure } from 'enzyme'; 22 | import Adapter from 'enzyme-adapter-react-13'; 23 | 24 | configure({ adapter: new Adapter() }); 25 | ``` 26 | 27 | ```js 28 | // test file 29 | import { shallow, mount, render } from 'enzyme'; 30 | 31 | const wrapper = shallow(); 32 | ``` 33 | 34 | ES5: 35 | 36 | ```js 37 | // setup file 38 | var enzyme = require('enzyme'); 39 | var Adapter = require('enzyme-adapter-react-13'); 40 | 41 | enzyme.configure({ adapter: new Adapter() }); 42 | ``` 43 | 44 | 45 | ```js 46 | // test file 47 | var enzyme = require('enzyme'); 48 | 49 | var wrapper = enzyme.shallow(); 50 | ``` 51 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/map.md: -------------------------------------------------------------------------------- 1 | # `.map(fn) => Array` 2 | 3 | Maps the current array of nodes to another array. Each node is passed in as a `ReactWrapper` 4 | to the map function. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `fn` (`Function ( ReactWrapper node, Number index ) => Any`): A mapping function to be run for every node in 10 | the collection, the results of which will be mapped to the returned array. Should expect a ReactWrapper as the first argument, and will be run with a context of 11 | the original instance. 12 | 13 | 14 | 15 | #### Returns 16 | 17 | `Array`: Returns an array of the returned values from the mapping function.. 18 | 19 | 20 | 21 | #### Example 22 | 23 | ```jsx 24 | const wrapper = mount(( 25 |
26 |
bax
27 |
bar
28 |
baz
29 |
30 | )); 31 | 32 | const texts = wrapper.find('.foo').map((node) => node.text()); 33 | expect(texts).to.eql(['bax', 'bar', 'baz']); 34 | ``` 35 | 36 | 37 | #### Related Methods 38 | 39 | - [`.forEach(fn) => ReactWrapper`](forEach.md) 40 | - [`.reduce(fn[, initialValue]) => Any`](reduce.md) 41 | - [`.reduceRight(fn[, initialValue]) => Any`](reduceRight.md) 42 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/map.md: -------------------------------------------------------------------------------- 1 | # `.map(fn) => Array` 2 | 3 | Maps the current array of nodes to another array. Each node is passed in as a `ShallowWrapper` 4 | to the map function. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `fn` (`Function ( ShallowWrapper node, Number index ) => Any`): A mapping function to be run for every node in 10 | the collection, the results of which will be mapped to the returned array. Should expect a ShallowWrapper as the first argument, and will be run with a context of 11 | the original instance. 12 | 13 | 14 | 15 | #### Returns 16 | 17 | `Array`: Returns an array of the returned values from the mapping function.. 18 | 19 | 20 | 21 | #### Example 22 | 23 | ```jsx 24 | const wrapper = shallow(( 25 |
26 |
bax
27 |
bar
28 |
baz
29 |
30 | )); 31 | 32 | const texts = wrapper.find('.foo').map((node) => node.text()); 33 | expect(texts).to.eql(['bax', 'bar', 'baz']); 34 | ``` 35 | 36 | 37 | #### Related Methods 38 | 39 | - [`.forEach(fn) => ShallowWrapper`](forEach.md) 40 | - [`.reduce(fn[, initialValue]) => Any`](reduce.md) 41 | - [`.reduceRight(fn[, initialValue]) => Any`](reduceRight.md) 42 | -------------------------------------------------------------------------------- /docs/installation/react-16.md: -------------------------------------------------------------------------------- 1 | # Working with React 16 2 | 3 | If you are wanting to use enzyme with React 16, but don't already have React 16 and react-dom 4 | installed, you should do so: 5 | 6 | ```bash 7 | npm i --save react@16 react-dom@16 8 | ``` 9 | 10 | Next, to get started with enzyme, you can simply install it with npm: 11 | 12 | ```bash 13 | npm i --save-dev enzyme enzyme-adapter-react-16 14 | ``` 15 | 16 | And then you're ready to go! In your test files you can simply `require` or `import` enzyme: 17 | 18 | ES6: 19 | ```js 20 | // setup file 21 | import { configure } from 'enzyme'; 22 | import Adapter from 'enzyme-adapter-react-16'; 23 | 24 | configure({ adapter: new Adapter() }); 25 | ``` 26 | 27 | ```js 28 | // test file 29 | import { shallow, mount, render } from 'enzyme'; 30 | 31 | const wrapper = shallow(); 32 | ``` 33 | 34 | ES5: 35 | 36 | ```js 37 | // setup file 38 | var enzyme = require('enzyme'); 39 | var Adapter = require('enzyme-adapter-react-16'); 40 | 41 | enzyme.configure({ adapter: new Adapter() }); 42 | ``` 43 | 44 | 45 | ```js 46 | // test file 47 | var enzyme = require('enzyme'); 48 | 49 | var wrapper = enzyme.shallow(); 50 | ``` 51 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/forEach.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | import sinon from 'sinon-sandbox'; 4 | 5 | export default function describeForEach({ 6 | Wrap, 7 | Wrapper, 8 | }) { 9 | describe('.forEach(fn)', () => { 10 | it('calls a function for each node in the wrapper', () => { 11 | const wrapper = Wrap(( 12 |
13 |
14 |
15 |
16 |
17 | )); 18 | const spy = sinon.spy(); 19 | 20 | wrapper.find('.foo').forEach(spy); 21 | 22 | expect(spy).to.have.property('callCount', 3); 23 | expect(spy.args[0][0]).to.be.instanceOf(Wrapper); 24 | expect(spy.args[0][0].hasClass('bax')).to.equal(true); 25 | expect(spy.args[0][1]).to.equal(0); 26 | expect(spy.args[1][0]).to.be.instanceOf(Wrapper); 27 | expect(spy.args[1][0].hasClass('bar')).to.equal(true); 28 | expect(spy.args[1][1]).to.equal(1); 29 | expect(spy.args[2][0]).to.be.instanceOf(Wrapper); 30 | expect(spy.args[2][0].hasClass('baz')).to.equal(true); 31 | expect(spy.args[2][1]).to.equal(2); 32 | }); 33 | }); 34 | } 35 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/type.md: -------------------------------------------------------------------------------- 1 | # `.type() => String | Function | null` 2 | 3 | Returns the type of the only node of this wrapper. 4 | If it's a React component, this will be the component constructor. 5 | If it's a native DOM node, it will be a string with the tag name. 6 | If it's `null`, it will be `null`. It must be a single-node wrapper. 7 | 8 | 9 | #### Returns 10 | 11 | `String | Function | null`: The type of the node 12 | 13 | 14 | #### Examples 15 | 16 | ```jsx 17 | function Foo() { 18 | return
; 19 | } 20 | const wrapper = mount().childAt(0); 21 | expect(wrapper.type()).to.equal('div'); 22 | ``` 23 | 24 | ```jsx 25 | function Foo() { 26 | return ( 27 |
28 | 29 |
30 | ); 31 | } 32 | const wrapper = mount(); 33 | expect(wrapper.find('.btn').type()).to.equal('button'); 34 | ``` 35 | 36 | ```jsx 37 | function Foo() { 38 | return ; 39 | } 40 | const wrapper = mount(); 41 | expect(wrapper.type()).to.equal(Foo); 42 | expect(wrapper.childAt(0).type()).to.equal(Bar); 43 | ``` 44 | 45 | ```jsx 46 | function Null() { 47 | return null; 48 | } 49 | const wrapper = mount(); 50 | expect(wrapper.type()).to.equal(null); 51 | ``` 52 | -------------------------------------------------------------------------------- /docs/api/ReactWrapper/getWrappingComponent.md: -------------------------------------------------------------------------------- 1 | # `.getWrappingComponent() => ReactWrapper` 2 | 3 | If a `wrappingComponent` was passed in `options`, this methods returns a `ReactWrapper` around the rendered `wrappingComponent`. This `ReactWrapper` can be used to update the `wrappingComponent`'s props, state, etc. 4 | 5 | 6 | #### Returns 7 | 8 | `ReactWrapper`: A `ReactWrapper` around the rendered `wrappingComponent` 9 | 10 | 11 | 12 | #### Examples 13 | 14 | ```jsx 15 | import { Provider } from 'react-redux'; 16 | import { Router } from 'react-router'; 17 | import store from './my/app/store'; 18 | import mockStore from './my/app/mockStore'; 19 | 20 | function MyProvider(props) { 21 | const { children, customStore } = props; 22 | 23 | return ( 24 | 25 | 26 | {children} 27 | 28 | 29 | ); 30 | } 31 | MyProvider.propTypes = { 32 | children: PropTypes.node, 33 | customStore: PropTypes.shape({}), 34 | }; 35 | MyProvider.defaultProps = { 36 | children: null, 37 | customStore: null, 38 | }; 39 | 40 | const wrapper = mount(, { 41 | wrappingComponent: MyProvider, 42 | }); 43 | const provider = wrapper.getWrappingComponent(); 44 | provider.setProps({ customStore: mockStore }); 45 | ``` 46 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/getWrappingComponent.md: -------------------------------------------------------------------------------- 1 | # `.getWrappingComponent() => ShallowWrapper` 2 | 3 | If a `wrappingComponent` was passed in `options`, this methods returns a `ShallowWrapper` around the rendered `wrappingComponent`. This `ShallowWrapper` can be used to update the `wrappingComponent`'s props, state, etc. 4 | 5 | 6 | #### Returns 7 | 8 | `ShallowWrapper`: A `ShallowWrapper` around the rendered `wrappingComponent` 9 | 10 | 11 | 12 | #### Examples 13 | 14 | ```jsx 15 | import { Provider } from 'react-redux'; 16 | import { Router } from 'react-router'; 17 | import store from './my/app/store'; 18 | import mockStore from './my/app/mockStore'; 19 | 20 | function MyProvider(props) { 21 | const { children, customStore } = props; 22 | 23 | return ( 24 | 25 | 26 | {children} 27 | 28 | 29 | ); 30 | } 31 | MyProvider.propTypes = { 32 | children: PropTypes.node, 33 | customStore: PropTypes.shape({}), 34 | }; 35 | MyProvider.defaultProps = { 36 | children: null, 37 | customStore: null, 38 | }; 39 | 40 | const wrapper = shallow(, { 41 | wrappingComponent: MyProvider, 42 | }); 43 | const provider = wrapper.getWrappingComponent(); 44 | provider.setProps({ customStore: mockStore }); 45 | ``` 46 | -------------------------------------------------------------------------------- /docs/api/ShallowWrapper/shallow.md: -------------------------------------------------------------------------------- 1 | # `.shallow([options]) => ShallowWrapper` 2 | 3 | Shallow renders the root node and returns a shallow wrapper around it. 4 | It must be a single-node wrapper. 5 | 6 | 7 | #### Arguments 8 | 9 | 1. `options` (`Object` [optional]): 10 | - `options.context`: (`Object` [optional]): Context to be passed into the component 11 | - `options.disableLifecycleMethods`: (`Boolean` [optional]): If set to true, `componentDidMount` 12 | is not called on the component, and `componentDidUpdate` is not called after 13 | [`setProps`](ShallowWrapper/setProps.md) and [`setContext`](ShallowWrapper/setContext.md). Default to `false`. 14 | 15 | 16 | #### Returns 17 | 18 | `ShallowWrapper`: A new wrapper that wraps the node after it's been shallow rendered. 19 | 20 | 21 | #### Examples 22 | 23 | ```jsx 24 | function Bar() { 25 | return ( 26 |
27 |
28 |
29 | ); 30 | } 31 | ``` 32 | 33 | ```jsx 34 | function Foo() { 35 | return ( 36 |
37 | 38 |
39 | ); 40 | } 41 | ``` 42 | 43 | ```jsx 44 | const wrapper = shallow(); 45 | expect(wrapper.find('.in-bar')).to.have.lengthOf(0); 46 | expect(wrapper.find(Bar)).to.have.lengthOf(1); 47 | expect(wrapper.find(Bar).shallow().find('.in-bar')).to.have.lengthOf(1); 48 | ``` 49 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/isEmpty.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | import sinon from 'sinon-sandbox'; 4 | 5 | export default function describeLast({ 6 | Wrap, 7 | }) { 8 | describe('.isEmpty()', () => { 9 | let warningStub; 10 | let fooNode; 11 | let missingNode; 12 | 13 | beforeEach(() => { 14 | warningStub = sinon.stub(console, 'warn'); 15 | const wrapper = Wrap(
); 16 | fooNode = wrapper.find('.foo'); 17 | missingNode = wrapper.find('.missing'); 18 | }); 19 | afterEach(() => { 20 | warningStub.restore(); 21 | }); 22 | 23 | it('displays a deprecation warning', () => { 24 | fooNode.isEmpty(); 25 | expect(warningStub.calledWith('Enzyme::Deprecated method isEmpty() called, use exists() instead.')).to.equal(true); 26 | }); 27 | 28 | it('calls exists() instead', () => { 29 | const existsSpy = sinon.spy(); 30 | fooNode.exists = existsSpy; 31 | expect(fooNode.isEmpty()).to.equal(true); 32 | expect(existsSpy).to.have.property('called', true); 33 | }); 34 | 35 | it('returns true if wrapper is empty', () => { 36 | expect(fooNode.isEmpty()).to.equal(false); 37 | expect(missingNode.isEmpty()).to.equal(true); 38 | }); 39 | }); 40 | } 41 | -------------------------------------------------------------------------------- /packages/enzyme-test-suite/test/shared/methods/flatMap.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { expect } from 'chai'; 3 | 4 | export default function describeFlatMap({ 5 | Wrap, 6 | }) { 7 | describe('.flatMap(fn)', () => { 8 | it('returns a wrapper with the mapped and flattened nodes', () => { 9 | const wrapper = Wrap(( 10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | )); 25 | 26 | const nodes = wrapper.find('.foo').flatMap((w) => w.children().getElements()); 27 | 28 | expect(nodes).to.have.lengthOf(6); 29 | expect(nodes.at(0).hasClass('bar')).to.equal(true); 30 | expect(nodes.at(1).hasClass('bar')).to.equal(true); 31 | expect(nodes.at(2).hasClass('baz')).to.equal(true); 32 | expect(nodes.at(3).hasClass('baz')).to.equal(true); 33 | expect(nodes.at(4).hasClass('bax')).to.equal(true); 34 | expect(nodes.at(5).hasClass('bax')).to.equal(true); 35 | }); 36 | }); 37 | } 38 | -------------------------------------------------------------------------------- /packages/enzyme-adapter-react-helper/src/safeSFC.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import gOPDs from 'object.getownpropertydescriptors'; 3 | 4 | import ifReact from './ifReact'; 5 | 6 | function assertFunction(fn) { 7 | if (typeof fn !== 'function') { 8 | throw new TypeError('Component must be a function'); 9 | } 10 | return fn; 11 | } 12 | 13 | function copyStatics(source, target) { 14 | assertFunction(source); 15 | 16 | // eslint-disable-next-line no-param-reassign 17 | target.displayName = source.displayName || source.name; 18 | const { 19 | prototype: oldProto, 20 | ...descriptors 21 | } = gOPDs(source); 22 | Object.defineProperties(target, descriptors); 23 | 24 | return target; 25 | } 26 | 27 | function nullToNoScript(fn) { 28 | // eslint-disable-next-line prefer-arrow-callback 29 | return copyStatics(fn, function NullHandler(...args) { 30 | const element = fn(...args); 31 | return element === null ?