From ef3c87c47af98635ffc8bcd900dbeb2311df204b Mon Sep 17 00:00:00 2001 From: Matt Brennan Date: Fri, 2 Nov 2018 11:18:23 +0000 Subject: [PATCH] test rewrapping --- .../__tests__/x-interaction.test.jsx | 43 +++++++++++++------ components/x-interaction/src/Interaction.jsx | 3 +- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/components/x-interaction/__tests__/x-interaction.test.jsx b/components/x-interaction/__tests__/x-interaction.test.jsx index 5951b0c36..ce8b0136c 100644 --- a/components/x-interaction/__tests__/x-interaction.test.jsx +++ b/components/x-interaction/__tests__/x-interaction.test.jsx @@ -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(); - const target = mount(); + 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(); + + 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; diff --git a/components/x-interaction/src/Interaction.jsx b/components/x-interaction/src/Interaction.jsx index a864d257d..1689210ba 100644 --- a/components/x-interaction/src/Interaction.jsx +++ b/components/x-interaction/src/Interaction.jsx @@ -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) ); }