Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CustomerIO class to Enable Dependency Injection, Better Testing, and Flexibility #84

Open
AristideVB opened this issue Sep 22, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@AristideVB
Copy link

Description:

The current design of the CustomerIO class relies on static methods, making it difficult to write unit tests and achieve Dependency Injection. Converting static methods to instance methods will make the class more testable and flexible.

Proposed Changes:

  1. Convert static methods in CustomerIO to instance methods.
  2. Continue to use the existing private constructor const CustomerIO._(); to ensure that CustomerIO remains a singleton.

This approach will maintain the singleton behavior while offering greater flexibility for testing and modularization.

Example:

Before:

// Using static method
CustomerIO.subscribeToInAppEventListener(_handleInAppEvent);

After:

// Using instance method through singleton
final customerIO = CustomerIO.instance;
customerIO.subscribeToInAppEventListener(_handleInAppEvent);

Practical Example with InAppMessagesBloc:

Suppose you have an InAppMessagesBloc that handles incoming in-app messages. In the current architecture, mocking CustomerIO for testing the Bloc is difficult.

Before:

class InAppMessagesBloc extends Bloc<InAppMessagesEvent, InAppMessagesState> {
  InAppMessagesBloc() : super(InAppMessagesInitial()) {
    on<InAppMessagesEventReceived>(_onInAppMessagesEventReceived);

    _inAppMessageStreamSubscription =
        CustomerIO.subscribeToInAppEventListener(_handleInAppEvent);
  }

  late StreamSubscription<dynamic> _inAppMessageStreamSubscription;
 // ...
}

After:

With the proposed changes, we can inject a mock or stubbed CustomerIO instance into the InAppMessagesBloc for better unit testing.

class InAppMessagesBloc extends Bloc<InAppMessagesEvent, InAppMessagesState> {
  InAppMessagesBloc(CustomerIO customerIO) : super(InAppMessagesInitial()) {
    on<InAppMessagesEventReceived>(_onInAppMessagesEventReceived);

    _inAppMessageStreamSubscription =
        customerIO.subscribeToInAppEventListener(_handleInAppEvent);
  }

  late StreamSubscription<dynamic> _inAppMessageStreamSubscription;
 // ...
}

Benefits:

  • Enhances testability by allowing for mocking or stubbing of methods.
  • Allows for Dependency Injection in more modular codebases.
  • Maintains singleton behavior.
@Shahroz16 Shahroz16 added the enhancement New feature or request label Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants