-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix) proxy function props to prevent stale state in callbacks (#604)
* proxy props to prevent callback stale closures * fix: rollup config for tsx tests * split tsconfig * update @playwright/test
- Loading branch information
Showing
14 changed files
with
614 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@paypal/react-paypal-js": minor | ||
--- | ||
|
||
(fix) proxy props to prevent stale closure |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "@testing-library/jest-dom"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
CreateOrderActions, | ||
CreateOrderData, | ||
OnClickActions, | ||
} from "@paypal/paypal-js"; | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
Check warning on line 6 in packages/react-paypal-js/src/hooks/useProxyProps.test.ts
|
||
import { useProxyProps } from "./useProxyProps"; | ||
|
||
describe("useProxyProps", () => { | ||
test("should return an object of wrapped callbacks", () => { | ||
const createOrder = jest.fn().mockReturnValue("createOrder"); | ||
const onClick = jest.fn().mockReturnValue("onClick"); | ||
|
||
const props = { | ||
createOrder, | ||
onClick, | ||
}; | ||
|
||
const { | ||
result: { current }, | ||
} = renderHook(() => useProxyProps(props)); | ||
|
||
expect(current).toHaveProperty("createOrder"); | ||
expect(current).toHaveProperty("onClick"); | ||
expect(current.createOrder).not.toBe(props.createOrder); | ||
expect(current.onClick).not.toBe(props.onClick); | ||
|
||
expect( | ||
current.createOrder!( | ||
Check warning on line 29 in packages/react-paypal-js/src/hooks/useProxyProps.test.ts
|
||
{} as CreateOrderData, | ||
{} as CreateOrderActions, | ||
), | ||
).toBe("createOrder"); | ||
expect(current.onClick!({}, {} as OnClickActions)).toBe("onClick"); | ||
Check warning on line 34 in packages/react-paypal-js/src/hooks/useProxyProps.test.ts
|
||
|
||
expect(props.createOrder).toHaveBeenCalled(); | ||
expect(props.onClick).toHaveBeenCalled(); | ||
|
||
// ensure no props mutation | ||
expect(props.createOrder).toBe(createOrder); | ||
expect(props.onClick).toBe(onClick); | ||
}); | ||
|
||
test("should not wrap or mutate non-function props", () => { | ||
const fundingSource = ["paypal"]; | ||
const props = { | ||
fundingSource, | ||
}; | ||
|
||
const { | ||
result: { current }, | ||
} = renderHook(() => useProxyProps(props)); | ||
|
||
expect(current.fundingSource).toBe(props.fundingSource); | ||
expect(props.fundingSource).toBe(fundingSource); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useRef } from "react"; | ||
|
||
export function useProxyProps<T extends Record<PropertyKey, unknown>>( | ||
props: T, | ||
): T { | ||
const proxyRef = useRef( | ||
new Proxy<T>({} as T, { | ||
get(target: T, prop: PropertyKey, receiver) { | ||
/** | ||
* | ||
* If target[prop] is a function, return a function that accesses | ||
* this function off the target object. We can mutate the target with | ||
* new copies of this function without having to re-render the | ||
* SDK components to pass new callbacks. | ||
* | ||
* */ | ||
if (typeof target[prop] === "function") { | ||
return (...args: unknown[]) => | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
(target[prop] as Function)(...args); | ||
} | ||
|
||
return Reflect.get(target, prop, receiver); | ||
}, | ||
}), | ||
); | ||
|
||
proxyRef.current = Object.assign(proxyRef.current, props); | ||
|
||
return proxyRef.current; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
// these are overridden by the Rollup plugin | ||
"outDir": "./dist", | ||
"rootDir": "./src", | ||
"include": ["src/**/*.ts", "src/**/*.tsx"], | ||
"exclude": ["src/**/*.test.ts", "src/**/*.test.tsx"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src/**/*.test.ts", "src/**/*.test.tsx", "jest.setup.ts"] | ||
} |