Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed May 16, 2024
1 parent a30ebd4 commit 4ba2343
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,74 @@ const modalClose = jest.fn();
const alertOpen = jest.fn();
const alertClose = jest.fn();

const offsetDays = {
120: dayjs().add(120, 'day'),
90: dayjs().add(90, 'day'),
60: dayjs().add(60, 'day'),
30: dayjs().add(30, 'day'),
10: dayjs().add(10, 'day'),
1: dayjs().subtract(1, 'day'),
};

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

it.each([
(() => {
const endDate = dayjs().add(120, 'day');
return { endDate, expected: expiryThresholds[120]({ date: formatDate(endDate.toString()) }) };
})(),
(() => {
const endDate = dayjs().add(90, 'day');
return { endDate, expected: expiryThresholds[90]({ date: formatDate(endDate.toString()) }) };
})(),
(() => {
const endDate = dayjs().add(60, 'day');
return { endDate, expected: expiryThresholds[60]({ date: formatDate(endDate.toString()) }) };
})(),
(() => {
const endDate = dayjs().add(30, 'day');
return { endDate, expected: expiryThresholds[30]({ date: formatDate(endDate.toString()) }) };
})(),
(() => {
const endDate = dayjs().add(10, 'day');
const today = dayjs().add(1, 'minutes');
const durationDiff = dayjs.duration(endDate.diff(today));

return {
endDate,
expected: expiryThresholds[10]({
date: formatDate(endDate.toString()),
days: durationDiff.days(),
hours: durationDiff.hours(),
}),
};
})(),
(() => {
const endDate = dayjs().subtract(1, 'day');
return { endDate, expected: expiryThresholds[0]({ date: formatDate(endDate.toString()) }) };
})(),
])('displays correct notification and modal when plan is expiring in %s days', ({ endDate, expected }) => {
{
endDate: offsetDays['120'],
expected: expiryThresholds[120]({
date: formatDate(offsetDays['120'].toString()),
}),
isNonExpiredBudget: true,
},
{
endDate: offsetDays['90'],
expected: expiryThresholds[90]({
date: formatDate(offsetDays['90'].toString()),
}),
isNonExpiredBudget: true,
},
{
endDate: offsetDays['60'],
expected: expiryThresholds[60]({
date: formatDate(offsetDays['60'].toString()),
}),
isNonExpiredBudget: true,
},
{
endDate: offsetDays['30'],
expected: expiryThresholds[30]({
date: formatDate(offsetDays['30'].toString()),
}),
isNonExpiredBudget: true,
},
{
endDate: offsetDays['10'],
expected: expiryThresholds[10]({
date: formatDate(offsetDays['10'].toString()),
days: dayjs.duration(offsetDays['10'].diff(dayjs())).days(),
hours: dayjs.duration(offsetDays['10'].diff(dayjs())).hours(),
}),
isNonExpiredBudget: true,
},
{
endDate: offsetDays['1'],
expected: expiryThresholds[0]({
date: formatDate(offsetDays['1'].toString()),
}),
isNonExpiredBudget: false,
},
])('displays correct notification and modal when plan is expiring in %s days', ({ endDate, expected, isNonExpiredBudget }) => {
const budgets = [{ end: endDate }]; // Mock data with an expiring budget

const { result } = renderHook(() => useExpiry('enterpriseId', budgets, modalOpen, modalClose, alertOpen, alertClose));

expect(result.current.notification).toEqual(expected.notificationTemplate);
expect(result.current.modal).toEqual(expected.modalTemplate);
expect(result.current.status).toEqual(expected.status);
expect(result.current.isNonExpiredBudget).toEqual(isNonExpiredBudget);
});

it('displays no notification with both an expired and non-expired budget', () => {
Expand Down
1 change: 0 additions & 1 deletion src/components/BudgetExpiryAlertAndModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import useExpiry from './data/hooks/useExpiry';
const BudgetExpiryAlertAndModal = ({ enterpriseUUID, enterpriseFeatures, disableExpiryMessagingForLearnerCredit }) => {
const [modalIsOpen, modalOpen, modalClose] = useToggle(false);
const [alertIsOpen, alertOpen, alertClose] = useToggle(false);

const location = useLocation();

const budgetDetailRouteMatch = matchPath(
Expand Down
2 changes: 1 addition & 1 deletion src/data/reducers/portalConfiguration.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const portalConfiguration = (state = initialState, action) => {
enterpriseSlug: null,
enterpriseBranding: null,
identityProvider: null,
disableExpiryMessagingForLearnerCredit: null,
disableExpiryMessagingForLearnerCredit: false,
enableCodeManagementScreen: false,
enableReportingConfigScreen: false,
enableSubscriptionManagementScreen: false,
Expand Down
3 changes: 3 additions & 0 deletions src/data/reducers/portalConfiguration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const initialState = {
enterpriseSlug: null,
enterpriseBranding: null,
identityProvider: null,
disableExpiryMessagingForLearnerCredit: false,
enableCodeManagementScreen: false,
enableReportingConfigScreen: false,
enableSubscriptionManagementScreen: false,
Expand Down Expand Up @@ -43,6 +44,7 @@ const enterpriseData = {
identity_provider: {
uuid: 'test-identity-provider-uuid',
},
disable_expiry_messaging_for_learner_credit: true,
enable_portal_code_management_screen: true,
enable_portal_reporting_config_screen: true,
enable_portal_subscription_management_screen: true,
Expand Down Expand Up @@ -77,6 +79,7 @@ describe('portalConfiguration reducer', () => {
enterpriseSlug: enterpriseData.slug,
enterpriseBranding: enterpriseData.branding_configuration,
identityProvider: enterpriseData.identity_provider,
disableExpiryMessagingForLearnerCredit: enterpriseData.disable_expiry_messaging_for_learner_credit,
enableCodeManagementScreen: enterpriseData.enable_portal_code_management_screen,
enableReportingConfigScreen: enterpriseData.enable_portal_reporting_config_screen,
enableSubscriptionManagementScreen: enterpriseData.enable_portal_subscription_management_screen, // eslint-disable-line max-len
Expand Down

0 comments on commit 4ba2343

Please sign in to comment.