Skip to content

Make Analytics injectable for testability#2133

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/refactor-analytics-usage
Draft

Make Analytics injectable for testability#2133
Copilot wants to merge 6 commits intomainfrom
copilot/refactor-analytics-usage

Conversation

Copy link

Copilot AI commented Feb 3, 2026

Analytics(getBackend()) calls FirebaseAnalytics.instance directly, preventing widget tests from mocking analytics. The root Sharezone widget cannot be tested as a result.

Changes

Core widget refactoring

  • Sharezone accepts optional Analytics parameter, falls back to blocDependencies.analytics
  • Removed static Analytics field in favor of instance parameter
  • Centralized Analytics creation in run_app.dart (single instance, passed to both RegistrationGateway and BlocDependencies)

Gateway updates

  • RegistrationGateway and LinkProviderGateway accept optional Analytics parameter
  • All default to Analytics(getBackend()) when not provided (backward compatible)

BLoC and page integration

  • AccountPageBlocFactory, EmailAndPasswordLinkBloc accept optional Analytics
  • AuthApp provides Analytics via Provider for login/signup flows
  • Pages use context.read<Analytics>() or BlocProvider.of<SharezoneContext>(context).analytics

Example

// Tests can now mock Analytics
final mockAnalytics = Analytics(NullAnalyticsBackend());
final sharezone = Sharezone(
  blocDependencies: deps,
  analytics: mockAnalytics,  // Injectable
  // ...
);

// Production code unchanged (defaults work)
final sharezone = Sharezone(
  blocDependencies: deps,  // Uses deps.analytics
  // ...
);

All existing code works unchanged. Tests can now pass NullAnalyticsBackend for mocking.

Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor the usage of Analytics(getBackend())</issue_title>
<issue_description>In many places of this app we use Analytics(getBackend(). This is a problem because we can't run our root widget Sharezone in a widget test. getBackend() calls FirebaseAnalytics.instance which is not mockable. Instead, should pass a FirebaseAnalytics object so that we can pass a mocked version in tests.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 5 commits February 3, 2026 21:04
Co-authored-by: nilsreichardt <24459435+nilsreichardt@users.noreply.github.com>
Co-authored-by: nilsreichardt <24459435+nilsreichardt@users.noreply.github.com>
…lify test

Co-authored-by: nilsreichardt <24459435+nilsreichardt@users.noreply.github.com>
Co-authored-by: nilsreichardt <24459435+nilsreichardt@users.noreply.github.com>
Co-authored-by: nilsreichardt <24459435+nilsreichardt@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor usage of Analytics(getBackend()) for mockable tests Make Analytics injectable for testability Feb 3, 2026
Copilot AI requested a review from nilsreichardt February 3, 2026 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor the usage of Analytics(getBackend())

2 participants