Skip to content

Commit

Permalink
Release v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jozsi committed Jul 11, 2019
1 parent cb12331 commit 4379300
Show file tree
Hide file tree
Showing 11 changed files with 2,988 additions and 2,300 deletions.
3 changes: 1 addition & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"presets": ["react-app"],
"plugins": ["transform-es2015-modules-commonjs"]
"presets": ["@babel/env", "@babel/preset-react"]
}
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@ Supports both [Wrapper Components](#wrapper) and [HOCs (Higher Order Components)

```js
// cosmos.proxies.js
import createWrapperProxy from "react-cosmos-wrapper-proxy";
import { MuiThemeProvider, createMuiTheme } from "@material-ui/core/styles";
import purple from '@material-ui/core/colors/purple'
import createWrapperProxy from 'react-cosmos-wrapper-proxy';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import purple from '@material-ui/core/colors/purple';

const muiProxy = createWrapperProxy({
// Required
component: MuiThemeProvider, // The wrapper component
fixtureKey: "mui", // Key
fixtureKey: 'mui', // Key

// Optional
defaultEnabled: true, // enable by default in every fixture
// Props to pass to the wrapper component
// Note: can be passed from the fixture as well
props: {
theme: createMuiTheme({
palette: {
primary: purple
}
primary: purple,
},
}),
someOtherProp: "hello"
}
someOtherProp: 'hello',
},
});

export default [muiProxy];
Expand All @@ -42,7 +43,9 @@ export default [muiProxy];
export default {
component: MyComponent,
// Pass an object of props or `true` to enable the proxy
mui: true
mui: true,
// Or if `defaultEnabled` is set, selectively disable it
mui: false,
};
```

Expand All @@ -52,14 +55,14 @@ export default {

```js
// cosmos.proxies.js
import createWrapperProxy from "react-cosmos-wrapper-proxy";
import { reduxForm } from "redux-form";
import createWrapperProxy from 'react-cosmos-wrapper-proxy';
import { reduxForm } from 'redux-form';

const reduxFormProxy = createWrapperProxy({
// Required
component: reduxForm, // The wrapper function
hoc: true, // Differentiate it from a simple wrapper
fixtureKey: "rf" // Key
fixtureKey: 'rf', // Key
});

export default [reduxFormProxy];
Expand All @@ -73,12 +76,12 @@ export default {
// Then pass an array of arguments
rf: [
{
form: "formName"
}
]
form: 'formName',
},
],
// If the HOC is simply myHoc(Component)
// Then just pass true
// rf: true
rf: true,
};
```

Expand Down
57 changes: 35 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;

var _react = require("react");
var _react = _interopRequireDefault(require("react"));

var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { keys.push.apply(keys, Object.getOwnPropertySymbols(object)); } if (enumerableOnly) keys = keys.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var HOCPropsProxy = function HOCPropsProxy(props) {
var nextProxy = props.nextProxy,
Expand All @@ -23,46 +38,44 @@ var HOCPropsProxy = function HOCPropsProxy(props) {

var NextProxy = nextProxy.value,
next = nextProxy.next;

fixture.props = Object.assign({}, rest, fixture.props);

return _react2.default.createElement(NextProxy, {
fixture.props = _objectSpread({}, rest, {}, fixture.props);
return _react["default"].createElement(NextProxy, {
fixture: fixture,
onComponentRef: onComponentRef,
onFixtureUpdate: onFixtureUpdate,
nextProxy: next()
});
};

exports.default = function (_ref) {
var _default = function _default(_ref) {
var Component = _ref.component,
props = _ref.props,
fixtureKey = _ref.fixtureKey,
hoc = _ref.hoc,
_ref$defaultEnabled = _ref.defaultEnabled,
defaultEnabled = _ref$defaultEnabled === undefined ? false : _ref$defaultEnabled;
defaultEnabled = _ref.defaultEnabled;
return function WrapperProxy(_ref2) {
var nextProxy = _ref2.nextProxy,
nextProps = _objectWithoutProperties(_ref2, ["nextProxy"]);

var fixtureProps = nextProps.fixture[fixtureKey];
var fixtureEnabled = defaultEnabled || !!fixtureProps;
var fixtureEnabled = fixtureProps !== undefined ? fixtureProps : defaultEnabled;

if (fixtureEnabled && hoc) {
var HOComponent = fixtureProps[Symbol.iterator] ? Component.apply(undefined, _toConsumableArray(fixtureProps))(HOCPropsProxy) : Component(HOCPropsProxy);

return _react2.default.createElement(HOComponent, Object.assign({}, nextProps, { nextProxy: nextProxy }));
var HOComponent = fixtureProps[Symbol.iterator] ? Component.apply(void 0, _toConsumableArray(fixtureProps))(HOCPropsProxy) : Component(HOCPropsProxy);
return _react["default"].createElement(HOComponent, _extends({}, nextProps, {
nextProxy: nextProxy
}));
}

var NextProxy = nextProxy.value,
next = nextProxy.next;

var nextProxyEl = _react2.default.createElement(NextProxy, Object.assign({}, nextProps, { nextProxy: next() }));
var nextProxyEl = _react["default"].createElement(NextProxy, _extends({}, nextProps, {
nextProxy: next()
}));

return fixtureEnabled ? _react2.default.createElement(
Component,
Object.assign({}, props, fixtureProps),
nextProxyEl
) : nextProxyEl;
return fixtureEnabled ? _react["default"].createElement(Component, _extends({}, props, fixtureProps), nextProxyEl) : nextProxyEl;
};
};

exports["default"] = _default;
29 changes: 15 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-cosmos-wrapper-proxy",
"version": "1.5.0",
"version": "1.6.0",
"description": "Easily wrap components using react-cosmos",
"main": "lib/index.js",
"author": "Józsi <[email protected]>",
Expand All @@ -12,23 +12,24 @@
"test": "yarn jest"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-jest": "^23.0.1",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-preset-react-app": "^3.1.0",
"babel-runtime": "^6.26.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"jest": "^23.1.0",
"react-cosmos-shared": "^4.4.0"
"@babel/cli": "^7.5.0",
"@babel/core": "^7.5.4",
"@babel/preset-env": "^7.5.4",
"@babel/preset-react": "^7.0.0",
"@skidding/linked-list": "^1.0.2",
"babel-jest": "^24.8.0",
"enzyme": "^3.10.0",
"enzyme-adapter-react-16": "^1.14.0",
"jest": "^24.8.0"
},
"dependencies": {
"react": "^16.4.0",
"react-dom": "^16.4.0"
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"jest": {
"rootDir": "src",
"setupTestFrameworkScriptFile": "<rootDir>/setupTests.js"
"setupFilesAfterEnv": [
"<rootDir>/setupTests.js"
]
}
}
45 changes: 30 additions & 15 deletions src/__tests__/default-enabled.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { mount } from "enzyme";
import createWrapperProxy from "../";
import { createLinkedList } from "react-cosmos-shared";
import React from 'react';
import { mount } from 'enzyme';
import createWrapperProxy from '../';
import createLinkedList from '@skidding/linked-list';

const WrapperComponent = ({ children }) => <div>{children}</div>;
const Component = () => <span>__COMPONENT_MOCK__</span>;
Expand All @@ -17,22 +17,22 @@ const renderProxy = proxyOptions => fixtureOptions => {

const WrapperProxy = createWrapperProxy({
component: WrapperComponent,
...proxyOptions
})
...proxyOptions,
});

return mount(
<WrapperProxy
nextProxy={createLinkedList([ NextProxy, LastProxy ])}
nextProxy={createLinkedList([NextProxy, LastProxy])}
fixture={fixtureOptions}
onComponentRef={jest.fn()}
onFixtureUpdate={jest.fn()}
/>
/>,
);
};

describe("defaultEnabled", () => {
it("Should wrap a component when `defaultEnabled` is true", () => {
const proxyOptions = { fixtureKey: "wrapper", defaultEnabled: true };
describe('defaultEnabled', () => {
it('Should wrap a component when `defaultEnabled` is true', () => {
const proxyOptions = { fixtureKey: 'wrapper', defaultEnabled: true };

const fixtureOptions = { component: Component };

Expand All @@ -41,17 +41,32 @@ describe("defaultEnabled", () => {
expect(wrapper.find(WrapperComponent)).toHaveLength(1);
});

it("Should not wrap a component when `defaultEnabled` is falsey", () => {
it('Should not wrap a component when `defaultEnabled` is falsey', () => {
expect(
renderProxy({ fixtureKey: "wrapper" })({ component: Component }).find(WrapperComponent)
renderProxy({ fixtureKey: 'wrapper' })({ component: Component }).find(
WrapperComponent,
),
).toHaveLength(0);

expect(
renderProxy({ fixtureKey: "wrapper", defaultEnabled: null })({ component: Component }).find(WrapperComponent)
renderProxy({ fixtureKey: 'wrapper', defaultEnabled: null })({
component: Component,
}).find(WrapperComponent),
).toHaveLength(0);

expect(
renderProxy({ fixtureKey: "wrapper", defaultEnabled: undefined })({ component: Component }).find(WrapperComponent)
renderProxy({ fixtureKey: 'wrapper', defaultEnabled: undefined })({
component: Component,
}).find(WrapperComponent),
).toHaveLength(0);
});

it('Should not wrap a component when `defaultEnabled` is enabled but overridden in fixture', () => {
expect(
renderProxy({ fixtureKey: 'wrapper', defaultEnabled: true })({
component: Component,
wrapper: false,
}).find(WrapperComponent),
).toHaveLength(0);
});
});
Loading

0 comments on commit 4379300

Please sign in to comment.