-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/update-createOrder
- Loading branch information
Showing
17 changed files
with
630 additions
and
150 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
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
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"; | ||
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!( | ||
{} as CreateOrderData, | ||
{} as CreateOrderActions, | ||
), | ||
).toBe("createOrder"); | ||
expect(current.onClick!({}, {} as OnClickActions)).toBe("onClick"); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["src"], | ||
"exclude": ["src/stories"] | ||
"exclude": ["src/stories", "src/**/*.test.ts", "src/**/*.test.tsx"] | ||
} |
Oops, something went wrong.