From a852ef7acda0e7f4795c2f79d457c8611b0d428f Mon Sep 17 00:00:00 2001 From: ilee2u Date: Fri, 13 Dec 2024 15:48:24 -0500 Subject: [PATCH] temp: attempting to test Sidebar - getting a strange error where hooks aren't being picked up... --- package-lock.json | 9 ----- package.json | 1 - src/components/Sidebar/index.jsx | 10 ++--- src/components/Sidebar/index.test.jsx | 54 ++++++++++++++++++++++++--- src/hooks/use-course-upgrade.js | 1 + 5 files changed, 53 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index d4257a77..5b60c440 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "@edx/brand": "npm:@edx/brand-openedx@1.2.0", "@optimizely/react-sdk": "^2.9.2", "react-markdown": "^8.0.5", - "rosie": "^2.1.1", "uuid": "9.0.0" }, "devDependencies": { @@ -21823,14 +21822,6 @@ "rimraf": "bin.js" } }, - "node_modules/rosie": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/rosie/-/rosie-2.1.1.tgz", - "integrity": "sha512-2AXB7WrIZXtKMZ6Q/PlozqPF5nu/x7NEvRJZOblrJuprrPfm5gL8JVvJPj9aaib9F8IUALnLUFhzXrwEtnI5cQ==", - "engines": { - "node": ">=10" - } - }, "node_modules/rtlcss": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/rtlcss/-/rtlcss-4.1.1.tgz", diff --git a/package.json b/package.json index 7542a8ef..d7af8941 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,6 @@ "@edx/brand": "npm:@edx/brand-openedx@1.2.0", "@optimizely/react-sdk": "^2.9.2", "react-markdown": "^8.0.5", - "rosie": "^2.1.1", "uuid": "9.0.0" }, "peerDependencies": { diff --git a/src/components/Sidebar/index.jsx b/src/components/Sidebar/index.jsx index 0487cf25..48dd87e5 100644 --- a/src/components/Sidebar/index.jsx +++ b/src/components/Sidebar/index.jsx @@ -23,7 +23,6 @@ const Sidebar = ({ isOpen, setIsOpen, unitId, - isUpgradeEligible, }) => { const { apiError, @@ -31,7 +30,7 @@ const Sidebar = ({ messageList, } = useSelector(state => state.learningAssistant); - const { upgradeUrl, auditTrialDaysRemaining } = useCourseUpgrade(); + const { upgradeable, upgradeUrl, auditTrialDaysRemaining } = useCourseUpgrade(); const chatboxContainerRef = useRef(null); @@ -85,13 +84,13 @@ const Sidebar = ({ if (auditTrialDaysRemaining > 1) { const irtl = new Intl.RelativeTimeFormat({ style: 'long' }); return ( -
+
Your trial ends {irtl.format(auditTrialDaysRemaining, 'day')}. Upgrade for full access to Xpert.
); } if (auditTrialDaysRemaining === 1) { return ( -
+
Your trial ends today! Upgrade for full access to Xpert.
); @@ -109,7 +108,7 @@ const Sidebar = ({
- {isUpgradeEligible + {upgradeable && (
{getDaysRemainingMessage()} @@ -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; diff --git a/src/components/Sidebar/index.test.jsx b/src/components/Sidebar/index.test.jsx index f7a0cb04..8d18a4ad 100644 --- a/src/components/Sidebar/index.test.jsx +++ b/src/components/Sidebar/index.test.jsx @@ -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'; @@ -36,6 +34,10 @@ jest.mock('../../experiments', () => ({ usePromptExperimentDecision: jest.fn(), })); +jest.mock('../../hooks', () => ({ + useCourseUpgrade: jest.fn(), +})); + const defaultProps = { courseId: 'some-course-id', isOpen: true, @@ -65,11 +67,9 @@ const render = async (props = {}, sliceState = {}) => { describe('', () => { beforeEach(async () => { - setModel('courseHomeMeta', Factory.build('courseHomeMeta')); - setModel('coursewareMeta', Factory.build('coursewareMeta')); - jest.resetAllMocks(); usePromptExperimentDecision.mockReturnValue([]); + useCourseUpgrade.mockReturnValue([]); }); describe('when it\'s open', () => { @@ -87,6 +87,48 @@ describe('', () => { 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', () => { diff --git a/src/hooks/use-course-upgrade.js b/src/hooks/use-course-upgrade.js index b78cbe4a..1c1a2b57 100644 --- a/src/hooks/use-course-upgrade.js +++ b/src/hooks/use-course-upgrade.js @@ -25,6 +25,7 @@ export default function useCourseUpgrade() { auditTrialDaysRemaining = Math.ceil((auditTrialExpirationDate - Date.now()) / millisecondsInOneDay); auditTrialExpired = auditTrialDaysRemaining < 0; + console.log({ upgradeUrl }, { auditTrialDaysRemaining }); } return {