d636ec6339
* add testing lib * remove coverage folder from git * finished basic test setup * fix tests typescript and import paths * add first snapshot * testing utils * rename test files; update script flags; ++tests * testing utils functions * testing downloadAndResize wip * remove download test * specify unwanted coverage paths; remove update snapshots flag * fix strings tests * testing downloadAndResize method * increasing testing * fixing snapshots wip * fixed shell mobile snapshot * adding snapshots for the screens * fix onboard snapshot * fix typescript issues * fix TabsSelector snapshot * Account for testing device's locale in ago() tests * Remove platform detection on regex * mocking store state wip * mocking store state * increasing test coverage * increasing test coverage * increasing test coverage on src/screens * src/screens (except for profile) above 80% cov * testing profile screen wip * increase coverage on Menu and TabsSelector * mocking profile ui state wip * mocking profile ui state wip * fixing mobileshell tests wip * snapshots using testing-library * fixing profile tests wip * removing mobile shell tests * src/view/com tests wip * remove unnecessary patch-package * fixed profile test error * clear mocks after every test * fix base mocked store values (getters) * fix base mocked store values (hasLoaded, nonReplyFeed) * profile screen above 80% coverage * testing custom hooks * improving composer coverage * fix tests after merge * finishing composer coverage * improving src/com/discover coverage * improve src/view/com/login coverage fix SuggestedFollows tests adding some comments * fix SuggestedFollows tests * improve src/view/com/profile coverage extra minor fixes * improve src/view/com/notifications coverage * update coverage ignore patterns * rename errorMessageTryAgainButton increase SuggestedFollows converage * improve src/view/com/posts coverage * improve src/view/com/onboard coverage * update snapshot * improve src/view/com/post coverage * improve src/view/com/post-thread coverage rename ErrorMessage tests test Debug and Log components * init testing state * testing root-store * updating comments * small fixes * removed extra console logs * improve src/state/models coverage refactor rootStore rename some spies * adding cleanup method after tests * improve src/state/models coverage * improve src/state/models coverage * improve src/state/models coverage * improve src/state/models coverage * test setInterval in setupState * Clean up tests and update Home screen state management * Remove some tests we dont need * Remove snapshot tests * Remove any tests that dont demonstrate clear value * Cleanup Co-authored-by: Paul Frazee <pfrazee@gmail.com>
240 lines
5.2 KiB
TypeScript
240 lines
5.2 KiB
TypeScript
import React, {useRef} from 'react'
|
|
import {
|
|
Share,
|
|
StyleProp,
|
|
StyleSheet,
|
|
TouchableOpacity,
|
|
TouchableWithoutFeedback,
|
|
View,
|
|
ViewStyle,
|
|
} from 'react-native'
|
|
import {IconProp} from '@fortawesome/fontawesome-svg-core'
|
|
import RootSiblings from 'react-native-root-siblings'
|
|
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
|
|
import {Text} from '../text/Text'
|
|
import {Button, ButtonType} from './Button'
|
|
import {colors} from '../../../lib/styles'
|
|
import {toShareUrl} from '../../../../lib/strings'
|
|
import {useStores} from '../../../../state'
|
|
import {ReportPostModal, ConfirmModal} from '../../../../state/models/shell-ui'
|
|
import {TABS_ENABLED} from '../../../../build-flags'
|
|
|
|
const HITSLOP = {left: 10, top: 10, right: 10, bottom: 10}
|
|
|
|
export interface DropdownItem {
|
|
icon?: IconProp
|
|
label: string
|
|
onPress: () => void
|
|
}
|
|
|
|
export type DropdownButtonType = ButtonType | 'bare'
|
|
|
|
export function DropdownButton({
|
|
type = 'bare',
|
|
style,
|
|
items,
|
|
label,
|
|
menuWidth,
|
|
children,
|
|
}: {
|
|
type: DropdownButtonType
|
|
style?: StyleProp<ViewStyle>
|
|
items: DropdownItem[]
|
|
label?: string
|
|
menuWidth?: number
|
|
children?: React.ReactNode
|
|
}) {
|
|
const ref = useRef<TouchableOpacity>(null)
|
|
|
|
const onPress = () => {
|
|
ref.current?.measure(
|
|
(
|
|
_x: number,
|
|
_y: number,
|
|
width: number,
|
|
height: number,
|
|
pageX: number,
|
|
pageY: number,
|
|
) => {
|
|
if (!menuWidth) {
|
|
menuWidth = 200
|
|
}
|
|
createDropdownMenu(
|
|
pageX + width - menuWidth,
|
|
pageY + height,
|
|
menuWidth,
|
|
items,
|
|
)
|
|
},
|
|
)
|
|
}
|
|
|
|
if (type === 'bare') {
|
|
return (
|
|
<TouchableOpacity
|
|
style={style}
|
|
onPress={onPress}
|
|
hitSlop={HITSLOP}
|
|
// Fix an issue where specific references cause runtime error in jest environment
|
|
ref={process.env.JEST_WORKER_ID != null ? null : ref}>
|
|
{children}
|
|
</TouchableOpacity>
|
|
)
|
|
}
|
|
return (
|
|
<View ref={ref}>
|
|
<Button onPress={onPress} style={style} label={label}>
|
|
{children}
|
|
</Button>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
export function PostDropdownBtn({
|
|
style,
|
|
children,
|
|
itemHref,
|
|
isAuthor,
|
|
onCopyPostText,
|
|
onDeletePost,
|
|
}: {
|
|
style?: StyleProp<ViewStyle>
|
|
children?: React.ReactNode
|
|
itemHref: string
|
|
itemTitle: string
|
|
isAuthor: boolean
|
|
onCopyPostText: () => void
|
|
onDeletePost: () => void
|
|
}) {
|
|
const store = useStores()
|
|
|
|
const dropdownItems: DropdownItem[] = [
|
|
TABS_ENABLED
|
|
? {
|
|
icon: ['far', 'clone'],
|
|
label: 'Open in new tab',
|
|
onPress() {
|
|
store.nav.newTab(itemHref)
|
|
},
|
|
}
|
|
: undefined,
|
|
{
|
|
icon: ['far', 'paste'],
|
|
label: 'Copy post text',
|
|
onPress() {
|
|
onCopyPostText()
|
|
},
|
|
},
|
|
{
|
|
icon: 'share',
|
|
label: 'Share...',
|
|
onPress() {
|
|
Share.share({url: toShareUrl(itemHref)})
|
|
},
|
|
},
|
|
{
|
|
icon: 'circle-exclamation',
|
|
label: 'Report post',
|
|
onPress() {
|
|
store.shell.openModal(new ReportPostModal(itemHref))
|
|
},
|
|
},
|
|
isAuthor
|
|
? {
|
|
icon: ['far', 'trash-can'],
|
|
label: 'Delete post',
|
|
onPress() {
|
|
store.shell.openModal(
|
|
new ConfirmModal(
|
|
'Delete this post?',
|
|
'Are you sure? This can not be undone.',
|
|
onDeletePost,
|
|
),
|
|
)
|
|
},
|
|
}
|
|
: undefined,
|
|
].filter(Boolean) as DropdownItem[]
|
|
|
|
return (
|
|
<DropdownButton style={style} items={dropdownItems} menuWidth={200}>
|
|
{children}
|
|
</DropdownButton>
|
|
)
|
|
}
|
|
|
|
function createDropdownMenu(
|
|
x: number,
|
|
y: number,
|
|
width: number,
|
|
items: DropdownItem[],
|
|
): RootSiblings {
|
|
const onPressItem = (index: number) => {
|
|
sibling.destroy()
|
|
items[index].onPress()
|
|
}
|
|
const onOuterPress = () => sibling.destroy()
|
|
const sibling = new RootSiblings(
|
|
(
|
|
<>
|
|
<TouchableWithoutFeedback onPress={onOuterPress}>
|
|
<View style={styles.bg} />
|
|
</TouchableWithoutFeedback>
|
|
<View style={[styles.menu, {left: x, top: y, width}]}>
|
|
{items.map((item, index) => (
|
|
<TouchableOpacity
|
|
key={index}
|
|
style={[styles.menuItem]}
|
|
onPress={() => onPressItem(index)}>
|
|
{item.icon && (
|
|
<FontAwesomeIcon style={styles.icon} icon={item.icon} />
|
|
)}
|
|
<Text style={styles.label}>{item.label}</Text>
|
|
</TouchableOpacity>
|
|
))}
|
|
</View>
|
|
</>
|
|
),
|
|
)
|
|
return sibling
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
bg: {
|
|
position: 'absolute',
|
|
top: 0,
|
|
right: 0,
|
|
bottom: 0,
|
|
left: 0,
|
|
backgroundColor: '#000',
|
|
opacity: 0.1,
|
|
},
|
|
menu: {
|
|
position: 'absolute',
|
|
backgroundColor: '#fff',
|
|
borderRadius: 14,
|
|
opacity: 1,
|
|
paddingVertical: 6,
|
|
},
|
|
menuItem: {
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
paddingVertical: 10,
|
|
paddingLeft: 15,
|
|
paddingRight: 40,
|
|
},
|
|
menuItemBorder: {
|
|
borderTopWidth: 1,
|
|
borderTopColor: colors.gray1,
|
|
marginTop: 4,
|
|
paddingTop: 12,
|
|
},
|
|
icon: {
|
|
marginLeft: 6,
|
|
marginRight: 8,
|
|
},
|
|
label: {
|
|
fontSize: 18,
|
|
},
|
|
})
|