Fix tests

This commit is contained in:
Andreas Eriksson 2021-10-03 10:38:16 +02:00
parent 198de6ebf2
commit 98cb630b9e
4 changed files with 95 additions and 63 deletions

View File

@ -1,9 +1,14 @@
import { fireEvent } from '@testing-library/react-native'
import {
fireEvent,
waitFor,
waitForElementToBeRemoved,
} from '@testing-library/react-native'
import React from 'react'
import RNCalendarEvents from 'react-native-calendar-events'
import * as AddCalendarEvent from 'react-native-add-calendar-event'
import { render } from '../../utils/testHelpers'
import { SaveToCalendar } from '../saveToCalendar.component'
import Toast from 'react-native-simple-toast'
import { Linking, Platform } from 'react-native'
jest.mock('react-native-simple-toast', () => ({
SHORT: 'short',
@ -25,9 +30,8 @@ jest.mock('react-native', () => {
return RN
})
jest.mock('react-native-calendar-events', () => ({
saveEvent: jest.fn().mockResolvedValue('52'),
requestPermissions: jest.fn().mockResolvedValue('authorized'),
jest.mock('react-native-add-calendar-event', () => ({
presentEventCreatingDialog: jest.fn().mockResolvedValue({ action: 'SAVED' }),
}))
const defaultEvent = {
@ -35,10 +39,12 @@ const defaultEvent = {
startDate: '2021-06-19 13:00',
endDate: '2021-06-19 14:00',
location: 'Gubbängsskolan',
allDay: false,
}
const defaultProps = {
event: defaultEvent,
child: { name: 'Olle Testperson' },
}
const setup = (customProps = {}) => {
@ -63,90 +69,90 @@ test('renders save to calendar', () => {
fireEvent.press(screen.getByTestId('actionsButton'))
expect(screen.getByText(/Spara/i)).toBeTruthy()
})
test('requests calendar permissons', () => {
const screen = setup()
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByText(/Spara/i))
expect(RNCalendarEvents.requestPermissions).toHaveBeenCalled()
expect(screen.getByTestId('saveToCalendar')).toBeTruthy()
})
test('can save an event to the calendar', async () => {
const screen = setup()
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByTestId('saveToCalendar'))
expect(AddCalendarEvent.presentEventCreatingDialog).toHaveBeenCalledWith({
allDay: false,
endDate: '2021-06-19T12:00:00.000Z',
startDate: '2021-06-19T11:00:00.000Z',
title: 'Olle Testperson - Utvecklingssamtal',
location: defaultEvent.location,
})
await waitForElementToBeRemoved(() => screen.getByTestId('saveToCalendar'))
})
test('removes any null values from the event', async () => {
const screen = setup({
event: {
...defaultEvent,
location: null,
description: null,
},
})
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByText(/Spara/i))
await RNCalendarEvents.requestPermissions()
fireEvent.press(screen.getByTestId('saveToCalendar'))
expect(RNCalendarEvents.saveEvent).toHaveBeenCalledWith('Utvecklingssamtal', {
startDate: '2021-06-19T11:00:00.000Z',
expect(AddCalendarEvent.presentEventCreatingDialog).toHaveBeenCalledWith({
allDay: false,
endDate: '2021-06-19T12:00:00.000Z',
})
})
test('removes any null values from the event', async () => {
const screen = setup()
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByText(/Spara/i))
await RNCalendarEvents.requestPermissions()
expect(RNCalendarEvents.saveEvent).toHaveBeenCalledWith('Utvecklingssamtal', {
startDate: '2021-06-19T11:00:00.000Z',
endDate: '2021-06-19T12:00:00.000Z',
location: 'Gubbängsskolan',
title: 'Olle Testperson - Utvecklingssamtal',
})
await waitForElementToBeRemoved(() => screen.getByTestId('saveToCalendar'))
})
test('calls toast with success', async () => {
const screen = setup()
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByText(/Spara/i))
await RNCalendarEvents.requestPermissions()
await RNCalendarEvents.saveEvent()
fireEvent.press(screen.getByTestId('saveToCalendar'))
expect(Toast.showWithGravity).toHaveBeenCalledWith(
'✔️ Sparad till kalender',
'short',
'bottom'
await waitFor(() =>
expect(Toast.showWithGravity).toHaveBeenCalledWith(
'✔️ Sparad till kalender',
'short',
'bottom'
)
)
})
test('says if something goes wrong', async () => {
const screen = setup()
RNCalendarEvents.saveEvent.mockRejectedValueOnce()
AddCalendarEvent.presentEventCreatingDialog.mockImplementationOnce(() => {
throw new Error('Error from test')
})
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByText(/Spara/i))
await RNCalendarEvents.requestPermissions()
await RNCalendarEvents.saveEvent()
fireEvent.press(screen.getByTestId('saveToCalendar'))
expect(Toast.showWithGravity).toHaveBeenCalledWith(
'Något gick fel',
'short',
'bottom'
await waitFor(() =>
expect(Toast.showWithGravity).toHaveBeenCalledWith(
'Något gick fel',
'short',
'bottom'
)
)
})
test('tells user if they havent authorized calendar', async () => {
const screen = setup()
RNCalendarEvents.requestPermissions.mockResolvedValueOnce('not auth')
AddCalendarEvent.presentEventCreatingDialog.mockImplementationOnce(() => {
throw 'permissionNotGranted'
})
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByText(/Spara/i))
await RNCalendarEvents.requestPermissions()
await RNCalendarEvents.saveEvent()
fireEvent.press(screen.getByTestId('saveToCalendar'))
expect(Toast.showWithGravity).toHaveBeenCalledWith(
'Du måste godkänna åtkomst till kalendern',
@ -154,3 +160,25 @@ test('tells user if they havent authorized calendar', async () => {
'bottom'
)
})
test('can open device calendar to day on IOS', async () => {
const screen = setup()
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByTestId('openDayInDeviceCalendar'))
expect(Linking.openURL).toHaveBeenCalledWith('calshow:645793200')
})
test('can open device calendar to day on Android', async () => {
const screen = setup()
Platform.OS = 'android'
fireEvent.press(screen.getByTestId('actionsButton'))
fireEvent.press(screen.getByTestId('openDayInDeviceCalendar'))
expect(Linking.openURL).toHaveBeenCalledWith(
'content://com.android.calendar/time/1624100400000'
)
})

View File

@ -46,6 +46,8 @@ export const SaveToCalendar = ({ event, child }: SaveToCalendarProps) => {
const openEventCreateDialog = async ({
title,
description,
allDay,
startDate,
endDate,
location,
@ -63,26 +65,26 @@ export const SaveToCalendar = ({ event, child }: SaveToCalendarProps) => {
const firstName = studentName(child.name)
const titleWithChildPrefix = `${firstName} - ${title}`
const detailsWithoutEmpty = removeEmptyValues(details)
const eventConfig: AddCalendarEvent.CreateOptions = {
title: titleWithChildPrefix,
startDate: detailsWithoutEmpty.startDate,
endDate: detailsWithoutEmpty.endDate,
notes: event.description,
allDay: event.allDay,
location: event.location,
startDate: details.startDate,
endDate: details.endDate,
notes: description,
allDay: allDay,
location: location,
}
const configWithoutEmptyVals = removeEmptyValues(eventConfig)
try {
const result = await AddCalendarEvent.presentEventCreatingDialog(
eventConfig
configWithoutEmptyVals
)
if (result.action === 'SAVED') {
toast(translate('calender.saveToCalenderSuccess'))
}
} catch (error) {
console.log(error)
if (error === 'permissionNotGranted') {
toast(translate('calender.approveAccessToCalender'))
} else {
@ -116,6 +118,7 @@ export const SaveToCalendar = ({ event, child }: SaveToCalendarProps) => {
>
<MenuItem
accessoryLeft={PlusSquareOutline}
testID="saveToCalendar"
title={(evaProps) => (
<Text {...evaProps} maxFontSizeMultiplier={2}>
{translate('calender.saveToCalender')}
@ -125,6 +128,7 @@ export const SaveToCalendar = ({ event, child }: SaveToCalendarProps) => {
/>
<MenuItem
accessoryLeft={CalendarOutlineIcon}
testID="openDayInDeviceCalendar"
title={(evaProps) => (
<Text {...evaProps} maxFontSizeMultiplier={2}>
{translate('calender.openDayInDeviceCalendar')}

View File

@ -43,8 +43,7 @@
"react-native": "0.65.1",
"react-native-add-calendar-event": "^4.0.0",
"react-native-animatable": "^1.3.3",
"react-native-dev-menu": "^4.0.2",
"react-native-dev-menu": "^4.0.2",
"react-native-device-info": "^8.3.3",
"react-native-fix-image": "2.1.0",
"react-native-gesture-handler": "^1.10.3",

View File

@ -5,6 +5,7 @@ import { EvaIconsPack } from '@ui-kitten/eva-icons'
import React, { ReactElement } from 'react'
import { LanguageProvider } from '../context/language/languageContext'
import { translations } from './translation'
import { lightTheme } from '../design/themes'
export const render = (
ui: ReactElement<any, string>,
@ -14,7 +15,7 @@ export const render = (
return (
<>
<IconRegistry icons={EvaIconsPack} />
<ApplicationProvider {...eva} theme={eva.light}>
<ApplicationProvider {...eva} theme={lightTheme}>
<LanguageProvider
cache={false}
data={translations}