donni106 / matomo-tracker-react-native

Stand alone library for using Matomo tracking in React Native and Expo projects.
MIT License
47 stars 15 forks source link

Matomo tracker is not a constructor #28

Open Estroo opened 1 month ago

Estroo commented 1 month ago

Hi, I'm trying to encapsulate Matomo in jest mocking for my unit test. Everything worked well with useMatomo but as soon as it goes to newMatomoTracker everything goes wrong. Following the readme, here's my implementation in my app.tsx :

const instance = new MatomoTracker({
        urlBase: MATOMO_URL as string,
        siteId: MATOMO_SITE_ID  as number,
        userId: hash(id),
        disabled: !MATOMO_TRACKING_ENABLED,
        log: false,
    });

And my react-native-mock :

jest.mock('matomo-tracker-react-native', () => {
    return {
        default: jest.fn().mockImplementation(function () {
            return {
                initialize: jest.fn(),
                trackAppStart: jest.fn(),
                trackScreenView: jest.fn(),
                trackAction: jest.fn(),
                trackEvent: jest.fn(),
                trackSiteSearch: jest.fn(),
                trackLink: jest.fn(),
                trackDownload: jest.fn(),
                track: jest.fn(),
            };
        }),
        useMatomo: jest.fn().mockReturnValue({
            trackAppStart: jest.fn(),
            trackScreenView: jest.fn(),
        }),
        MatomoTracker: jest.fn().mockImplementation(function () {
            return {
                initialize: jest.fn(),
                trackAppStart: jest.fn(),
                trackScreenView: jest.fn(),
                trackAction: jest.fn(),
                trackEvent: jest.fn(),
                trackSiteSearch: jest.fn(),
                trackLink: jest.fn(),
                trackDownload: jest.fn(),
                track: jest.fn(),
            };
        }),
    };
});

I've tried both default / define MatomoTracker but I end up with the same error : TypeError: _matomoTrackerReactNative.default is not a constructor

If you have any idea about how to fix it Thanks