├── .babelrc
├── .gitignore
├── .travis.yml
├── README.md
├── karma.conf.js
├── package.json
├── react-stateless-wrapper.js
├── test
└── react-stateless-wrapper.test.js
└── tests.webpack.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["es2015", "react"]
3 | }
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "5.1"
4 | script:
5 | - npm test
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ### Deprecated, there are better options now, check out [Enzyme](https://github.com/airbnb/enzyme)
2 |
3 | Wraps React stateless components into regular components, for unit testing purposes.
4 |
5 | ```bash
6 | npm install react-stateless-wrapper --save-dev
7 | ```
8 |
9 | Usage:
10 | ```js
11 | import { wrap } from 'react-stateless-wrapper'
12 | let WrappedComponent = wrap(StatelessComponent)
13 | TestUtils.renderIntoDocument()
14 | ```
15 |
--------------------------------------------------------------------------------
/karma.conf.js:
--------------------------------------------------------------------------------
1 | module.exports = function(config) {
2 | config.set({
3 | basePath: __dirname,
4 | frameworks: ['jasmine'],
5 | files: [
6 | 'tests.webpack.js'
7 | ],
8 | preprocessors: {
9 | 'tests.webpack.js': [ 'webpack' ]
10 | },
11 | reporters: ['progress'],
12 | colors: true,
13 | logLevel: config.LOG_INFO,
14 | browsers: ['PhantomJS'],
15 | singleRun: true,
16 | webpack: {
17 | module: {
18 | loaders: [
19 | { test: /\.js$/, loader: 'babel' }
20 | ]
21 | }
22 | },
23 | webpackServer: {
24 | noInfo: true
25 | }
26 | })
27 | }
28 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-stateless-wrapper",
3 | "version": "1.0.9",
4 | "description": "Wraps React stateless components into regular components",
5 | "main": "react-stateless-wrapper.js",
6 | "scripts": {
7 | "test": "./node_modules/.bin/karma start"
8 | },
9 | "author": "Niels Gerritsen",
10 | "repository": {
11 | "type": "git",
12 | "url": "https://github.com/ngerritsen/react-stateless-wrapper"
13 | },
14 | "license": "ISC",
15 | "devDependencies": {
16 | "babel-core": "^6.7.6",
17 | "babel-loader": "^6.2.4",
18 | "babel-preset-es2015": "^6.6.0",
19 | "babel-preset-react": "^6.5.0",
20 | "jasmine-core": "^2.3.4",
21 | "karma": "^0.13.10",
22 | "karma-jasmine": "^0.3.6",
23 | "karma-phantomjs-launcher": "^1.0.0",
24 | "karma-webpack": "^1.7.0",
25 | "phantomjs-prebuilt": "^2.1.7",
26 | "react": "^15.0.1",
27 | "react-addons-test-utils": "^15.0.1",
28 | "react-dom": "^15.0.1",
29 | "webpack": "^1.12.2"
30 | },
31 | "peerDependencies": {
32 | "react": "0.13.x || 0.14.x || ^15.x"
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/react-stateless-wrapper.js:
--------------------------------------------------------------------------------
1 | var React = require('react')
2 |
3 | function wrap(statelessComponent) {
4 | var reactClass = {}
5 |
6 | Object.keys(statelessComponent).forEach(function (key) {
7 | reactClass[key] = statelessComponent[key];
8 | });
9 |
10 | reactClass.displayName = statelessComponent.name || statelessComponent.displayName;
11 | reactClass.render = function() {
12 | return statelessComponent(this.props, this.context);
13 | };
14 |
15 | return React.createClass(reactClass);
16 | }
17 |
18 | module.exports.wrap = wrap;
19 |
--------------------------------------------------------------------------------
/test/react-stateless-wrapper.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import TestUtils from 'react-addons-test-utils'
3 | import { wrap } from '../react-stateless-wrapper'
4 |
5 | describe('react stateless wrapper', () => {
6 | const TestComponent = ({message}) => (
7 |
10 | )
11 |
12 | it('works', () => {
13 | const WrappedTestComponent = wrap(TestComponent)
14 | const test = TestUtils.renderIntoDocument()
15 | const message = TestUtils.findRenderedDOMComponentWithClass(test, 'test-class')
16 | const messages = TestUtils.scryRenderedDOMComponentsWithClass(test, 'test-class')
17 |
18 | expect(message.innerHTML).toEqual('test')
19 | expect(messages.length).toEqual(1)
20 | })
21 |
22 | it('has the stateless component displayName', () => {
23 | const WrappedTestComponent = wrap(TestComponent)
24 | expect(WrappedTestComponent.displayName).toEqual('TestComponent')
25 | })
26 |
27 | it('transfers static properties', () => {
28 | const TestComponentWithStaticProperties = ({message}) => (
29 |
32 | )
33 |
34 | TestComponentWithStaticProperties.contextTypes = { a: React.PropTypes.number }
35 | TestComponentWithStaticProperties.propTypes = { b: React.PropTypes.number }
36 |
37 | const WrappedTestComponent = wrap(TestComponentWithStaticProperties)
38 |
39 | expect(WrappedTestComponent.contextTypes).toEqual({ a: React.PropTypes.number })
40 | expect(WrappedTestComponent.propTypes).toEqual({ b: React.PropTypes.number })
41 | })
42 | })
43 |
--------------------------------------------------------------------------------
/tests.webpack.js:
--------------------------------------------------------------------------------
1 | var context = require.context('./test', true, /.test\.js$/);
2 | context.keys().forEach(context);
3 |
--------------------------------------------------------------------------------