-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide testing implementation
You can now easily test your components depending on Matomo features by using `provideMatomoTesting()` in `TestBed`. Cherry-picked from ef5d358
- Loading branch information
1 parent
088798b
commit eeb6d68
Showing
9 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './public-api'; |
110 changes: 110 additions & 0 deletions
110
projects/ngx-matomo-client/testing/matomo-testing-instance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { MatomoECommerceItem, MatomoInstance } from 'ngx-matomo-client/core'; | ||
|
||
/** | ||
* No-op implementation of {@link MatomoInstance} | ||
*/ | ||
export class MatomoTestingInstance implements MatomoInstance { | ||
areCookiesEnabled(): boolean { | ||
return false; | ||
} | ||
|
||
getAttributionCampaignKeyword(): string { | ||
return ''; | ||
} | ||
|
||
getAttributionCampaignName(): string { | ||
return ''; | ||
} | ||
|
||
getAttributionInfo(): string[] { | ||
return []; | ||
} | ||
|
||
getAttributionReferrerTimestamp(): string { | ||
return ''; | ||
} | ||
|
||
getAttributionReferrerUrl(): string { | ||
return ''; | ||
} | ||
|
||
getCrossDomainLinkingUrlParameter(): string { | ||
return ''; | ||
} | ||
|
||
getCurrentUrl(): string { | ||
return ''; | ||
} | ||
|
||
getCustomDimension(customDimensionId: number): string { | ||
return ''; | ||
} | ||
|
||
getCustomPagePerformanceTiming(): string { | ||
return ''; | ||
} | ||
|
||
getCustomVariable(index: number, scope: string): string { | ||
return ''; | ||
} | ||
|
||
getEcommerceItems(): MatomoECommerceItem[] { | ||
return []; | ||
} | ||
|
||
getExcludedReferrers(): string[] { | ||
return []; | ||
} | ||
|
||
getLinkTrackingTimer(): number { | ||
return 0; | ||
} | ||
|
||
getMatomoUrl(): string { | ||
return ''; | ||
} | ||
|
||
getPageViewId(): string { | ||
return ''; | ||
} | ||
|
||
getPiwikUrl(): string { | ||
return ''; | ||
} | ||
|
||
getRememberedConsent(): number | string { | ||
return ''; | ||
} | ||
|
||
getRememberedCookieConsent(): number | string { | ||
return ''; | ||
} | ||
|
||
getUserId(): string { | ||
return ''; | ||
} | ||
|
||
getVisitorId(): string { | ||
return ''; | ||
} | ||
|
||
getVisitorInfo(): unknown[] { | ||
return []; | ||
} | ||
|
||
hasCookies(): boolean { | ||
return false; | ||
} | ||
|
||
hasRememberedConsent(): boolean { | ||
return false; | ||
} | ||
|
||
isConsentRequired(): boolean { | ||
return false; | ||
} | ||
|
||
isUserOptedOut(): boolean { | ||
return false; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
projects/ngx-matomo-client/testing/matomo-testing-tracker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { MatomoInstance, MatomoTracker } from 'ngx-matomo-client/core'; | ||
import { MatomoTestingInstance } from './matomo-testing-instance'; | ||
|
||
/** | ||
* No-op implementation of {@link MatomoTracker} | ||
* | ||
* All commands are memoized and can later be retrieved using | ||
* {@link getAllCommands getAllCommands()} or {@link getCommand getCommand(index)}. | ||
* | ||
* All <i>getter</i> methods will immediately resolve to an <i>empty value</i>. | ||
* This can be customized by setting a custom Matomo instance with {@link setMatomoInstance setMatomoInstance()}. | ||
*/ | ||
export class MatomoTestingTracker extends MatomoTracker { | ||
#fakeInstance: MatomoInstance = new MatomoTestingInstance(); | ||
#paq: unknown[][] = []; | ||
|
||
/** Retrieve the current Matomo instance */ | ||
getMatomoInstance(): MatomoInstance { | ||
return this.#fakeInstance; | ||
} | ||
|
||
/** Set the current matomo instance */ | ||
setMatomoInstance(instance: MatomoInstance) { | ||
this.#fakeInstance = instance; | ||
} | ||
|
||
/** Retrieve all memoized commands */ | ||
getAllCommands(): unknown[][] { | ||
return [...this.#paq]; | ||
} | ||
|
||
/** Retrieve nth memoized command */ | ||
getCommand(index: number): unknown[] | undefined { | ||
return this.#paq[index]; | ||
} | ||
|
||
/** Clear all memoized commands */ | ||
reset() { | ||
this.#paq = []; | ||
} | ||
|
||
protected push(command: unknown[]): void { | ||
this.#paq.push(command); | ||
} | ||
|
||
protected async pushFn<T>(fn: (matomo: MatomoInstance) => T): Promise<T> { | ||
return fn(this.#fakeInstance); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
projects/ngx-matomo-client/testing/matomo-testing.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ModuleWithProviders, NgModule } from '@angular/core'; | ||
import { | ||
MatomoOptOutFormComponent, | ||
MatomoTrackClickDirective, | ||
MatomoTrackerDirective, | ||
} from 'ngx-matomo-client/core'; | ||
import { provideMatomoTesting } from './providers'; | ||
|
||
@NgModule({ | ||
imports: [MatomoTrackerDirective, MatomoTrackClickDirective, MatomoOptOutFormComponent], | ||
exports: [MatomoTrackerDirective, MatomoTrackClickDirective, MatomoOptOutFormComponent], | ||
}) | ||
export class MatomoTestingModule { | ||
static forRoot(): ModuleWithProviders<MatomoTestingModule> { | ||
return { | ||
ngModule: MatomoTestingModule, | ||
providers: [provideMatomoTesting()], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"$schema": "../../../node_modules/ng-packagr/ng-package.schema.json", | ||
"lib": { | ||
"entryFile": "public-api.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { EnvironmentProviders, makeEnvironmentProviders, Provider } from '@angular/core'; | ||
import { | ||
MATOMO_CONFIGURATION, | ||
MatomoConfiguration, | ||
MatomoFeature, | ||
MatomoTracker, | ||
} from 'ngx-matomo-client/core'; | ||
import { MatomoTestingTracker } from './matomo-testing-tracker'; | ||
|
||
/** | ||
* Set up a no-op Matomo tracker. Useful for testing scenario. | ||
* | ||
* | ||
* Testing tracker is available as both {@link MatomoTracker} and {@link MatomoTestingTracker} injection tokens, | ||
* the latter allowing to customize testing behavior (see {@link MatomoTestingTracker}). | ||
* | ||
* @see MatomoTestingTracker | ||
*/ | ||
export function provideMatomoTesting( | ||
config: Partial<MatomoConfiguration> | (() => Partial<MatomoConfiguration>) = {}, | ||
..._ignored: MatomoFeature[] | ||
): EnvironmentProviders { | ||
const providers: Provider[] = [ | ||
MatomoTestingTracker, | ||
{ | ||
provide: MatomoTracker, | ||
useExisting: MatomoTestingTracker, | ||
}, | ||
]; | ||
|
||
// Allow to provide a configuration, because it can be useful for users depending on `MATOMO_CONFIGURATION` in their code. | ||
if (typeof config === 'function') { | ||
providers.push({ | ||
provide: MATOMO_CONFIGURATION, | ||
useFactory: config, | ||
}); | ||
} else { | ||
providers.push({ | ||
provide: MATOMO_CONFIGURATION, | ||
useValue: config, | ||
}); | ||
} | ||
|
||
return makeEnvironmentProviders(providers); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { provideMatomoTesting } from './providers'; | ||
export { MatomoTestingModule } from './matomo-testing.module'; | ||
export { MatomoTestingTracker } from './matomo-testing-tracker'; | ||
export { MatomoTestingInstance } from './matomo-testing-instance'; |