diff --git a/package-lock.json b/package-lock.json index 4fa8809a..823dd954 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "axios": "^1.17.0", "events": "^3.3.0", "gd-eventlog": "^0.1.27", - "incyclist-services": "^1.9.26", + "incyclist-services": "^1.9.27", "mqtt": "^5.15.2", "path-browserify": "^1.0.1", "process": "^0.11.10", @@ -12907,9 +12907,9 @@ } }, "node_modules/incyclist-services": { - "version": "1.9.26", - "resolved": "https://registry.npmjs.org/incyclist-services/-/incyclist-services-1.9.26.tgz", - "integrity": "sha512-hxSE5Vw3verEIzhw9qxkCDdKTiY+LlfELOVGryFSkAh7zCrLm3N1Eczs/hRZzPsZP9+UGxTaMAvMJjfycVc6mA==", + "version": "1.9.27", + "resolved": "https://registry.npmjs.org/incyclist-services/-/incyclist-services-1.9.27.tgz", + "integrity": "sha512-MTQbfiX95uHyahK8jR1Jn7+TrckHaUQ8cxJrxx1VU4Ogit7zmqhKqQXtYNAtRV+VVvwvtJyyTICI6xP5sQm9JQ==", "dependencies": { "@garmin/fitsdk": "^21.221.0", "axios": "^1.21.0", diff --git a/package.json b/package.json index f2033de0..57a58d79 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "axios": "^1.17.0", "events": "^3.3.0", "gd-eventlog": "^0.1.27", - "incyclist-services": "^1.9.26", + "incyclist-services": "^1.9.27", "mqtt": "^5.15.2", "path-browserify": "^1.0.1", "process": "^0.11.10", diff --git a/src/assets/intervals.png b/src/assets/intervals.png deleted file mode 100644 index 4289e66f..00000000 Binary files a/src/assets/intervals.png and /dev/null differ diff --git a/src/components/AppsDialog/AppsDialog.stories.tsx b/src/components/AppsDialog/AppsDialog.stories.tsx index 161de15f..f2d32f02 100644 --- a/src/components/AppsDialog/AppsDialog.stories.tsx +++ b/src/components/AppsDialog/AppsDialog.stories.tsx @@ -57,9 +57,9 @@ const meta: Meta = { visible: true, onClose: fn(), apps: [ - { name: 'Strava', key: 'strava', iconUrl: '', isConnected: true }, - { name: 'Intervals.icu', key: 'intervals', iconUrl: '', isConnected: false }, - { name: 'Komoot', key: 'komoot', iconUrl: '', isConnected: false }, + { name: 'Strava', key: 'strava', isConnected: true }, + { name: 'Intervals.icu', key: 'intervals', isConnected: false }, + { name: 'Komoot', key: 'komoot', isConnected: false }, ], renderApp: (key) => , }, @@ -74,9 +74,9 @@ export const Default: Story = {}; export const AllDisconnected: Story = { args: { apps: [ - { name: 'Strava', key: 'strava', iconUrl: '', isConnected: false }, - { name: 'Intervals.icu', key: 'intervals', iconUrl: '', isConnected: false }, - { name: 'Komoot', key: 'komoot', iconUrl: '', isConnected: false }, + { name: 'Strava', key: 'strava', isConnected: false }, + { name: 'Intervals.icu', key: 'intervals', isConnected: false }, + { name: 'Komoot', key: 'komoot', isConnected: false }, ], renderApp: (key) => , }, @@ -85,9 +85,9 @@ export const AllDisconnected: Story = { export const AllConnected: Story = { args: { apps: [ - { name: 'Strava', key: 'strava', iconUrl: '', isConnected: true }, - { name: 'Intervals.icu', key: 'intervals', iconUrl: '', isConnected: true }, - { name: 'Komoot', key: 'komoot', iconUrl: '', isConnected: true }, + { name: 'Strava', key: 'strava', isConnected: true }, + { name: 'Intervals.icu', key: 'intervals', isConnected: true }, + { name: 'Komoot', key: 'komoot', isConnected: true }, ], renderApp: (key) => , }, @@ -96,9 +96,9 @@ export const AllConnected: Story = { export const StravaConnected: Story = { args: { apps: [ - { name: 'Strava', key: 'strava', iconUrl: '', isConnected: true }, - { name: 'Intervals.icu', key: 'intervals', iconUrl: '', isConnected: false }, - { name: 'Komoot', key: 'komoot', iconUrl: '', isConnected: false }, + { name: 'Strava', key: 'strava', isConnected: true }, + { name: 'Intervals.icu', key: 'intervals', isConnected: false }, + { name: 'Komoot', key: 'komoot', isConnected: false }, ], renderApp: (key) => , }, diff --git a/src/components/AppsDialog/AppsDialog.test.tsx b/src/components/AppsDialog/AppsDialog.test.tsx index 7b5f42c8..87857e33 100644 --- a/src/components/AppsDialog/AppsDialog.test.tsx +++ b/src/components/AppsDialog/AppsDialog.test.tsx @@ -9,9 +9,9 @@ jest.mock('incyclist-services', () => { connected: Record = { strava: false, intervals: false, komoot: false }; openSettings = jest.fn(() => ([ - { name: 'Strava', key: 'strava', iconUrl: 's.svg', isConnected: this.connected.strava }, - { name: 'Intervals.icu', key: 'intervals', iconUrl: 'i.svg', isConnected: this.connected.intervals }, - { name: 'Komoot', key: 'komoot', iconUrl: 'k.svg', isConnected: this.connected.komoot }, + { name: 'Strava', key: 'strava', isConnected: this.connected.strava }, + { name: 'Intervals.icu', key: 'intervals', isConnected: this.connected.intervals }, + { name: 'Komoot', key: 'komoot', isConnected: this.connected.komoot }, ])); } diff --git a/src/components/AppsDialog/AppsDialogView.test.tsx b/src/components/AppsDialog/AppsDialogView.test.tsx index e31c9d0f..2d0b400e 100644 --- a/src/components/AppsDialog/AppsDialogView.test.tsx +++ b/src/components/AppsDialog/AppsDialogView.test.tsx @@ -17,9 +17,9 @@ jest.mock("../KomootSettings/KomootSettings", () => ({ })) const MOCK_APPS: AppDisplayProps[] = [ - { name: "Strava", key: "strava", iconUrl: "https://example.com/s.svg", isConnected: true }, - { name: "Intervals.icu", key: "intervals", iconUrl: "https://example.com/i.svg", isConnected: false }, - { name: "Komoot", key: "komoot", iconUrl: "https://example.com/k.svg", isConnected: false }, + { name: "Strava", key: "strava", isConnected: true }, + { name: "Intervals.icu", key: "intervals", isConnected: false }, + { name: "Komoot", key: "komoot", isConnected: false }, ] const MOCK_PROPS: AppsDialogViewProps = { @@ -44,9 +44,43 @@ describe('AppsDialogView', () => { expect(getByText('Strava')).toBeTruthy(); }); - it('renders with empty apps array without crashing', () => { - const { getByText } = render(); + it('renders no entries for an empty apps array', () => { + const { queryByText } = render(); + expect(queryByText('Strava')).toBeNull(); + expect(queryByText('Intervals.icu')).toBeNull(); + expect(queryByText('Komoot')).toBeNull(); + }); + + it('renders exactly the apps provided by the service', () => { + const apps: AppDisplayProps[] = [ + { name: 'Strava', key: 'strava', isConnected: true }, + { name: 'TrainingPeaks', key: 'trainingpeaks', isConnected: false }, + ]; + const { getByText, queryByText } = render(); expect(getByText('Strava')).toBeTruthy(); + expect(getByText('TrainingPeaks')).toBeTruthy(); + expect(queryByText('Intervals.icu')).toBeNull(); + expect(queryByText('Komoot')).toBeNull(); + }); + + it('renders the entries in the order provided by the service', () => { + const apps: AppDisplayProps[] = [ + { name: 'Komoot', key: 'komoot', isConnected: false }, + { name: 'Strava', key: 'strava', isConnected: false }, + ]; + const { getAllByText } = render(); + const names = getAllByText(/Komoot|Strava/).map(n => n.props.children); + expect(names).toEqual(['Komoot', 'Strava']); + }); + + it('expands an app that is not known to the app (generic OAuth settings) with its name', () => { + const apps: AppDisplayProps[] = [{ name: 'TrainingPeaks', key: 'trainingpeaks', isConnected: false }]; + const renderApp = jest.fn(() => null); + const { getByText } = render(); + + fireEvent.press(getByText('TrainingPeaks')); + + expect(renderApp).toHaveBeenCalledWith('trainingpeaks', MOCK_PROPS.onClose, false); }); it('renders with all sections collapsed without crashing', () => { @@ -60,4 +94,51 @@ describe('AppsDialogView', () => { // If it doesn't crash, the test passes. expect(getByText('Strava')).toBeTruthy(); }); + + describe('accessibility', () => { + it('every entry is a button that tells the name of the app and its connection state', () => { + const { getByRole } = render(); + + expect(getByRole('button', { name: 'Strava, connected' })).toBeTruthy(); + expect(getByRole('button', { name: 'Intervals.icu, not connected' })).toBeTruthy(); + expect(getByRole('button', { name: 'Komoot, not connected' })).toBeTruthy(); + }); + + it('the entry tells whether it is expanded', () => { + const { getByLabelText } = render(); + + expect(getByLabelText('Strava, connected').props.accessibilityState).toMatchObject({ expanded: false }); + + fireEvent.press(getByLabelText('Strava, connected')); + + expect(getByLabelText('Strava, connected').props.accessibilityState).toMatchObject({ expanded: true }); + expect(getByLabelText('Komoot, not connected').props.accessibilityState).toMatchObject({ expanded: false }); + }); + + it('collapsing an entry is reported as well', () => { + const { getByLabelText } = render(); + + fireEvent.press(getByLabelText('Strava, connected')); + fireEvent.press(getByLabelText('Strava, connected')); + + expect(getByLabelText('Strava, connected').props.accessibilityState).toMatchObject({ expanded: false }); + }); + + it('the label follows a change of the connection state', () => { + const { getByLabelText, rerender } = render(); + const connectedKomoot = MOCK_APPS.map(a => a.key === 'komoot' ? { ...a, isConnected: true } : a); + + rerender(); + + expect(getByLabelText('Komoot, connected')).toBeTruthy(); + }); + + it('the chevron is not read by screen readers', () => { + const { queryAllByText } = render(); + + // one chevron per entry, none of them is visible to screen readers + expect(queryAllByText('>', { includeHiddenElements: false })).toHaveLength(0); + expect(queryAllByText('>', { includeHiddenElements: true })).toHaveLength(MOCK_APPS.length); + }); + }); }); diff --git a/src/components/AppsDialog/AppsDialogView.tsx b/src/components/AppsDialog/AppsDialogView.tsx index 022a424b..605bcf46 100644 --- a/src/components/AppsDialog/AppsDialogView.tsx +++ b/src/components/AppsDialog/AppsDialogView.tsx @@ -6,13 +6,6 @@ import { colors, textSizes } from '../../theme'; import { AppSettingsContext } from '../AppSettingsView/AppSettingsView'; import { OAuthAppSettings } from '../OAuthAppSettings/OAuthAppSettings'; import { KomootSettings } from '../KomootSettings/KomootSettings'; -import { OAuthAppKey } from '../OAuthAppSettings/types'; - -const APP_SECTIONS = [ - { key: 'strava', name: 'Strava' }, - { key: 'intervals', name: 'Intervals.icu' }, - { key: 'komoot', name: 'Komoot' }, -]; // Stable reference so AppSettingsContext.Provider (rendered inside a .map() below, where // useMemo can't be used) doesn't re-trigger consumers on every render — see SonarCloud @@ -35,8 +28,9 @@ export const AppsDialogView = ({ if (key === 'komoot') { return ; } - return ; - }, []); + const name = apps.find(a => a.key === key)?.name; + return ; + }, [apps]); const renderApp = renderAppProp || defaultRenderApp; @@ -53,12 +47,14 @@ export const AppsDialogView = ({ buttons={dialogButtons} > - {APP_SECTIONS.map((section) => { - const appData = apps.find(a => a.key === section.key); - const isConnected = appData?.isConnected ?? false; - const isExpanded = expandedKey === section.key; + {apps.map((app) => { + const isConnected = app.isConnected ?? false; + const isExpanded = expandedKey === app.key; + + const handlePress = () => handleToggle(app.key); - const handlePress = () => handleToggle(section.key); + // the status is only shown as colour of the badge: screen readers get it as part of the label + const accessibilityLabel = `${app.name}, ${isConnected ? 'connected' : 'not connected'}`; const badgeStyle = [ styles.badge, @@ -66,17 +62,24 @@ export const AppsDialogView = ({ ]; return ( - + - {section.name} + {app.name} - + {'>'} @@ -84,7 +87,7 @@ export const AppsDialogView = ({ {isExpanded && ( - {renderApp(section.key, onClose, false)} + {renderApp(app.key, onClose, false)} )} diff --git a/src/components/AppsSettings/types.ts b/src/components/AppsSettings/types.ts index ec249798..a71485b7 100644 --- a/src/components/AppsSettings/types.ts +++ b/src/components/AppsSettings/types.ts @@ -1,6 +1,5 @@ export interface AppDisplayProps { name: string; key: string; - iconUrl: string; isConnected: boolean; } diff --git a/src/components/OAuthAppSettings/OAuthAppSettings.credentials.test.tsx b/src/components/OAuthAppSettings/OAuthAppSettings.credentials.test.tsx new file mode 100644 index 00000000..2774e667 --- /dev/null +++ b/src/components/OAuthAppSettings/OAuthAppSettings.credentials.test.tsx @@ -0,0 +1,77 @@ +import React from 'react'; +import { render, fireEvent, waitFor } from '@testing-library/react-native'; +import { TouchableOpacity } from 'react-native'; +import InAppBrowser from 'react-native-inappbrowser-reborn'; +import { OAuthAppSettings } from './OAuthAppSettings'; + +const mockConnect = jest.fn().mockResolvedValue(true); + +jest.mock('incyclist-services', () => ({ + useAppsService: () => ({ + openAppSettings: jest.fn().mockReturnValue({ isConnected: false, operations: [] }), + connect: (...args: unknown[]) => mockConnect(...args), + disconnect: jest.fn(), + enableOperation: jest.fn().mockReturnValue([]), + }), +})); + +jest.mock('../AppSettingsView', () => ({ + AppSettingsView: ({ connectButton }: { connectButton: () => React.ReactNode }) => connectButton(), +})); + +jest.mock('../../assets/btn_strava_connectwith_orange.svg', () => ({ + __esModule: true, + default: () => null, +})); + +/** simulates the auth-server redirecting back to the app, echoing the state that the app sent */ +const mockCallback = (query: Record) => { + (InAppBrowser.openAuth as jest.Mock).mockImplementation(async (url: string) => { + const state = new URL(url).searchParams.get('state') as string; + const params = new URLSearchParams({ ...query, state }); + return { type: 'success', url: `incyclist://oauth/callback?${params.toString()}` }; + }); +}; + +afterEach(() => { + jest.clearAllMocks(); +}); + +describe('OAuthAppSettings - credentials handed to the AppsService', () => { + + const connectWith = async (appKey: string, query: Record) => { + mockCallback(query); + const { UNSAFE_getByType } = render(); + fireEvent.press(UNSAFE_getByType(TouchableOpacity)); + await waitFor(() => expect(mockConnect).toHaveBeenCalled()); + }; + + it('passes tokens, id and expiration on, in the same way for every app', async () => { + for (const appKey of ['strava', 'intervals', 'trainingpeaks']) { + mockConnect.mockClear(); + await connectWith(appKey, { accesstoken: 'at', refreshtoken: 'rt', id: '42', expiration: '1767261600000' }); + + expect(mockConnect).toHaveBeenCalledWith(appKey, { + accesstoken: 'at', + refreshtoken: 'rt', + id: '42', + expiration: '1767261600000', + }); + } + }); + + it('passes expiration as null if the auth-server does not provide one (Strava, Intervals)', async () => { + await connectWith('strava', { accesstoken: 'at', refreshtoken: 'rt', id: '42' }); + + expect(mockConnect).toHaveBeenCalledWith('strava', expect.objectContaining({ accesstoken: 'at', expiration: null })); + }); + + it('does not connect if the auth-server reports an error', async () => { + mockCallback({ error: 'aborted' }); + const { UNSAFE_getByType } = render(); + fireEvent.press(UNSAFE_getByType(TouchableOpacity)); + + await waitFor(() => expect(InAppBrowser.openAuth).toHaveBeenCalled()); + expect(mockConnect).not.toHaveBeenCalled(); + }); +}); diff --git a/src/components/OAuthAppSettings/OAuthAppSettings.generic.test.tsx b/src/components/OAuthAppSettings/OAuthAppSettings.generic.test.tsx new file mode 100644 index 00000000..c881ff7f --- /dev/null +++ b/src/components/OAuthAppSettings/OAuthAppSettings.generic.test.tsx @@ -0,0 +1,58 @@ +import React from 'react'; +import { render } from '@testing-library/react-native'; +import { OAuthAppSettings } from './OAuthAppSettings'; + +jest.mock('incyclist-services', () => ({ + useAppsService: jest.fn(() => ({ + openAppSettings: jest.fn().mockReturnValue({ isConnected: false, operations: [] }), + connect: jest.fn().mockResolvedValue(true), + disconnect: jest.fn(), + enableOperation: jest.fn().mockReturnValue([]), + })), +})); + +const mockAppSettingsView = jest.fn((_props: Record) => null); +jest.mock('../AppSettingsView', () => ({ + AppSettingsView: (props: Record) => mockAppSettingsView(props), +})); + +jest.mock('../../assets/btn_strava_connectwith_orange.svg', () => ({ + __esModule: true, + default: () => null, +})); + +const lastProps = () => mockAppSettingsView.mock.calls.at(-1)![0] as { + title: string; + connectButton: () => React.ReactElement; +}; + +describe('OAuthAppSettings - apps provided by the service', () => { + beforeEach(() => { + mockAppSettingsView.mockClear(); + }); + + it('uses the display name of the app as title and in the connect button', () => { + render(); + + expect(lastProps().title).toBe('TrainingPeaks'); + + const button = render(lastProps().connectButton()); + expect(button.getByText('Connect with TrainingPeaks')).toBeTruthy(); + }); + + it('falls back to the key if no name is provided', () => { + render(); + + expect(lastProps().title).toBe('trainingpeaks'); + const button = render(lastProps().connectButton()); + expect(button.getByText('Connect with trainingpeaks')).toBeTruthy(); + }); + + it('keeps the official Connect-with-Strava button for Strava', () => { + render(); + + expect(lastProps().title).toBe('Strava'); + const button = render(lastProps().connectButton()); + expect(button.queryByText('Connect with Strava')).toBeNull(); + }); +}); diff --git a/src/components/OAuthAppSettings/OAuthAppSettings.tsx b/src/components/OAuthAppSettings/OAuthAppSettings.tsx index 0736fc6c..41769ad8 100644 --- a/src/components/OAuthAppSettings/OAuthAppSettings.tsx +++ b/src/components/OAuthAppSettings/OAuthAppSettings.tsx @@ -1,5 +1,5 @@ import React, { useState, useRef, useCallback, useEffect } from 'react'; -import { Linking, StyleSheet, TouchableOpacity } from 'react-native'; +import { Linking, StyleSheet, Text, TouchableOpacity } from 'react-native'; import InAppBrowser from 'react-native-inappbrowser-reborn'; import { useAppsService } from 'incyclist-services'; import type { AppsOperation } from 'incyclist-services'; @@ -14,7 +14,7 @@ import type { OAuthAppSettingsProps } from './types'; const OAUTH_BASE = 'https://auth.incyclist.com'; const REDIRECT_URI = 'incyclist://oauth/callback'; -const OAuthAppSettings = ({ appKey, onBack }: OAuthAppSettingsProps) => { +const OAuthAppSettings = ({ appKey, name, onBack }: OAuthAppSettingsProps) => { const service = useAppsService(); const { logEvent, logError } = useLogging('OAuthAppSettings'); @@ -100,6 +100,8 @@ const OAuthAppSettings = ({ appKey, onBack }: OAuthAppSettingsProps) => { accesstoken: parsed.searchParams.get('accesstoken'), refreshtoken: parsed.searchParams.get('refreshtoken'), id: parsed.searchParams.get('id'), + // same contract for all apps: whatever the auth-server provides is passed on, services handles the formats + expiration: parsed.searchParams.get('expiration'), }; await service.connect(appKey, credentials); @@ -146,22 +148,19 @@ const OAuthAppSettings = ({ appKey, onBack }: OAuthAppSettingsProps) => { [onConnect, isConnecting], ); - const intervalsConnectButton = useCallback( + const title = name ?? appKey; + + const genericConnectButton = useCallback( () => ( - - {React.createElement( - require('react-native').Text, - { style: styles.intervalsButtonText }, - 'Connect with Intervals.icu', - )} + + {`Connect with ${title}`} ), - [onConnect, isConnecting], + [onConnect, isConnecting, title], ); - const connectButton = appKey === 'strava' ? stravaConnectButton : intervalsConnectButton; - - const title = appKey === 'strava' ? 'Strava' : 'Intervals.icu'; + // Strava requires its official "Connect with Strava" button + const connectButton = appKey === 'strava' ? stravaConnectButton : genericConnectButton; return ( { }; const styles = StyleSheet.create({ - intervalsButton: { + connectButton: { backgroundColor: colors.buttonPrimary, paddingHorizontal: 16, paddingVertical: 10, borderRadius: 4, alignItems: 'center', }, - intervalsButtonText: { + connectButtonText: { color: colors.text, fontSize: textSizes.normalText, fontWeight: '600', diff --git a/src/components/OAuthAppSettings/index.ts b/src/components/OAuthAppSettings/index.ts index d7311f3d..dc8ca2fe 100644 --- a/src/components/OAuthAppSettings/index.ts +++ b/src/components/OAuthAppSettings/index.ts @@ -1,2 +1,2 @@ export { OAuthAppSettings } from './OAuthAppSettings'; -export type { OAuthAppSettingsProps, OAuthAppKey } from './types'; \ No newline at end of file +export type { OAuthAppSettingsProps } from './types'; \ No newline at end of file diff --git a/src/components/OAuthAppSettings/types.ts b/src/components/OAuthAppSettings/types.ts index eb6db45b..a34704b9 100644 --- a/src/components/OAuthAppSettings/types.ts +++ b/src/components/OAuthAppSettings/types.ts @@ -1,9 +1,10 @@ import type { AppsOperation } from 'incyclist-services'; -export type OAuthAppKey = 'strava' | 'intervals'; - export interface OAuthAppSettingsProps { - appKey: OAuthAppKey; + /** Key of the app as provided by the AppsService (e.g. 'strava', 'intervals', 'trainingpeaks') */ + appKey: string; + /** Display name of the app, as provided by the AppsService. Falls back to the key */ + name?: string; onBack?: () => void; }