Skip to content

Commit

Permalink
test rewrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
apaleslimghost committed Nov 2, 2018
1 parent 619af26 commit ef3c87c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
43 changes: 31 additions & 12 deletions components/x-interaction/__tests__/x-interaction.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,42 @@ describe('x-interaction', () => {
).toBe(15);
});

it('should pass actions to actionsRef on mount and null on unmount', async () => {
const actionsRef = jest.fn();
describe('actionsRef', () => {
it('should pass actions to actionsRef on mount and null on unmount', async () => {
const actionsRef = jest.fn();

const Base = () => null;
const Wrapped = withActions({
foo() {},
})(Base);
const Base = () => null;
const Wrapped = withActions({
foo() {},
})(Base);

const target = mount(<Wrapped actionsRef={actionsRef} />);

const target = mount(<Wrapped actionsRef={actionsRef} />);
expect(actionsRef).toHaveBeenCalled();
expect(actionsRef.mock.calls[0][0]).toHaveProperty('foo');

expect(actionsRef).toHaveBeenCalled();
expect(actionsRef.mock.calls[0][0]).toHaveProperty('foo');
target.unmount();

target.unmount();
expect(actionsRef).toHaveBeenLastCalledWith(null);
});

expect(actionsRef).toHaveBeenLastCalledWith(null);
});
it('should pass all actions for rewrapped components', async () => {
const actionsRef = jest.fn();

const Base = () => null;
const Wrapped = withActions({
bar() {},
})(withActions({
foo() {},
})(Base));

const target = mount(<Wrapped actionsRef={actionsRef} />);

expect(actionsRef).toHaveBeenCalled();
expect(actionsRef.mock.calls[0][0]).toHaveProperty('foo');
expect(actionsRef.mock.calls[0][0]).toHaveProperty('bar');
});
});

it(`shouldn't reset props when others change`, async () => {
const Base = () => null;
Expand Down
3 changes: 2 additions & 1 deletion components/x-interaction/src/Interaction.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export const withActions = (getActions) => (Component) => {
// to wrap it further. so, discard its wrapper and rewrap the original
// component with the new actions on top
if(Component._wraps) {
const wrappedGetActions = Component._wraps.getActions;
Component = Component._wraps.Component;
getActions = initialState => Object.assign(
invoke(Component._wraps.getActions, initialState),
invoke(wrappedGetActions, initialState),
invoke(_wraps.getActions, initialState)
);
}
Expand Down

0 comments on commit ef3c87c

Please sign in to comment.