Skip to content

Commit

Permalink
Merge pull request #52 from edx/ahtesham/unit-tests-track-events
Browse files Browse the repository at this point in the history
feat: add segment events unit tests
  • Loading branch information
ahtesham-quraish authored Jun 10, 2024
2 parents 89e0332 + 0f5bf85 commit c8d732b
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 27 deletions.
6 changes: 0 additions & 6 deletions src/forms/login-popup/tests/LoginPopup.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ const mockStore = configureStore();
// mocking getAllPossibleQueryParams
jest.mock('../../../data/utils', () => jest.fn());

// Mocking the sendTrackEvent
jest.mock('@edx/frontend-platform/analytics', () => ({
sendTrackEvent: jest.fn(),
sendPageEvent: jest.fn(),
}));

// Mocking the trackForgotPasswordLinkClick function
jest.mock('../../../tracking/trackers/login', () => ({
...jest.requireActual('../../../tracking/trackers/login'),
Expand Down
5 changes: 0 additions & 5 deletions src/forms/progressive-profiling-popup/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ import ProgressiveProfilingForm from './index';
const IntlProgressiveProfilingForm = injectIntl(ProgressiveProfilingForm);
const mockStore = configureStore();

jest.mock('@edx/frontend-platform/analytics', () => ({
sendTrackEvent: jest.fn(),
sendPageEvent: jest.fn(),
}));

jest.mock('@edx/frontend-platform/auth', () => ({
configure: jest.fn(),
getAuthenticatedUser: jest.fn({}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ import SSOFailureAlert from '../../../common-components/SSOFailureAlert';
import RegistrationPage from '../../index';
import RegistrationFailureAlert from '../RegistrationFailureAlert';

jest.mock('@edx/frontend-platform/analytics', () => ({
sendTrackEvent: jest.fn(),
sendPageEvent: jest.fn(),
}));

jest.mock('@edx/frontend-platform/i18n', () => ({
...jest.requireActual('@edx/frontend-platform/i18n'),
getLocale: jest.fn(),
Expand Down
5 changes: 0 additions & 5 deletions src/forms/registration-popup/tests/RegistrationPopup.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import { clearRegistrationBackendError, registerUser, setUserPipelineDataLoaded
import * as utils from '../data/utils';
import RegistrationForm from '../index';

jest.mock('@edx/frontend-platform/analytics', () => ({
sendTrackEvent: jest.fn(),
sendPageEvent: jest.fn(),
}));

const IntlRegistrationForm = injectIntl(RegistrationForm);
const mockStore = configureStore();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import { loginErrorClear } from '../../../login-popup/data/reducers';
import { forgotPassword, forgotPasswordClearStatus } from '../data/reducers';
import ForgotPasswordPage from '../index';

jest.mock('@edx/frontend-platform/analytics', () => ({
sendTrackEvent: jest.fn(),
sendPageEvent: jest.fn(),
}));

const IntlForgotPasswordPage = injectIntl(ForgotPasswordPage);
const mockStore = configureStore();

Expand Down
4 changes: 4 additions & 0 deletions src/setupTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class MockLoggingService {

logError = jest.fn();
}
jest.mock('@edx/frontend-platform/analytics', () => ({
sendTrackEvent: jest.fn(),
sendPageEvent: jest.fn(),
}));

export default function initializeMockLogging() {
const loggingService = configureLogging(MockLoggingService, {
Expand Down
2 changes: 1 addition & 1 deletion src/tracking/trackers/progressive-profiling.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const eventNames = {
loginAndRegistration: 'login_and_registration',
};

// Event tracker for successful registration
// Event tracker for Progressive profiling skip button click
export const trackProgressiveProfilingSkipLinkClickEvent = () => createEventTracker(
eventNames.ProgressiveProfilingSkipLinkClick,
{},
Expand Down
37 changes: 37 additions & 0 deletions src/tracking/trackers/tests/forgot-password.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createEventTracker, createPageEventTracker } from '../../../data/segment/utils';
import {
categories,
eventNames,
forgotPasswordPageViewedEvent,
trackForgotPasswordPageEvent,
} from '../forgotpassword';

// Mock createEventTracker function
jest.mock('../../../data/segment/utils', () => ({
createEventTracker: jest.fn().mockImplementation(() => jest.fn()),
createPageEventTracker: jest.fn().mockImplementation(() => jest.fn()),
}));

describe('Tracking Functions', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should fire trackForgotPasswordPageEvent', () => {
trackForgotPasswordPageEvent();

expect(createPageEventTracker).toHaveBeenCalledWith(
eventNames.loginAndRegistration,
'forgot-password',
);
});

it('should fire forgotPasswordPageViewedEvent', () => {
forgotPasswordPageViewedEvent();

expect(createEventTracker).toHaveBeenCalledWith(
eventNames.forgotPasswordPageViewd,
{ category: categories.userEngagement },
);
});
});
36 changes: 36 additions & 0 deletions src/tracking/trackers/tests/progressive-profiling.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createEventTracker, createPageEventTracker } from '../../../data/segment/utils';
import {
eventNames,
trackProgressiveProfilingPageEvent,
trackProgressiveProfilingSkipLinkClickEvent,
} from '../progressive-profiling';

// Mock createEventTracker function
jest.mock('../../../data/segment/utils', () => ({
createEventTracker: jest.fn().mockImplementation(() => jest.fn()),
createPageEventTracker: jest.fn().mockImplementation(() => jest.fn()),
}));

describe('Tracking Functions', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should fire trackProgressiveProfilingSkipLinkClickEvent', () => {
trackProgressiveProfilingSkipLinkClickEvent();

expect(createEventTracker).toHaveBeenCalledWith(
eventNames.ProgressiveProfilingSkipLinkClick,
{},
);
});

it('should fire trackProgressiveProfilingPageEvent', () => {
trackProgressiveProfilingPageEvent();

expect(createPageEventTracker).toHaveBeenCalledWith(
eventNames.loginAndRegistration,
'welcome',
);
});
});
36 changes: 36 additions & 0 deletions src/tracking/trackers/tests/register.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createEventTracker, createPageEventTracker } from '../../../data/segment/utils';
import {
eventNames,
registrationSuccessEvent,
trackRegistrationPageEvent,
} from '../register';

// Mock createEventTracker function
jest.mock('../../../data/segment/utils', () => ({
createEventTracker: jest.fn().mockImplementation(() => jest.fn()),
createPageEventTracker: jest.fn().mockImplementation(() => jest.fn()),
}));

describe('Tracking Functions', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should fire registrationSuccessEvent', () => {
registrationSuccessEvent();

expect(createEventTracker).toHaveBeenCalledWith(
eventNames.RegistrationSuccess,
{},
);
});

it('should fire trackRegistrationPageEvent', () => {
trackRegistrationPageEvent();

expect(createPageEventTracker).toHaveBeenCalledWith(
eventNames.loginAndRegistration,
'register',
);
});
});
26 changes: 26 additions & 0 deletions src/tracking/trackers/tests/reset-password.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createPageEventTracker } from '../../../data/segment/utils';
import {
eventNames,
trackResettPasswordPageEvent,
} from '../reset-password';

// Mock createEventTracker function
jest.mock('../../../data/segment/utils', () => ({
createEventTracker: jest.fn().mockImplementation(() => jest.fn()),
createPageEventTracker: jest.fn().mockImplementation(() => jest.fn()),
}));

describe('Tracking Functions', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should fire trackResettPasswordPageEvent', () => {
trackResettPasswordPageEvent();

expect(createPageEventTracker).toHaveBeenCalledWith(
eventNames.loginAndRegistration,
'reset-password',
);
});
});

0 comments on commit c8d732b

Please sign in to comment.