Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Binary file removed src/assets/intervals.png
Binary file not shown.
24 changes: 12 additions & 12 deletions src/components/AppsDialog/AppsDialog.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ const meta: Meta<typeof AppsDialogView> = {
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) => <MockAppContent appKey={key} isConnected={key === 'strava'} />,
},
Expand All @@ -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) => <MockAppContent appKey={key} isConnected={false} />,
},
Expand All @@ -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) => <MockAppContent appKey={key} isConnected={true} />,
},
Expand All @@ -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) => <MockAppContent appKey={key} isConnected={key === 'strava'} />,
},
Expand Down
6 changes: 3 additions & 3 deletions src/components/AppsDialog/AppsDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ jest.mock('incyclist-services', () => {
connected: Record<string, boolean> = { 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 },
]));
}

Expand Down
91 changes: 86 additions & 5 deletions src/components/AppsDialog/AppsDialogView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -44,9 +44,43 @@ describe('AppsDialogView', () => {
expect(getByText('Strava')).toBeTruthy();
});

it('renders with empty apps array without crashing', () => {
const { getByText } = render(<AppsDialogView {...MOCK_PROPS} apps={[]} />);
it('renders no entries for an empty apps array', () => {
const { queryByText } = render(<AppsDialogView {...MOCK_PROPS} apps={[]} />);
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(<AppsDialogView {...MOCK_PROPS} apps={apps} />);
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(<AppsDialogView {...MOCK_PROPS} apps={apps} />);
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(<AppsDialogView {...MOCK_PROPS} apps={apps} renderApp={renderApp} />);

fireEvent.press(getByText('TrainingPeaks'));

expect(renderApp).toHaveBeenCalledWith('trainingpeaks', MOCK_PROPS.onClose, false);
});

it('renders with all sections collapsed without crashing', () => {
Expand All @@ -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(<AppsDialogView {...MOCK_PROPS} />);

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(<AppsDialogView {...MOCK_PROPS} />);

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(<AppsDialogView {...MOCK_PROPS} />);

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(<AppsDialogView {...MOCK_PROPS} />);
const connectedKomoot = MOCK_APPS.map(a => a.key === 'komoot' ? { ...a, isConnected: true } : a);

rerender(<AppsDialogView {...MOCK_PROPS} apps={connectedKomoot} />);

expect(getByLabelText('Komoot, connected')).toBeTruthy();
});

it('the chevron is not read by screen readers', () => {
const { queryAllByText } = render(<AppsDialogView {...MOCK_PROPS} />);

// 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);
});
});
});
39 changes: 21 additions & 18 deletions src/components/AppsDialog/AppsDialogView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,8 +28,9 @@ export const AppsDialogView = ({
if (key === 'komoot') {
return <KomootSettings onBack={onBack} />;
}
return <OAuthAppSettings appKey={key as OAuthAppKey} onBack={onBack} />;
}, []);
const name = apps.find(a => a.key === key)?.name;
return <OAuthAppSettings appKey={key} name={name} onBack={onBack} />;
}, [apps]);

const renderApp = renderAppProp || defaultRenderApp;

Expand All @@ -53,38 +47,47 @@ export const AppsDialogView = ({
buttons={dialogButtons}
>
<View style={styles.container}>
{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,
{ backgroundColor: isConnected ? colors.success : colors.disabled }
];

return (
<View key={section.key} style={styles.section}>
<View key={app.key} style={styles.section}>
<TouchableOpacity
style={styles.header}
onPress={handlePress}
activeOpacity={0.7}
accessibilityRole="button"
accessibilityLabel={accessibilityLabel}
accessibilityState={{ expanded: isExpanded }}
>
<View style={styles.headerLeft}>
<Text style={styles.appName}>{section.name}</Text>
<Text style={styles.appName}>{app.name}</Text>
<View style={badgeStyle} />
</View>
<Text style={[styles.chevron, isExpanded && styles.chevronExpanded]}>
<Text
style={[styles.chevron, isExpanded && styles.chevronExpanded]}
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
>
{'>'}
</Text>
</TouchableOpacity>

{isExpanded && (
<View style={styles.body}>
<AppSettingsContext.Provider value={NON_STANDALONE_CONTEXT_VALUE}>
{renderApp(section.key, onClose, false)}
{renderApp(app.key, onClose, false)}
</AppSettingsContext.Provider>
</View>
)}
Expand Down
1 change: 0 additions & 1 deletion src/components/AppsSettings/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface AppDisplayProps {
name: string;
key: string;
iconUrl: string;
isConnected: boolean;
}
Original file line number Diff line number Diff line change
@@ -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<string, string>) => {
(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<string, string>) => {
mockCallback(query);
const { UNSAFE_getByType } = render(<OAuthAppSettings appKey={appKey} name={appKey} />);
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(<OAuthAppSettings appKey='trainingpeaks' name='TrainingPeaks' />);
fireEvent.press(UNSAFE_getByType(TouchableOpacity));

await waitFor(() => expect(InAppBrowser.openAuth).toHaveBeenCalled());
expect(mockConnect).not.toHaveBeenCalled();
});
});
Loading
Loading