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
87 changes: 87 additions & 0 deletions apps/mobile/src/ui/haptics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

const { platform, calls, nativeFault, expoHaptics, sentry } = vi.hoisted(() => {
const calls: string[] = [];
const nativeFault = { message: undefined as string | undefined };
// Plain functions, not `vi.fn`: a spy attaches its own handlers to a returned promise, which
// would hide the very unhandled rejection this test looks for.
const refuse =
(name: string) =>
(arg?: string): Promise<void> => {
calls.push(arg === undefined ? name : `${name}:${arg}`);
if (nativeFault.message) throw new Error(nativeFault.message);
return Promise.reject(new Error('A haptics engine is not available on this device'));
};
return {
platform: { OS: 'android' as 'android' | 'ios' },
calls,
nativeFault,
expoHaptics: {
performAndroidHapticsAsync: refuse('performAndroidHapticsAsync'),
selectionAsync: refuse('selectionAsync'),
impactAsync: refuse('impactAsync'),
notificationAsync: refuse('notificationAsync'),
AndroidHaptics: { Toggle_On: 'toggle-on', Confirm: 'confirm', Reject: 'reject' },
ImpactFeedbackStyle: { Light: 'light', Medium: 'medium', Heavy: 'heavy' },
NotificationFeedbackType: { Success: 'success', Warning: 'warning', Error: 'error' },
},
sentry: { captureException: vi.fn() },
};
});

vi.mock('react-native', () => ({ Platform: platform }));
vi.mock('expo-haptics', () => expoHaptics);
vi.mock('@sentry/react-native', () => sentry);

const { haptic } = await import('./haptics');

/** Lets the rejected promises settle, so that an unhandled one would be reported. */
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));

describe('haptic on a phone without a haptics engine', () => {
const unhandled = vi.fn();

beforeEach(() => {
process.on('unhandledRejection', unhandled);
});

afterEach(() => {
process.off('unhandledRejection', unhandled);
calls.length = 0;
nativeFault.message = undefined;
vi.clearAllMocks();
});

it('drops the refusal of the engine on Android, silently', async () => {
platform.OS = 'android';
expect(() => {
haptic('toastSuccess');
}).not.toThrow();
await settle();
expect(calls).toEqual(['performAndroidHapticsAsync:confirm']);
expect(unhandled).not.toHaveBeenCalled();
expect(sentry.captureException).not.toHaveBeenCalled();
});

it('drops the refusal of every iOS method, silently', async () => {
platform.OS = 'ios';
expect(() => {
haptic('favoriteOn');
haptic('ratingStar');
haptic('toastError');
}).not.toThrow();
await settle();
expect(calls).toEqual(['impactAsync:light', 'selectionAsync', 'notificationAsync:error']);
expect(unhandled).not.toHaveBeenCalled();
expect(sentry.captureException).not.toHaveBeenCalled();
});

it('stays silent when the native module throws before any promise', () => {
platform.OS = 'android';
nativeFault.message = 'Cannot find native module ExpoHaptics';
expect(() => {
haptic('arrived');
}).not.toThrow();
expect(calls).toHaveLength(1);
});
});
21 changes: 17 additions & 4 deletions apps/mobile/src/ui/haptics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import { Platform } from 'react-native';

type HapticKey = keyof typeof motion.haptics;

/**
* Starts a vibration without waiting for it. A vibration is a garnish: a phone without a
* haptics engine (Redmi Note 9: « A haptics engine is not available on this device ») rejects
* it, and that refusal is dropped in silence, neither an error nor a Sentry report.
*/
function vibrate(start: () => Promise<void>): void {
try {
start().catch(() => undefined);
} catch {
// A native module missing from the build throws before any promise: same silence.
}
}

/**
* A vibration of Direction-Artistique › Mouvement (D-030), as `mappings.motion.haptics` names it
* for each system: expo-haptics on iOS, `performAndroidHapticsAsync` on Android. Nothing else
Expand All @@ -12,19 +25,19 @@ type HapticKey = keyof typeof motion.haptics;
export function haptic(key: HapticKey): void {
const feedback = motion.haptics[key];
if (Platform.OS === 'android') {
void Haptics.performAndroidHapticsAsync(Haptics.AndroidHaptics[feedback.android]);
vibrate(() => Haptics.performAndroidHapticsAsync(Haptics.AndroidHaptics[feedback.android]));
return;
}
const { ios } = feedback;
switch (ios.method) {
case 'selectionAsync':
void Haptics.selectionAsync();
vibrate(() => Haptics.selectionAsync());
break;
case 'impactAsync':
void Haptics.impactAsync(Haptics.ImpactFeedbackStyle[ios.style]);
vibrate(() => Haptics.impactAsync(Haptics.ImpactFeedbackStyle[ios.style]));
break;
case 'notificationAsync':
void Haptics.notificationAsync(Haptics.NotificationFeedbackType[ios.style]);
vibrate(() => Haptics.notificationAsync(Haptics.NotificationFeedbackType[ios.style]));
break;
}
}
Loading