Skip to content

Commit e693840

Browse files
authored
Merge pull request #2154 from oasisprotocol/lw/less-embed-transak
Fallback to transak button if embedded doesn't initialize after 5sec
2 parents aa522fa + 79f7a3f commit e693840

4 files changed

Lines changed: 29 additions & 26 deletions

File tree

.changelog/2154.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fallback to transak button if embedded doesn't initialize after 5sec

playwright/tests/extension.spec.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { test } from '../utils/extensionTestExtend'
22
import { expect } from '@playwright/test'
33
import { warnSlowApi } from '../utils/warnSlowApi'
44
import { mockApi } from '../utils/mockApi'
5-
import { fillPrivateKeyAndPassword } from '../utils/fillPrivateKey'
6-
import { privateKey, privateKeyAddress } from '../../src/utils/__fixtures__/test-inputs'
75

86
test.beforeEach(async ({ context }) => {
97
await warnSlowApi(context)
@@ -48,7 +46,6 @@ test.describe('The extension popup should load', () => {
4846
*/
4947
test('Transak can not be embedded in extension', async ({ page, extensionPopupURL }) => {
5048
test.fail()
51-
await page.setViewportSize({ width: 1280, height: 720 })
5249

5350
/* TODO: reenable when transak throws only a few errors
5451
await expectNoErrorsInConsole(page, {
@@ -61,28 +58,14 @@ test.describe('The extension popup should load', () => {
6158
},
6259
})
6360
*/
64-
await page.goto(`${extensionPopupURL}/open-wallet/private-key`)
65-
await fillPrivateKeyAndPassword(page, {
66-
privateKey: privateKey,
67-
privateKeyAddress: privateKeyAddress,
68-
persistenceCheckboxDisabled: 'disabled-checked',
61+
await page.goto(`${extensionPopupURL}/`)
62+
await page.evaluate(() => {
63+
const iframe = document.createElement('iframe')
64+
iframe.src = 'https://global.transak.com'
65+
document.body.appendChild(iframe)
6966
})
70-
await expect(page.getByTestId('account-selector')).toBeVisible()
71-
await page.getByRole('link', { name: 'Buy' }).click()
72-
await expect(page.getByRole('heading', { name: 'Buy ROSE' })).toBeVisible()
73-
74-
await page
75-
.getByText(
76-
'I understand that I’m using a third-party solution and Oasis* does not carry any responsibility over the usage of this solution.',
77-
)
78-
.click()
67+
await page.locator('iframe').scrollIntoViewIfNeeded()
7968
await expect(page.frameLocator('iframe')!.getByAltText('Powered by Transak')).toBeVisible()
80-
// Wait for conversion to be loaded otherwise clicking "Buy now" early reloads the iframe
81-
await expect(page.frameLocator('iframe')!.locator('#transak-calculator-source:disabled')).toHaveValue(
82-
/\d/,
83-
)
84-
await page.frameLocator('iframe')!.getByText('Buy now').click()
85-
await expect(page.frameLocator('iframe')!.getByText(/email/i).first()).toBeVisible()
8669
})
8770

8871
test('recover from fatal errors', async ({ extensionPopupURL, context }) => {

playwright/tests/fiat.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test.describe('Fiat on-ramp', () => {
5454
await expectNoErrorsInConsole(page)
5555
await setup(page)
5656
await page.route('https://*.transak.com/?*', route =>
57-
route.fulfill({ status: 301, headers: { Location: 'https://global-stg.transak.com/' } }),
57+
route.fulfill({ status: 302, headers: { Location: 'https://global-stg.transak.com/' } }),
5858
)
5959

6060
await page
@@ -70,7 +70,7 @@ test.describe('Fiat on-ramp', () => {
7070
await expectNoErrorsInConsole(page)
7171
await setup(page)
7272
await page.route('https://*.transak.com/*', route =>
73-
route.fulfill({ status: 301, headers: { Location: 'https://phishing-transak.com/' } }),
73+
route.fulfill({ status: 302, headers: { Location: 'https://phishing-transak.com/' } }),
7474
)
7575
await page.route('https://phishing-transak.com/', route => route.fulfill({ body: `phishing` }))
7676

src/app/pages/FiatOnrampPage/index.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ export function FiatOnramp() {
5555
const thirdPartyAcknowledged = useSelector(selectThirdPartyAcknowledged)
5656
// Intentionally not responsive. If it initializes with embedded iframe, user
5757
// inputs some data, then resizes: do not lose user's inputs!
58-
const [shouldOpenTransakInNewTab] = useState(window.innerWidth <= 768 || window.innerHeight <= 700)
58+
const [shouldOpenTransakInNewTab, setShouldOpenTransakInNewTab] = useState(
59+
window.innerWidth <= 768 || window.innerHeight <= 700,
60+
)
5961

6062
// Ignore refreshing account balance. Don't destroy and re-create iframe if balance changes and account balance is loading again.
6163
const [isInitialLoading, setInitialLoading] = useState(true)
@@ -164,6 +166,23 @@ export function FiatOnramp() {
164166
// 'allow-top-navigation-by-user-activation',
165167
].join(' ')}
166168
src={transakUrl}
169+
onLoad={event => {
170+
// Fallback to button if embedding fails.
171+
// Note: onError isn't called on X-Frame-Options errors, and onLoad is called even on error. So rely on Transak's postMessage.
172+
const onMessage = (event: MessageEvent) => {
173+
if (event?.data?.event_id === 'TRANSAK_WIDGET_INITIALISED') {
174+
// Success
175+
clearTimeout(failureTimeoutId)
176+
window.removeEventListener('message', onMessage)
177+
}
178+
}
179+
window.addEventListener('message', onMessage)
180+
const failureTimeoutId = window.setTimeout(() => {
181+
// Error (assumed - after 5 seconds of not initializing)
182+
setShouldOpenTransakInNewTab(true)
183+
window.removeEventListener('message', onMessage)
184+
}, 5_000)
185+
}}
167186
style={{
168187
display: 'block',
169188
width: '100%',

0 commit comments

Comments
 (0)