/** * Copyright (c) 2019 Paul Armstrong */ import Button from '../Button'; import React from 'react'; import Ripple from '../Ripple'; import { fireEvent, render } from 'react-native-testing-library'; import { StyleSheet, TouchableOpacity } from 'react-native'; describe('Button', () => { describe('icon', () => { test('renders an icon before the title', () => { const FakeIcon = (): React.ReactElement =>
; const { getByType } = render(); expect(getByType(FakeIcon)).not.toBeUndefined(); }); test('does not render the title when iconOnly', () => { const FakeIcon = (): React.ReactElement => ; const { getByText } = render(); expect(() => getByText('tacos')).toThrow(); }); }); describe('onPress', () => { test('property is called when button is pressed', () => { const handlePress = jest.fn(); const { getByType } = render(); fireEvent.press(getByType(TouchableOpacity)); expect(handlePress).toHaveBeenCalled(); }); test('property is not called when disabled', () => { const handlePress = jest.fn(); const { getByType } = render(); fireEvent.press(getByType(TouchableOpacity)); expect(handlePress).not.toHaveBeenCalled(); }); }); describe('styles', () => { describe('active', () => { test('sets active styles while press inside', () => { const { getByType } = render(); fireEvent(getByType(TouchableOpacity), 'pressIn', { nativeEvent: { locationX: 0, locationY: 0 } }); expect(StyleSheet.flatten(getByType(Ripple).props.style)).toMatchObject({ shadowOffset: { width: 0, height: 2 }, shadowRadius: 3, }); }); test('removes active styles after press out', () => { const { getByType } = render(); fireEvent(getByType(TouchableOpacity), 'pressIn', { nativeEvent: { locationX: 0, locationY: 0 } }); fireEvent(getByType(TouchableOpacity), 'pressOut', { nativeEvent: { locationX: 0, locationY: 0 } }); expect(StyleSheet.flatten(getByType(Ripple).props.style)).toMatchObject({ shadowOffset: { width: 0, height: 2 }, shadowRadius: 5, }); }); test('when disabled, does not set active styles', () => { const { getByType } = render(); fireEvent(getByType(TouchableOpacity), 'pressIn', { nativeEvent: { locationX: 0, locationY: 0 } }); expect(StyleSheet.flatten(getByType(Ripple).props.style)).toMatchObject({ shadowOffset: { width: 0, height: 2 }, shadowRadius: 5, }); fireEvent(getByType(TouchableOpacity), 'pressOut', { nativeEvent: { locationX: 0, locationY: 0 } }); expect(StyleSheet.flatten(getByType(Ripple).props.style)).toMatchObject({ shadowOffset: { width: 0, height: 2 }, shadowRadius: 5, }); }); }); }); });