Problem
Across almost all mock service implementations in the mobile app, we simulate asynchronous network responses using local setTimeout delays. This has led to:
- Significant code duplication:
await new Promise((r) => setTimeout(r, ...)) is copied repeatedly.
- Inconsistent and hardcoded delay values (sometimes locally defined as constants, other times hardcoded inline as raw numbers like
800, 500, 200).
- No global toggle to disable delays during automated testing or speed up mock mode.
Affected Files and Current Implementation
Here are the specific locations where this is duplicated:
1. apps/mobile/src/services/product.service.ts
Redefines delays and calls them repeatedly:
const MOCK_DELAYS = { FAST: 500, NORMAL: 600, SLOW: 800 } as const;
// Used 7 times:
await new Promise((r) => setTimeout(r, MOCK_DELAYS.SLOW));
await new Promise((r) => setTimeout(r, MOCK_DELAYS.FAST));
await new Promise((r) => setTimeout(r, MOCK_DELAYS.NORMAL));
2. apps/mobile/src/services/order.service.ts
Redefines delays and calls them repeatedly:
const MOCK_DELAYS = { FAST: 500, NORMAL: 600, SLOW: 800, INSTANT: 300 } as const;
// Used 4 times:
await new Promise((r) => setTimeout(r, MOCK_DELAYS.NORMAL));
await new Promise((r) => setTimeout(r, MOCK_DELAYS.FAST));
await new Promise((r) => setTimeout(r, MOCK_DELAYS.INSTANT));
3. apps/mobile/src/services/venture.service.ts
Defines another set of constants:
const MOCK_DELAY_MS = 500;
const MOCK_CREATE_DELAY_MS = 800;
const MOCK_MUTATION_DELAY_MS = 600;
// Used 7 times:
await new Promise((r) => setTimeout(r, MOCK_DELAY_MS));
await new Promise((r) => setTimeout(r, MOCK_CREATE_DELAY_MS));
await new Promise((r) => setTimeout(r, MOCK_MUTATION_DELAY_MS));
4. apps/mobile/src/services/project.service.ts
Uses hardcoded raw numbers inline (no constants):
await new Promise((r) => setTimeout(r, 800)); // used 4 times
await new Promise((r) => setTimeout(r, 500)); // used 1 time
5. apps/mobile/src/services/auth.service.ts
Uses hardcoded raw numbers inline (no constants):
await new Promise((r) => setTimeout(r, 500)); // used 2 times
await new Promise((r) => setTimeout(r, 200)); // used 2 times
6. apps/mobile/src/services/status.service.ts
Uses hardcoded raw number inline:
await new Promise((resolve) => setTimeout(resolve, 800));
Proposed Solution
- Create Utility Helper: Create a centralized helper function (e.g.,
mockDelay(speed?: 'fast' | 'normal' | 'slow' | 'instant' | number): Promise<void>) inside a shared services utility file (e.g., apps/mobile/src/services/api-utils.ts or a new mock-utils.ts).
- Support Bypass configuration: Allow bypassing the delay if a configuration flag (or environment variable like
EXPO_PUBLIC_NO_MOCK_DELAY) is enabled. This will speed up automated integration tests that run on mocks.
- Refactor All Mocks: Replace all the occurrences above with
await mockDelay('slow'), await mockDelay('fast'), etc.
Problem
Across almost all mock service implementations in the mobile app, we simulate asynchronous network responses using local
setTimeoutdelays. This has led to:await new Promise((r) => setTimeout(r, ...))is copied repeatedly.800,500,200).Affected Files and Current Implementation
Here are the specific locations where this is duplicated:
1.
apps/mobile/src/services/product.service.tsRedefines delays and calls them repeatedly:
2.
apps/mobile/src/services/order.service.tsRedefines delays and calls them repeatedly:
3.
apps/mobile/src/services/venture.service.tsDefines another set of constants:
4.
apps/mobile/src/services/project.service.tsUses hardcoded raw numbers inline (no constants):
5.
apps/mobile/src/services/auth.service.tsUses hardcoded raw numbers inline (no constants):
6.
apps/mobile/src/services/status.service.tsUses hardcoded raw number inline:
Proposed Solution
mockDelay(speed?: 'fast' | 'normal' | 'slow' | 'instant' | number): Promise<void>) inside a shared services utility file (e.g.,apps/mobile/src/services/api-utils.tsor a newmock-utils.ts).EXPO_PUBLIC_NO_MOCK_DELAY) is enabled. This will speed up automated integration tests that run on mocks.await mockDelay('slow'),await mockDelay('fast'), etc.