Skip to content

Commit

Permalink
test: completed working unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ilee2u committed Dec 16, 2024
1 parent 48a19d4 commit d366bc0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/components/Sidebar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ const Sidebar = ({
if (auditTrialDaysRemaining > 1) {
const irtl = new Intl.RelativeTimeFormat({ style: 'long' });
return (
<div data-testid="x-days-remaining-message">
<div data-testid="days-remaining-message">
Your trial ends {irtl.format(auditTrialDaysRemaining, 'day')}. <a target="_blank" href={upgradeUrl} rel="noreferrer">Upgrade</a> for full access to Xpert.
</div>
);
} if (auditTrialDaysRemaining === 1) {
return (
<div data-testid="trial-ends-today">
<div data-testid="trial-ends-today-message">
Your trial ends today! <a target="_blank" href={upgradeUrl} rel="noreferrer">Upgrade</a> for full access to Xpert.
</div>
);
Expand All @@ -109,7 +109,7 @@ const Sidebar = ({
</div>
{upgradeable
&& (
<div className="p-3 trial-header">
<div className="p-3 trial-header" data-testid="get-days-remaining-message">
{getDaysRemainingMessage()}
</div>
)}
Expand Down
31 changes: 15 additions & 16 deletions src/components/Sidebar/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import React from 'react';
import { screen, act } from '@testing-library/react';

import { usePromptExperimentDecision } from '../../experiments';
import { useCourseUpgrade } from '../../hooks';
import { useCourseUpgrade, useTrackEvent } from '../../hooks';
import { render as renderComponent } from '../../utils/utils.test';
import { initialState } from '../../data/slice';
import showSurvey from '../../utils/surveyMonkey';

import Sidebar from '.';

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

jest.mock('../../utils/surveyMonkey', () => jest.fn());

jest.mock('@edx/frontend-platform/analytics', () => ({
Expand All @@ -34,10 +39,6 @@ jest.mock('../../experiments', () => ({
usePromptExperimentDecision: jest.fn(),
}));

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

const defaultProps = {
courseId: 'some-course-id',
isOpen: true,
Expand Down Expand Up @@ -70,6 +71,8 @@ describe('<Sidebar />', () => {
jest.resetAllMocks();
usePromptExperimentDecision.mockReturnValue([]);
useCourseUpgrade.mockReturnValue([]);
const mockedTrackEvent = jest.fn();
useTrackEvent.mockReturnValue({ track: mockedTrackEvent });
});

describe('when it\'s open', () => {
Expand Down Expand Up @@ -97,9 +100,8 @@ describe('<Sidebar />', () => {
});
render(undefined, { disclosureAcknowledged: true });
expect(screen.queryByTestId('sidebar-xpert')).toBeInTheDocument();

const daysRemainingMessage = screen.queryByTestId('x-days-remaining-message');
expect(daysRemainingMessage).toBeInTheDocument();
expect(screen.queryByTestId('get-days-remaining-message')).toBeInTheDocument();
expect(screen.queryByTestId('days-remaining-message')).toBeInTheDocument();
});

it('If auditTrialDaysRemaining === 1, say final day', () => {
Expand All @@ -110,9 +112,8 @@ describe('<Sidebar />', () => {
});
render(undefined, { disclosureAcknowledged: true });
expect(screen.queryByTestId('sidebar-xpert')).toBeInTheDocument();

const trialEndsTodayMessage = screen.queryByTestId('trial-ends-today');
expect(trialEndsTodayMessage).toBeInTheDocument();
expect(screen.queryByTestId('get-days-remaining-message')).toBeInTheDocument();
expect(screen.queryByTestId('trial-ends-today')).not.toBeInTheDocument();
});

it('If auditTrialDaysRemaining < 1, do not show either of those', () => {
Expand All @@ -123,11 +124,9 @@ describe('<Sidebar />', () => {
});
render(undefined, { disclosureAcknowledged: true });
expect(screen.queryByTestId('sidebar-xpert')).toBeInTheDocument();

const daysRemainingMessage = screen.queryByTestId('x-days-remaining-message');
const trialEndsTodayMessage = screen.queryByTestId('trial-ends-today');
expect(daysRemainingMessage).not.toBeInTheDocument();
expect(trialEndsTodayMessage).not.toBeInTheDocument();
expect(screen.queryByTestId('get-days-remaining-message')).toBeInTheDocument();
expect(screen.queryByTestId('days-remaining-message')).not.toBeInTheDocument();
expect(screen.queryByTestId('trial-ends-today')).not.toBeInTheDocument();
});
});

Expand Down
12 changes: 12 additions & 0 deletions src/setupTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ jest.mock('@src/generic/model-store', () => ({ useModel: jest.fn() }), { virtual
mergeConfig({
...process.env,
});

// const mockModelStore = {};
// jest.mock(
// '@src/generic/model-store',
// () => ({
// useModel: jest.fn((type) => mockModelStore[type]),
// setModel: jest.fn((type, data) => {
// mockModelStore[type] = data;
// }),
// }),
// { virtual: true },
// );

0 comments on commit d366bc0

Please sign in to comment.