Skip to content

Commit

Permalink
temp: attempting to test Sidebar
Browse files Browse the repository at this point in the history
- getting a strange error where hooks aren't being picked up...
  • Loading branch information
ilee2u committed Dec 13, 2024
1 parent f9809f1 commit a852ef7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@edx/brand": "npm:@edx/[email protected]",
"@optimizely/react-sdk": "^2.9.2",
"react-markdown": "^8.0.5",
"rosie": "^2.1.1",
"uuid": "9.0.0"
},
"peerDependencies": {
Expand Down
10 changes: 4 additions & 6 deletions src/components/Sidebar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ const Sidebar = ({
isOpen,
setIsOpen,
unitId,
isUpgradeEligible,
}) => {
const {
apiError,
disclosureAcknowledged,
messageList,
} = useSelector(state => state.learningAssistant);

const { upgradeUrl, auditTrialDaysRemaining } = useCourseUpgrade();
const { upgradeable, upgradeUrl, auditTrialDaysRemaining } = useCourseUpgrade();

const chatboxContainerRef = useRef(null);

Expand Down Expand Up @@ -85,13 +84,13 @@ const Sidebar = ({
if (auditTrialDaysRemaining > 1) {
const irtl = new Intl.RelativeTimeFormat({ style: 'long' });
return (
<div>
<div data-testid='x-days-remaining-message'>

Check failure on line 87 in src/components/Sidebar/index.jsx

View workflow job for this annotation

GitHub Actions / test

Unexpected usage of singlequote
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>
<div data-testid='trial-ends-today'>

Check failure on line 93 in src/components/Sidebar/index.jsx

View workflow job for this annotation

GitHub Actions / test

Unexpected usage of singlequote
Your trial ends today! <a target="_blank" href={upgradeUrl} rel="noreferrer">Upgrade</a> for full access to Xpert.
</div>
);
Expand All @@ -109,7 +108,7 @@ const Sidebar = ({
<div className="p-3 sidebar-header" data-testid="sidebar-xpert-header">
<XpertLogo />
</div>
{isUpgradeEligible
{upgradeable
&& (
<div className="p-3 trial-header">
{getDaysRemainingMessage()}
Expand Down Expand Up @@ -162,7 +161,6 @@ Sidebar.propTypes = {
isOpen: PropTypes.bool.isRequired,
setIsOpen: PropTypes.func.isRequired,
unitId: PropTypes.string.isRequired,
isUpgradeEligible: PropTypes.bool.isRequired,
};

export default Sidebar;
54 changes: 48 additions & 6 deletions src/components/Sidebar/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React from 'react';
import { screen, act } from '@testing-library/react';

import { Factory } from 'rosie';
import { setModel } from '@src/generic/model-store'; // eslint-disable-line import/no-unresolved

import { usePromptExperimentDecision } from '../../experiments';
import { useCourseUpgrade } from '../../hooks';
import { render as renderComponent } from '../../utils/utils.test';
import { initialState } from '../../data/slice';
import showSurvey from '../../utils/surveyMonkey';
Expand Down Expand Up @@ -36,6 +34,10 @@ jest.mock('../../experiments', () => ({
usePromptExperimentDecision: jest.fn(),
}));

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

const defaultProps = {
courseId: 'some-course-id',
isOpen: true,
Expand Down Expand Up @@ -65,11 +67,9 @@ const render = async (props = {}, sliceState = {}) => {

describe('<Sidebar />', () => {
beforeEach(async () => {
setModel('courseHomeMeta', Factory.build('courseHomeMeta'));
setModel('coursewareMeta', Factory.build('coursewareMeta'));

jest.resetAllMocks();
usePromptExperimentDecision.mockReturnValue([]);
useCourseUpgrade.mockReturnValue([]);
});

describe('when it\'s open', () => {
Expand All @@ -87,6 +87,48 @@ describe('<Sidebar />', () => {
render(undefined, { disclosureAcknowledged: true });
expect(screen.queryByTestId('sidebar-xpert')).toBeInTheDocument();
});

// TODO: Write out these tests.
it('If auditTrialDaysRemaining > 1, show days remaining', () => {
useCourseUpgrade.mockReturnValue({
upgradeable: true,
upgradeUrl: 'https://mockurl.com',
auditTrialDaysRemaining: 2,
});
render(undefined, { disclosureAcknowledged: true });
expect(screen.queryByTestId('sidebar-xpert')).toBeInTheDocument();

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

it('If auditTrialDaysRemaining === 1, say final day', () => {
useCourseUpgrade.mockReturnValue({
upgradeable: true,
upgradeUrl: 'https://mockurl.com',
auditTrialDaysRemaining: 1,
});
render(undefined, { disclosureAcknowledged: true });
expect(screen.queryByTestId('sidebar-xpert')).toBeInTheDocument();

const trialEndsTodayMessage = screen.queryByTestId('trial-ends-today');
expect(trialEndsTodayMessage).toBeInTheDocument();
});

it('If auditTrialDaysRemaining < 1, do not show either of those', () => {
useCourseUpgrade.mockReturnValue({
upgradeable: true,
upgradeUrl: 'https://mockurl.com',
auditTrialDaysRemaining: 0,
});
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();
});
});

describe('when it\'s not open', () => {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/use-course-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function useCourseUpgrade() {
auditTrialDaysRemaining = Math.ceil((auditTrialExpirationDate - Date.now()) / millisecondsInOneDay);

auditTrialExpired = auditTrialDaysRemaining < 0;
console.log({ upgradeUrl }, { auditTrialDaysRemaining });

Check warning on line 28 in src/hooks/use-course-upgrade.js

View workflow job for this annotation

GitHub Actions / test

Unexpected console statement
}

return {
Expand Down

0 comments on commit a852ef7

Please sign in to comment.