Skip to content

How to use testing APIs ? #108

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

Open
MikaStark opened this issue Jan 15, 2025 · 1 comment
Open

How to use testing APIs ? #108

MikaStark opened this issue Jan 15, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@MikaStark
Copy link

Hi, thanks for the effort you've put into maintaining this package—it's much appreciated!

I've been trying to use provideMatomoTesting in my unit tests but found it difficult to understand how the testing APIs are designed to work. Specifically, I'm unclear about its behavior and capabilities:

  • Does it maintain any state?
  • Is there a way to list or retrieve tracking events?
  • Is it simply a No-op tracker provider, or does it support more functionality?

For example, the MatomoTestingTracker JSDoc states:

All getter methods will immediately resolve to an empty value. This can be customized by setting a custom Matomo instance with setMatomoInstance().

However, setMatomoInstance is marked as deprecated and replaced by MATOMO_TESTING_INSTANCE, which isn’t exported by the package—so it's inaccessible. Is this intentional?

In short, could you consider adding a documentation page specifically about testing? It would be incredibly helpful to better understand how to use the testing APIs effectively.

Thanks!

@EmmanuelRoux
Copy link
Owner

EmmanuelRoux commented Jan 15, 2025

@MikaStark you’re right, testing api is relatively recent and not fully implemented yet. This will come in a future version and a doc will be published when the api is stabilized.

Currently you can use following setup to interact with the tracker provided using provideMatomoTesting():

TestBed.configureTestingModule({
  providers: [
    provideMatomoTesting(),
  ]
});

const tracker: MatomoTestingTracker = TestBed.inject(MatomoTestingTracker);

It is a no-op tracker memoizing history if commands. You can retrieve history of commands using following methods:

MatomoTestingTracker.getAllCommands(): unknown[][]; // Get all commands sent to Matomo
MatomoTestingTracker.getCommand(index: number): unknown[] | undefined; // Get a command by index
MatomoTestingTracker.reset(): void; // Reset commands history

For getter methods, the api is not well defined. The default provided instance will always return empty values.
To override this behavior, you can provide a custom MatomoInstance using:

TestBed.configureTestingModule({
  providers: [
    provideMatomoTesting(),
    {
      provide: MATOMO_TESTING_INSTANCE,
      useValue: { /* whatever you want */ } satisfies MatomoInstance,
    }
  ]
});

(A more convenient api will come later for this use case.)

MATOMO_TESTING_INSTANCE token is exported since version 7.0.1

EmmanuelRoux added a commit that referenced this issue Jan 15, 2025
EmmanuelRoux added a commit that referenced this issue Jan 15, 2025
## [7.0.1](v7.0.0...v7.0.1) (2025-01-15)

### Bug Fixes

* export `MATOMO_TESTING_INSTANCE` token ([81abc32](81abc32)), closes [#108](#108)
@EmmanuelRoux EmmanuelRoux added the enhancement New feature or request label Jan 16, 2025
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