Skip to content

Commit

Permalink
chore: Updated Disclosure unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rijuma committed Dec 16, 2024
1 parent 055c6fa commit 4bd5522
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 182 deletions.
49 changes: 45 additions & 4 deletions src/components/Disclosure/Disclosure.test.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import React from 'react';
import { screen } from '@testing-library/react';
import { fireEvent, screen } from '@testing-library/react';
import { render } from '../../utils/utils.test';
import { useCourseUpgrade, useTrackEvent } from '../../hooks';

import TrialDisclosure from '.';

const mockedUpgradeUrl = 'https://upgrade.edx/course/test';
const mockedAuditTrialDays = 7;

jest.mock('../../hooks', () => ({
useCourseUpgrade: jest.fn(),
useTrackEvent: jest.fn(() => ({ track: jest.fn() })),
}));

const PRIVACY_POLICY_URL = 'https://some.url/policy';
jest.mock('@edx/frontend-platform/config', () => ({
ensureConfig: jest.fn(),
Expand All @@ -15,6 +24,7 @@ describe('<TrialDisclosure />', () => {

describe('When trial upgrade is not being shown', () => {
beforeEach(() => {
useCourseUpgrade.mockReturnValue({ upgradeable: false });
({ container } = render(<TrialDisclosure><span>Children</span></TrialDisclosure>));
});

Expand All @@ -26,25 +36,56 @@ describe('<TrialDisclosure />', () => {
});

it('should not show the trial message', () => {
const upgrade = screen.queryByText('Free trial, then upgrade course for full access to Xpert features.');
const upgrade = screen.queryByTestId('free-days-label');

expect(upgrade).not.toBeInTheDocument();
});

it('should not show the upgrade CTA', () => {
const upgradeCta = screen.queryByTestId('upgrade-cta');

expect(upgradeCta).not.toBeInTheDocument();
});

it('should match snapshot', () => {
expect(container).toMatchSnapshot();
});
});

describe('When trial upgrade being shown', () => {
const mockedTrackEvent = jest.fn();

beforeEach(() => {
useCourseUpgrade.mockReturnValue({
upgradeable: true,
upgradeUrl: mockedUpgradeUrl,
auditTrialLengthDays: mockedAuditTrialDays,
});
useTrackEvent.mockReturnValue({ track: mockedTrackEvent });
({ container } = render(<TrialDisclosure showTrial><span>Children</span></TrialDisclosure>));
});

it('should show the trial message', () => {
const upgrade = screen.queryByText('Free trial, then upgrade course for full access to Xpert features.');
const upgrade = screen.queryByTestId('free-days-label');

expect(upgrade.textContent).toBe(`Free for ${mockedAuditTrialDays} days, then upgrade course for full access to Xpert features.`);
});

it('should show the trial button with the proper href to upgrade', () => {
const upgradeCta = screen.queryByTestId('upgrade-cta');

expect(upgradeCta).toBeInTheDocument();
expect(upgradeCta).toHaveAttribute('href', mockedUpgradeUrl);
});

it('should call the track event on click', () => {
const upgradeCta = screen.queryByTestId('upgrade-cta');

expect(mockedTrackEvent).not.toHaveBeenCalled();

fireEvent.click(upgradeCta);

expect(upgrade).toBeInTheDocument();
expect(mockedTrackEvent).toHaveBeenCalledWith('edx.ui.lms.learning_assistant.message');
});

it('should match snapshot', () => {
Expand Down
Loading

0 comments on commit 4bd5522

Please sign in to comment.