Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## Unreleased (develop)

- added: Swap success events name the swap mode, including Swap & Send from the send scene
- fixed: Android crash when opening DFX and other web pages on some Android 12+ phones, where the blurred header and tab bar redrew the page
- fixed: DFX shows an error instead of opening when it blocks the country of the user's internet connection, for Bitcoin and EVM wallets, which Edge logs in to DFX
- fixed: DFX sells entered in fiat open DFX with the crypto amount filled in
- fixed: The header back button in Buy and Sell plugin scenes acting on the other tab's plugin
- fixed: Exchange info, gift card info, Houdini chain, and Zcash migration status refreshes running twice per interval

## 4.53.0 (staging)
Expand Down
93 changes: 93 additions & 0 deletions src/__tests__/components/BlurBackground.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { describe, expect, it, jest } from '@jest/globals'
import type * as TestingLibrary from '@testing-library/react-native'
import type * as ReactModule from 'react'
import type * as ReactNative from 'react-native'

import type * as BlurBackgroundModule from '../../components/common/BlurBackground'

// BlurBackground picks its platform when it loads, so load a copy of it,
// with its own React and React Native, on an Android 12+ device. The JSX in
// this file renders with that copy of React:
let React: typeof ReactModule
let rtl: typeof TestingLibrary
let blur: typeof BlurBackgroundModule
jest.isolateModules(() => {
const { Platform } = jest.requireActual<typeof ReactNative>('react-native')
Platform.OS = 'android'
Object.defineProperty(Platform, 'Version', { get: () => 34 })
React = jest.requireActual<typeof ReactModule>('react')
rtl = jest.requireActual<typeof TestingLibrary>(
'@testing-library/react-native'
)
blur = jest.requireActual<typeof BlurBackgroundModule>(
'../../components/common/BlurBackground'
)
})

interface FocusedSceneProps {
hasWebView: boolean
}

/** A focused scene, publishing itself the way SceneWrapper does. */
const FocusedScene: ReactModule.FC<FocusedSceneProps> = props => {
blur.useSceneBlurTarget(true, props.hasWebView)
return null
}

/** The host component type `surface` renders over a focused scene. */
const renderedType = (
surface: ReactModule.ReactElement,
scene?: { hasWebView: boolean }
): string => {
const { toJSON, unmount } = rtl.render(
<>
{scene == null ? null : <FocusedScene hasWebView={scene.hasWebView} />}
{surface}
</>
)
const json = toJSON()
unmount()
if (json == null || Array.isArray(json)) throw new Error('no single surface')
return json.type
}

describe('blur surfaces over a focused WebView scene on Android', () => {
it('blurs modals over other scenes', () => {
expect(
renderedType(<blur.ModalBlurBackground />, { hasWebView: false })
).toBe('ExpoBlurView')
expect(renderedType(<blur.BlurUnderlayReversed />)).toBe('ExpoBlurView')
})

it('tints modals over a WebView scene', () => {
expect(
renderedType(<blur.ModalBlurBackground />, { hasWebView: true })
).toBe('View')
expect(
renderedType(<blur.BlurUnderlayReversed />, { hasWebView: true })
).toBe('View')
})

it('tints the chrome over a WebView scene', () => {
expect(
renderedType(<blur.ChromeBlurBackground />, { hasWebView: false })
).toBe('ExpoBlurView')
expect(
renderedType(<blur.ChromeBlurBackground />, { hasWebView: true })
).toBe('View')
})

it('blurs modals again once the WebView scene loses focus', () => {
const { toJSON, rerender, unmount } = rtl.render(
<>
<FocusedScene hasWebView />
<blur.ModalBlurBackground />
</>
)
expect(toJSON()).toMatchObject({ type: 'View' })

rerender(<blur.ModalBlurBackground />)
expect(toJSON()).toMatchObject({ type: 'ExpoBlurView' })
unmount()
})
})
94 changes: 94 additions & 0 deletions src/__tests__/components/GuiPluginBackButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { afterEach, describe, expect, it, jest } from '@jest/globals'
import { fireEvent, render } from '@testing-library/react-native'
import * as React from 'react'
import type * as ReactNative from 'react-native'

import {
PluginBackButton,
setPluginScene
} from '../../components/navigation/GuiPluginBackButton'

jest.mock('../../components/navigation/BackButton', () => {
const { TouchableOpacity } =
jest.requireActual<typeof ReactNative>('react-native')
return {
BackButton: (props: { onPress: () => void }) => (
<TouchableOpacity testID="backButton" onPress={props.onPress} />
)
}
})

const makeScene = (): { goBack: jest.Mock<() => boolean> } => ({
goBack: jest.fn(() => true)
})

const pressBack = (): void => {
const { getByTestId, unmount } = render(<PluginBackButton />)
fireEvent.press(getByTestId('backButton'))
unmount()
}

// The button's owner is module state, so each test releases what it took:
let releases: Array<() => void> = []
const register = (scene: ReturnType<typeof makeScene>): (() => void) => {
const release = setPluginScene(scene)
releases.push(release)
return release
}

afterEach(() => {
releases.forEach(release => {
release()
})
releases = []
})

describe('PluginBackButton', () => {
it('acts on the scene that registered last', () => {
const buyScene = makeScene()
const sellScene = makeScene()
register(buyScene)
register(sellScene)

pressBack()

expect(sellScene.goBack).toHaveBeenCalledTimes(1)
expect(buyScene.goBack).not.toHaveBeenCalled()
})

it('keeps the newly focused scene when the old one blurs after it', () => {
// React Navigation can focus the new tab's scene before it blurs the old:
const buyScene = makeScene()
const sellScene = makeScene()
const releaseBuy = register(buyScene)
register(sellScene)
releaseBuy()

pressBack()

expect(sellScene.goBack).toHaveBeenCalledTimes(1)
expect(buyScene.goBack).not.toHaveBeenCalled()
})

it('hands the button to the scene focused after a release', () => {
const buyScene = makeScene()
const sellScene = makeScene()
const releaseBuy = register(buyScene)
releaseBuy()
register(sellScene)

pressBack()

expect(sellScene.goBack).toHaveBeenCalledTimes(1)
expect(buyScene.goBack).not.toHaveBeenCalled()
})

it('does nothing once the scene holding it releases it', () => {
const buyScene = makeScene()
const releaseBuy = register(buyScene)
releaseBuy()

expect(pressBack).not.toThrow()
expect(buyScene.goBack).not.toHaveBeenCalled()
})
})
154 changes: 154 additions & 0 deletions src/__tests__/components/pluginBackHistory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import {
afterEach,
beforeEach,
describe,
expect,
it,
jest
} from '@jest/globals'

import {
BACK_SETTLE_MS,
makePluginBackHistory
} from '../../components/navigation/pluginBackHistory'

const DEEP_LINK = 'https://app.dfx.swiss/buy?blockchain=Monero&amount-in=500'
const LOGIN = 'https://app.dfx.swiss/login'
const EMAIL = 'https://app.dfx.swiss/login/mail'

beforeEach(() => {
jest.useFakeTimers()
})

afterEach(() => {
jest.useRealTimers()
})

describe('makePluginBackHistory', () => {
it('leaves the scene when the webview has no history', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation(LOGIN, false)

expect(history.pressBack()).toBe(false)
})

it('leaves the scene when going back bounces off a redirect', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
// DFX redirects its deep link to its login page:
history.onNavigation(DEEP_LINK, false)
history.onNavigation(LOGIN, true)

expect(history.pressBack()).toBe(true)
history.onNavigation(DEEP_LINK, false)
history.onNavigation(LOGIN, true)
jest.advanceTimersByTime(BACK_SETTLE_MS)

expect(leave).toHaveBeenCalledTimes(1)
})

it('leaves the scene when going back changes nothing', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation(LOGIN, true)

expect(history.pressBack()).toBe(true)
jest.advanceTimersByTime(BACK_SETTLE_MS)

expect(leave).toHaveBeenCalledTimes(1)
})

it('stays when going back reaches an earlier page', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation(LOGIN, true)
history.onNavigation(EMAIL, true)

expect(history.pressBack()).toBe(true)
history.onNavigation(LOGIN, true)
jest.advanceTimersByTime(BACK_SETTLE_MS)

expect(leave).not.toHaveBeenCalled()
})

it('treats a page that differs only in its query as the same page', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation(`${LOGIN}?redirect=buy`, true)

history.pressBack()
history.onNavigation(`${LOGIN}?redirect=buy&t=1`, true)
jest.advanceTimersByTime(BACK_SETTLE_MS)

expect(leave).toHaveBeenCalledTimes(1)
})

it('tells hash-routed pages apart', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation('https://plugin.example/#/amount', true)

history.pressBack()
history.onNavigation('https://plugin.example/#/start', true)
jest.advanceTimersByTime(BACK_SETTLE_MS)

expect(leave).not.toHaveBeenCalled()
})

it('does not leave once cancelled, as when the scene blurs', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation(LOGIN, true)

history.pressBack()
history.cancel()
jest.advanceTimersByTime(BACK_SETTLE_MS)

expect(leave).not.toHaveBeenCalled()
})

it('leaves on a second press that finds the page unchanged', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation(LOGIN, true)

expect(history.pressBack()).toBe(true)
// The caller leaves the scene itself when this returns false:
expect(history.pressBack()).toBe(false)
jest.advanceTimersByTime(BACK_SETTLE_MS)

// The dropped check must not leave a second time:
expect(leave).not.toHaveBeenCalled()
})

it('leaves on a second press during a redirect bounce', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation(DEEP_LINK, false)
history.onNavigation(LOGIN, true)

expect(history.pressBack()).toBe(true)
history.onNavigation(DEEP_LINK, false)
history.onNavigation(LOGIN, true)

expect(history.pressBack()).toBe(false)
jest.advanceTimersByTime(BACK_SETTLE_MS)
expect(leave).not.toHaveBeenCalled()
})

it('goes back again when the first press reached an earlier page', () => {
const leave = jest.fn()
const history = makePluginBackHistory(leave)
history.onNavigation(LOGIN, true)
history.onNavigation(EMAIL, true)

expect(history.pressBack()).toBe(true)
history.onNavigation(LOGIN, true)

// A fresh check starts from the earlier page:
expect(history.pressBack()).toBe(true)
jest.advanceTimersByTime(BACK_SETTLE_MS)
expect(leave).toHaveBeenCalledTimes(1)
})
})
Loading
Loading