-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CM-1020]feat:Adding tests for all Commerce Widget Flows (#2358)
- Loading branch information
1 parent
556e4df
commit 31cd0f2
Showing
18 changed files
with
874 additions
and
73 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
75 changes: 75 additions & 0 deletions
75
tests/func-tests/checkout/widgets-nextjs/src/app/commerce-add-tokens/page.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,75 @@ | ||
"use client"; | ||
import { Box } from '@biom3/react'; | ||
import { checkout } from '@imtbl/sdk'; | ||
import { CommerceFlowType, ConnectionSuccess } from '@imtbl/sdk/checkout'; | ||
import { useEffect } from 'react'; | ||
import { useCommerceWidget } from '../../hooks/useCommerceWidget'; | ||
|
||
function CommerceAddTokens() { | ||
|
||
const { widget } = useCommerceWidget(); | ||
|
||
|
||
useEffect(() => { | ||
if (!widget) return; | ||
widget.mount("widget-root", { | ||
flow: CommerceFlowType.ADD_TOKENS, | ||
}); | ||
|
||
widget.addListener( | ||
checkout.CommerceEventType.SUCCESS, | ||
(payload: checkout.CommerceSuccessEvent) => { | ||
const { type, data } = payload; | ||
|
||
// capture transaction hash after user adds tokens | ||
if (type === checkout.CommerceSuccessEventType.ADD_TOKENS_SUCCESS) { | ||
const { transactionHash } = data; | ||
console.log('Add Tokens Success: ', transactionHash); | ||
} | ||
} | ||
); | ||
|
||
// detect when user fails to add tokens | ||
widget.addListener( | ||
checkout.CommerceEventType.FAILURE, | ||
(payload: checkout.CommerceFailureEvent) => { | ||
const { type, data } = payload; | ||
|
||
if (type === checkout.CommerceFailureEventType.ADD_TOKENS_FAILED) { | ||
console.log('failed to add tokens', data.reason); | ||
} | ||
} | ||
); | ||
|
||
// remove widget from view when closed | ||
widget.addListener(checkout.CommerceEventType.CLOSE, () => { | ||
widget.unmount(); | ||
}); | ||
|
||
// clean up event listeners | ||
return () => { | ||
widget.removeListener(checkout.CommerceEventType.SUCCESS); | ||
widget.removeListener(checkout.CommerceEventType.FAILURE); | ||
widget.removeListener(checkout.CommerceEventType.CLOSE); | ||
}; | ||
|
||
|
||
}, [widget]); | ||
|
||
|
||
return ( | ||
<div> | ||
<Box | ||
id="widget-root" | ||
sx={{ | ||
minw: "430px", | ||
minh: "650px", | ||
bg: "base.color.translucent.standard.300", | ||
brad: "base.borderRadius.x5", | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default CommerceAddTokens; |
75 changes: 75 additions & 0 deletions
75
tests/func-tests/checkout/widgets-nextjs/src/app/commerce-bridge/page.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,75 @@ | ||
"use client"; | ||
import { Box } from '@biom3/react'; | ||
import { checkout } from '@imtbl/sdk'; | ||
import { CommerceFlowType } from '@imtbl/sdk/checkout'; | ||
import { useEffect } from 'react'; | ||
import { useCommerceWidget } from '../../hooks/useCommerceWidget'; | ||
|
||
|
||
function CommerceBridge() { | ||
|
||
const { widget } = useCommerceWidget(); | ||
|
||
|
||
useEffect(() => { | ||
if (!widget) return; | ||
widget.mount("widget-root", { | ||
flow: CommerceFlowType.BRIDGE, | ||
}); | ||
|
||
widget.addListener( | ||
checkout.CommerceEventType.SUCCESS, | ||
(payload: checkout.CommerceSuccessEvent) => { | ||
const { type, data } = payload; | ||
|
||
// capture provider after user executes bridge | ||
if (type === checkout.CommerceSuccessEventType.BRIDGE_SUCCESS) { | ||
console.log('bridge success', data.transactionHash); | ||
} | ||
} | ||
); | ||
|
||
// detect when user fails to execute bridge | ||
widget.addListener( | ||
checkout.CommerceEventType.FAILURE, | ||
(payload: checkout.CommerceFailureEvent) => { | ||
const { type, data } = payload; | ||
|
||
if (type === checkout.CommerceFailureEventType.BRIDGE_FAILED) { | ||
console.log('failed to execute bridge', data.reason); | ||
} | ||
} | ||
); | ||
|
||
// remove widget from view when closed | ||
widget.addListener(checkout.CommerceEventType.CLOSE, () => { | ||
widget.unmount(); | ||
}); | ||
|
||
// clean up event listeners | ||
return () => { | ||
widget.removeListener(checkout.CommerceEventType.SUCCESS); | ||
widget.removeListener(checkout.CommerceEventType.FAILURE); | ||
widget.removeListener(checkout.CommerceEventType.CLOSE); | ||
}; | ||
|
||
|
||
}, [widget]); | ||
|
||
|
||
return ( | ||
<div> | ||
<Box | ||
id="widget-root" | ||
sx={{ | ||
minw: "430px", | ||
minh: "650px", | ||
bg: "base.color.translucent.standard.300", | ||
brad: "base.borderRadius.x5", | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default CommerceBridge; |
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
79 changes: 79 additions & 0 deletions
79
tests/func-tests/checkout/widgets-nextjs/src/app/commerce-onramp/page.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,79 @@ | ||
"use client"; | ||
import { Box } from '@biom3/react'; | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { checkout } from '@imtbl/sdk'; | ||
import { CommerceFlowType } from '@imtbl/sdk/checkout'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { useCommerceWidget } from '../../hooks/useCommerceWidget'; | ||
import { MockProvider } from '../utils/mockProvider'; | ||
|
||
function CommerceOnRamp() { | ||
const { widget, factory } = useCommerceWidget(); | ||
const provider = useMemo(() => new Web3Provider(new MockProvider()), []); | ||
|
||
useEffect(() => { | ||
if (!widget || !factory) return; | ||
|
||
factory.updateProvider(provider); | ||
|
||
widget.mount("widget-root", { | ||
flow: CommerceFlowType.ONRAMP, | ||
}); | ||
|
||
widget.addListener( | ||
checkout.CommerceEventType.SUCCESS, | ||
(payload: checkout.CommerceSuccessEvent) => { | ||
const { type, data } = payload; | ||
|
||
// capture provider after user onramp | ||
if (type === checkout.CommerceSuccessEventType.ONRAMP_SUCCESS) { | ||
const { transactionHash } = data; | ||
console.log('onramp success', transactionHash); | ||
} | ||
} | ||
); | ||
|
||
// detect when user fails to onramp | ||
widget.addListener( | ||
checkout.CommerceEventType.FAILURE, | ||
(payload: checkout.CommerceFailureEvent) => { | ||
const { type, data } = payload; | ||
|
||
if (type === checkout.CommerceFailureEventType.ONRAMP_FAILED) { | ||
console.log('failed to onramp', data.reason); | ||
} | ||
} | ||
); | ||
|
||
// remove widget from view when closed | ||
widget.addListener(checkout.CommerceEventType.CLOSE, () => { | ||
widget.unmount(); | ||
}); | ||
|
||
// clean up event listeners | ||
return () => { | ||
widget.removeListener(checkout.CommerceEventType.SUCCESS); | ||
widget.removeListener(checkout.CommerceEventType.FAILURE); | ||
widget.removeListener(checkout.CommerceEventType.CLOSE); | ||
}; | ||
|
||
|
||
}, [widget, factory, provider]); | ||
|
||
|
||
return ( | ||
<div> | ||
<Box | ||
id="widget-root" | ||
sx={{ | ||
minw: "430px", | ||
minh: "650px", | ||
bg: "base.color.translucent.standard.300", | ||
brad: "base.borderRadius.x5", | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default CommerceOnRamp; |
79 changes: 79 additions & 0 deletions
79
tests/func-tests/checkout/widgets-nextjs/src/app/commerce-sale/page.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,79 @@ | ||
"use client"; | ||
import { Box } from '@biom3/react'; | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { checkout } from '@imtbl/sdk'; | ||
import { CommerceFlowType } from '@imtbl/sdk/checkout'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { useCommerceWidget } from '../../hooks/useCommerceWidget'; | ||
import { MockProvider } from '../utils/mockProvider'; | ||
|
||
function CommerceSale() { | ||
const { widget, factory } = useCommerceWidget(); | ||
const provider = useMemo(() => new Web3Provider(new MockProvider()), []); | ||
|
||
useEffect(() => { | ||
if (!widget || !factory) return; | ||
|
||
factory.updateProvider(provider); | ||
|
||
widget.mount("widget-root", { | ||
flow: CommerceFlowType.SALE, | ||
}); | ||
|
||
widget.addListener( | ||
checkout.CommerceEventType.SUCCESS, | ||
(payload: checkout.CommerceSuccessEvent) => { | ||
const { type, data } = payload; | ||
|
||
// capture provider after user completes sale | ||
if (type === checkout.CommerceSuccessEventType.SALE_SUCCESS) { | ||
const { transactionHash } = data; | ||
console.log('sale success', transactionHash); | ||
} | ||
} | ||
); | ||
|
||
// detect when user fails to complete sale | ||
widget.addListener( | ||
checkout.CommerceEventType.FAILURE, | ||
(payload: checkout.CommerceFailureEvent) => { | ||
const { type, data } = payload; | ||
|
||
if (type === checkout.CommerceFailureEventType.SALE_FAILED) { | ||
console.log('failed to sale', data.reason); | ||
} | ||
} | ||
); | ||
|
||
// remove widget from view when closed | ||
widget.addListener(checkout.CommerceEventType.CLOSE, () => { | ||
widget.unmount(); | ||
}); | ||
|
||
// clean up event listeners | ||
return () => { | ||
widget.removeListener(checkout.CommerceEventType.SUCCESS); | ||
widget.removeListener(checkout.CommerceEventType.FAILURE); | ||
widget.removeListener(checkout.CommerceEventType.CLOSE); | ||
}; | ||
|
||
|
||
}, [widget, factory, provider]); | ||
|
||
|
||
return ( | ||
<div> | ||
<Box | ||
id="widget-root" | ||
sx={{ | ||
minw: "430px", | ||
minh: "650px", | ||
bg: "base.color.translucent.standard.300", | ||
brad: "base.borderRadius.x5", | ||
}} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
export default CommerceSale; |
Oops, something went wrong.