Skip to content

refactor(mobile): centralize and unify mock delay simulation utility #222

Description

@masch

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

  1. 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).
  2. 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.
  3. Refactor All Mocks: Replace all the occurrences above with await mockDelay('slow'), await mockDelay('fast'), etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions