Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Wallet] Support push notifications with an "open url" semantic #5413

Merged
merged 3 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
99 changes: 64 additions & 35 deletions packages/mobile/src/alert/AlertBanner.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import * as React from 'react'
import 'react-native'
import { render } from 'react-native-testing-library'
import { AlertBanner } from 'src/alert/AlertBanner'
import { fireEvent, render } from 'react-native-testing-library'
import { Provider } from 'react-redux'
import AlertBanner from 'src/alert/AlertBanner'
import { ErrorDisplayType } from 'src/alert/reducer'
import { createMockStore } from 'test/utils'

describe('AlertBanner', () => {
const baseProps = {
hideAlert: jest.fn(),
}

describe('when message passed in', () => {
it('renders message', () => {
const { toJSON } = render(
<AlertBanner
{...baseProps}
alert={{
type: 'message',
displayMethod: ErrorDisplayType.BANNER,
message: 'This is your shadow speaking',
dismissAfter: 0,
}}
/>
<Provider
store={createMockStore({
alert: {
type: 'message',
displayMethod: ErrorDisplayType.BANNER,
message: 'This is your shadow speaking',
dismissAfter: 0,
},
})}
>
<AlertBanner />
</Provider>
)
expect(toJSON()).toMatchSnapshot()
})
Expand All @@ -29,16 +29,19 @@ describe('AlertBanner', () => {
describe('when message and title passed in', () => {
it('renders title with message', () => {
const { toJSON } = render(
<AlertBanner
{...baseProps}
alert={{
type: 'message',
displayMethod: ErrorDisplayType.BANNER,
title: 'Declaration',
message: 'This is your shadow speaking',
dismissAfter: 0,
}}
/>
<Provider
store={createMockStore({
alert: {
type: 'message',
displayMethod: ErrorDisplayType.BANNER,
title: 'Declaration',
message: 'This is your shadow speaking',
dismissAfter: 0,
},
})}
>
<AlertBanner />
</Provider>
)
expect(toJSON()).toMatchSnapshot()
})
Expand All @@ -47,17 +50,43 @@ describe('AlertBanner', () => {
describe('when error message passed in', () => {
it('renders error message', () => {
const { toJSON } = render(
<AlertBanner
{...baseProps}
alert={{
type: 'error',
displayMethod: ErrorDisplayType.BANNER,
message: 'This is an error',
dismissAfter: 0,
}}
/>
<Provider
store={createMockStore({
alert: {
type: 'error',
displayMethod: ErrorDisplayType.BANNER,
message: 'This is an error',
dismissAfter: 0,
},
})}
>
<AlertBanner />
</Provider>
)
expect(toJSON()).toMatchSnapshot()
})
})

describe('when an action is provided', () => {
it('it dispatches the action when pressed', () => {
const store = createMockStore({
alert: {
type: 'message',
displayMethod: ErrorDisplayType.BANNER,
message: 'My message',
dismissAfter: 0,
action: { type: 'MY_ACTION' },
},
})
const { toJSON, getByTestId } = render(
<Provider store={store}>
<AlertBanner />
</Provider>
)
expect(toJSON()).toMatchSnapshot()

fireEvent.press(getByTestId('SmartTopAlertTouchable'))
expect(store.getActions()).toEqual([{ type: 'MY_ACTION' }])
})
})
})
64 changes: 22 additions & 42 deletions packages/mobile/src/alert/AlertBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,30 @@
import SmartTopAlert, { AlertTypes } from '@celo/react-components/components/SmartTopAlert'
import * as React from 'react'
import { connect } from 'react-redux'
import { useDispatch } from 'react-redux'
import { hideAlert } from 'src/alert/actions'
import { ErrorDisplayType, State as AlertState } from 'src/alert/reducer'
import { RootState } from 'src/redux/reducers'
import { ErrorDisplayType } from 'src/alert/reducer'
import useSelector from 'src/redux/useSelector'

interface StateProps {
alert: AlertState | null
}

interface DispatchProps {
hideAlert: typeof hideAlert
}
export default function AlertBanner() {
const alert = useSelector((state) => state.alert)
const dispatch = useDispatch()

type Props = StateProps & DispatchProps

const mapStateToProps = (state: RootState): StateProps => {
return {
alert: state.alert,
const onPress = () => {
const action = alert?.action ?? hideAlert()
dispatch(action)
}
}

const mapDispatchToProps = {
hideAlert,
return (
<SmartTopAlert
isVisible={!!alert && alert.displayMethod === ErrorDisplayType.BANNER}
// TODO: this looks like a hack to re-render, refactor!
timestamp={Date.now()}
text={alert && alert.message}
onPress={onPress}
type={alert && alert.type === 'error' ? AlertTypes.ERROR : AlertTypes.MESSAGE}
dismissAfter={alert && alert.dismissAfter}
buttonMessage={alert && alert.buttonMessage}
title={alert && alert.title}
/>
)
}

export class AlertBanner extends React.Component<Props> {
render() {
const { alert, hideAlert: hideAlertAction } = this.props

return (
<SmartTopAlert
isVisible={!!alert && alert.displayMethod === ErrorDisplayType.BANNER}
timestamp={Date.now()}
text={alert && alert.message}
onPress={hideAlertAction}
type={alert && alert.type === 'error' ? AlertTypes.ERROR : AlertTypes.MESSAGE}
dismissAfter={alert && alert.dismissAfter}
buttonMessage={alert && alert.buttonMessage}
title={alert && alert.title}
/>
)
}
}

export default connect<StateProps, DispatchProps, {}, RootState>(
mapStateToProps,
mapDispatchToProps
)(AlertBanner)
89 changes: 77 additions & 12 deletions packages/mobile/src/alert/__snapshots__/AlertBanner.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AlertBanner when an action is provided it dispatches the action when pressed 1`] = `
<View
style={
Object {
"overflow": "hidden",
}
}
testID="infoBanner"
>
<View
accessible={true}
focusable={true}
forwardedRef={[Function]}
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
Object {
"alignItems": "center",
"backgroundColor": "#0768AE",
"flexDirection": "row",
"justifyContent": "center",
"paddingBottom": 10,
"paddingHorizontal": 25,
"paddingTop": 10,
"transform": Array [
Object {
"translateY": -500,
},
],
}
}
testID="SmartTopAlertTouchable"
>
<Text
style={
Array [
Object {
"color": "#2E3338",
"fontFamily": "Inter-Regular",
"fontSize": 14,
"lineHeight": 18,
},
false,
Object {
"color": "white",
"lineHeight": undefined,
"textAlign": "center",
},
]
}
>
My message
</Text>
</View>
</View>
`;

exports[`AlertBanner when error message passed in renders error message 1`] = `
<View
style={
Expand Down Expand Up @@ -36,6 +98,7 @@ exports[`AlertBanner when error message passed in renders error message 1`] = `
],
}
}
testID="SmartTopAlertTouchable"
>
<View
style={
Expand Down Expand Up @@ -79,17 +142,17 @@ exports[`AlertBanner when error message passed in renders error message 1`] = `
"fontSize": 14,
"lineHeight": 18,
},
Object {
"color": "white",
"lineHeight": undefined,
"textAlign": "center",
},
Object {
"color": "#2E3338",
"fontFamily": "Inter-Medium",
"fontSize": 14,
"lineHeight": 18,
},
Object {
"color": "white",
"lineHeight": undefined,
"textAlign": "center",
},
]
}
>
Expand Down Expand Up @@ -135,6 +198,7 @@ exports[`AlertBanner when message and title passed in renders title with message
],
}
}
testID="SmartTopAlertTouchable"
>
<Text
style={
Expand All @@ -145,29 +209,29 @@ exports[`AlertBanner when message and title passed in renders title with message
"fontSize": 14,
"lineHeight": 18,
},
false,
Object {
"color": "white",
"lineHeight": undefined,
"textAlign": "center",
},
false,
]
}
>
<Text
style={
Array [
Object {
"color": "white",
"lineHeight": undefined,
"textAlign": "center",
},
Object {
"color": "#2E3338",
"fontFamily": "Inter-Medium",
"fontSize": 14,
"lineHeight": 18,
},
Object {
"color": "white",
"lineHeight": undefined,
"textAlign": "center",
},
]
}
>
Expand Down Expand Up @@ -217,6 +281,7 @@ exports[`AlertBanner when message passed in renders message 1`] = `
],
}
}
testID="SmartTopAlertTouchable"
>
<Text
style={
Expand All @@ -227,12 +292,12 @@ exports[`AlertBanner when message passed in renders message 1`] = `
"fontSize": 14,
"lineHeight": 18,
},
false,
Object {
"color": "white",
"lineHeight": undefined,
"textAlign": "center",
},
false,
]
}
>
Expand Down
Loading