richie-education 2.29.2 → 2.29.3-dev48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/i18n/locales/ar-SA.json +193 -125
  2. package/i18n/locales/es-ES.json +193 -125
  3. package/i18n/locales/fa-IR.json +193 -125
  4. package/i18n/locales/fr-CA.json +193 -125
  5. package/i18n/locales/fr-FR.json +193 -125
  6. package/i18n/locales/ko-KR.json +193 -125
  7. package/i18n/locales/pt-PT.json +193 -125
  8. package/i18n/locales/ru-RU.json +193 -125
  9. package/i18n/locales/vi-VN.json +193 -125
  10. package/js/api/joanie.ts +42 -17
  11. package/js/api/lms/dummy.ts +1 -12
  12. package/js/components/ContractFrame/AbstractContractFrame.spec.tsx +16 -9
  13. package/js/components/ContractFrame/AbstractContractFrame.tsx +28 -23
  14. package/js/components/ContractFrame/LearnerContractFrame.tsx +2 -2
  15. package/js/components/ContractFrame/_styles.scss +6 -14
  16. package/js/components/ContractStatus/index.spec.tsx +1 -1
  17. package/js/components/ContractStatus/index.tsx +1 -1
  18. package/js/components/{SaleTunnel/CreditCardSelector → CreditCardSelector}/index.spec.tsx +15 -45
  19. package/js/components/{SaleTunnel/CreditCardSelector → CreditCardSelector}/index.tsx +19 -26
  20. package/js/components/DownloadContractButton/index.spec.tsx +1 -1
  21. package/js/components/Form/utils.ts +2 -1
  22. package/js/components/OpenEdxFullNameForm/index.spec.tsx +229 -0
  23. package/js/components/OpenEdxFullNameForm/index.tsx +7 -7
  24. package/js/components/PaymentInterfaces/LyraPopIn.tsx +2 -2
  25. package/js/components/PaymentInterfaces/PayplugLightbox.tsx +1 -1
  26. package/js/components/PaymentInterfaces/__mocks__/index.tsx +1 -4
  27. package/js/components/PaymentInterfaces/types.ts +5 -2
  28. package/js/components/PaymentScheduleGrid/_styles.scss +13 -0
  29. package/js/components/PaymentScheduleGrid/index.tsx +50 -70
  30. package/js/components/PurchaseButton/index.spec.tsx +84 -37
  31. package/js/components/PurchaseButton/index.tsx +9 -27
  32. package/js/components/SaleTunnel/AddressSelector/index.spec.tsx +2 -2
  33. package/js/components/SaleTunnel/CertificateSaleTunnel/index.tsx +2 -2
  34. package/js/components/SaleTunnel/CredentialSaleTunnel/index.tsx +6 -10
  35. package/js/components/SaleTunnel/GenericSaleTunnel.tsx +105 -48
  36. package/js/components/SaleTunnel/SaleTunnelInformation/index.tsx +16 -20
  37. package/js/components/SaleTunnel/SaleTunnelSavePaymentMethod/_styles.scss +12 -0
  38. package/js/components/SaleTunnel/SaleTunnelSavePaymentMethod/index.tsx +157 -0
  39. package/js/components/SaleTunnel/SaleTunnelSuccess/index.tsx +15 -29
  40. package/js/components/SaleTunnel/Sponsors/SaleTunnelSponsors.scss +4 -5
  41. package/js/components/SaleTunnel/Sponsors/SaleTunnelSponsors.tsx +39 -11
  42. package/js/components/SaleTunnel/SubscriptionButton/_styles.scss +7 -0
  43. package/js/components/SaleTunnel/SubscriptionButton/index.tsx +202 -0
  44. package/js/components/SaleTunnel/_styles.scss +16 -5
  45. package/js/components/SaleTunnel/hooks/useTerms.tsx +0 -77
  46. package/js/components/SaleTunnel/index.credential.spec.tsx +14 -25
  47. package/js/components/SaleTunnel/index.full-process.spec.tsx +114 -48
  48. package/js/components/SaleTunnel/index.spec.tsx +333 -717
  49. package/js/components/SaleTunnel/index.tsx +2 -1
  50. package/js/components/SignContractButton/index.omniscientOrders.spec.tsx +16 -11
  51. package/js/components/SignContractButton/index.spec.tsx +16 -20
  52. package/js/components/SignContractButton/index.tsx +3 -1
  53. package/js/hooks/useCreditCards/index.spec.tsx +70 -6
  54. package/js/hooks/useCreditCards/index.ts +49 -11
  55. package/js/hooks/useOrders/index.spec.tsx +322 -0
  56. package/js/hooks/{useOrders.ts → useOrders/index.ts} +40 -14
  57. package/js/hooks/usePaymentSchedule.tsx +23 -0
  58. package/js/hooks/useProductOrder/index.spec.tsx +77 -60
  59. package/js/hooks/useProductOrder/index.tsx +2 -2
  60. package/js/hooks/useResources/useResourcesRoot.ts +4 -3
  61. package/js/index.tsx +2 -0
  62. package/js/pages/DashboardCreditCardsManagement/CreditCardBrandLogo.spec.tsx +1 -1
  63. package/js/pages/DashboardCreditCardsManagement/CreditCardBrandLogo.tsx +4 -2
  64. package/js/pages/DashboardOrderLayout/index.spec.tsx +10 -1
  65. package/js/pages/TeacherDashboardContractsLayout/components/ContractActionsBar/index.spec.tsx +8 -5
  66. package/js/pages/TeacherDashboardContractsLayout/components/SignOrganizationContractButton/index.spec.tsx +8 -9
  67. package/js/pages/TeacherDashboardCourseLearnersLayout/components/CourseLearnerDataGrid/index.spec.tsx +1 -1
  68. package/js/pages/TeacherDashboardCourseLearnersLayout/components/CourseLearnerDataGrid/index.tsx +1 -6
  69. package/js/settings/settings.prod.ts +0 -2
  70. package/js/settings/settings.test.ts +16 -2
  71. package/js/translations/ar-SA.json +1 -1
  72. package/js/translations/es-ES.json +1 -1
  73. package/js/translations/fa-IR.json +1 -1
  74. package/js/translations/fr-CA.json +1 -1
  75. package/js/translations/fr-FR.json +1 -1
  76. package/js/translations/ko-KR.json +1 -1
  77. package/js/translations/pt-PT.json +1 -1
  78. package/js/translations/ru-RU.json +1 -1
  79. package/js/translations/vi-VN.json +1 -1
  80. package/js/types/Joanie.ts +81 -32
  81. package/js/utils/OrderHelper/index.ts +46 -38
  82. package/js/utils/PaymentScheduleHelper/index.spec.tsx +78 -0
  83. package/js/utils/PaymentScheduleHelper/index.ts +30 -0
  84. package/js/utils/ProductHelper/index.spec.ts +322 -166
  85. package/js/utils/ProductHelper/index.ts +32 -0
  86. package/js/utils/test/factories/joanie.ts +72 -69
  87. package/js/widgets/Dashboard/components/DashboardItem/Contract/index.spec.tsx +2 -2
  88. package/js/widgets/Dashboard/components/DashboardItem/CourseEnrolling/index.tsx +8 -18
  89. package/js/widgets/Dashboard/components/DashboardItem/Enrollment/DashboardItemEnrollment.spec.tsx +23 -0
  90. package/js/widgets/Dashboard/components/DashboardItem/Enrollment/DashboardItemEnrollment.tsx +23 -1
  91. package/js/widgets/Dashboard/components/DashboardItem/Enrollment/ProductCertificateFooter/index.spec.tsx +145 -34
  92. package/js/widgets/Dashboard/components/DashboardItem/Enrollment/ProductCertificateFooter/index.tsx +175 -37
  93. package/js/widgets/Dashboard/components/DashboardItem/Order/CertificateItem/index.tsx +51 -0
  94. package/js/widgets/Dashboard/components/DashboardItem/Order/ContractItem/index.tsx +52 -0
  95. package/js/widgets/Dashboard/components/DashboardItem/Order/DashboardItemNotResumable.spec.tsx +109 -0
  96. package/js/widgets/Dashboard/components/DashboardItem/Order/DashboardItemOrder.spec.tsx +219 -5
  97. package/js/widgets/Dashboard/components/DashboardItem/Order/DashboardItemOrder.tsx +111 -245
  98. package/js/widgets/Dashboard/components/DashboardItem/Order/DashboardItemOrderContract.spec.tsx +61 -8
  99. package/js/widgets/Dashboard/components/DashboardItem/Order/DashboardItemOrderContract.useUnionResource.cache.spec.tsx +17 -7
  100. package/js/widgets/Dashboard/components/DashboardItem/Order/DashboardItemSavePaymentMethod.spec.tsx +116 -0
  101. package/js/widgets/Dashboard/components/DashboardItem/Order/Installment/index.tsx +175 -0
  102. package/js/widgets/Dashboard/components/DashboardItem/Order/OrderPaymentDetailsModal/_styles.scss +7 -0
  103. package/js/widgets/Dashboard/components/DashboardItem/Order/OrderPaymentDetailsModal/index.tsx +127 -0
  104. package/js/widgets/Dashboard/components/DashboardItem/Order/OrderPaymentRetryModal/index.tsx +216 -0
  105. package/js/widgets/Dashboard/components/DashboardItem/Order/OrderStateLearnerMessage/index.spec.tsx +18 -71
  106. package/js/widgets/Dashboard/components/DashboardItem/Order/OrderStateLearnerMessage/index.tsx +40 -25
  107. package/js/widgets/Dashboard/components/DashboardItem/Order/OrderStateMessage/index.tsx +28 -22
  108. package/js/widgets/Dashboard/components/DashboardItem/Order/OrderStateTeacherMessage/index.spec.tsx +18 -73
  109. package/js/widgets/Dashboard/components/DashboardItem/Order/OrderStateTeacherMessage/index.tsx +32 -16
  110. package/js/widgets/Dashboard/components/DashboardItem/Order/OrganizationBlock/index.tsx +150 -0
  111. package/js/widgets/Dashboard/components/DashboardOrderLoader/index.tsx +32 -14
  112. package/js/widgets/Dashboard/components/Signature/SignatureDummy.tsx +25 -3
  113. package/js/widgets/SyllabusCourseRunsList/components/CourseProductItem/components/CourseProductCourseRuns/EnrollableCourseRunList.tsx +2 -6
  114. package/js/widgets/SyllabusCourseRunsList/components/CourseProductItem/components/CourseProductCourseRuns/index.spec.tsx +7 -14
  115. package/js/widgets/SyllabusCourseRunsList/components/CourseProductItem/components/CourseRunItem/index.spec.tsx +7 -5
  116. package/js/widgets/SyllabusCourseRunsList/components/CourseProductItem/components/CourseRunItem/index.tsx +5 -7
  117. package/js/widgets/SyllabusCourseRunsList/components/CourseProductItem/index.spec.tsx +242 -332
  118. package/js/widgets/SyllabusCourseRunsList/components/CourseProductItem/index.stories.tsx +12 -13
  119. package/js/widgets/SyllabusCourseRunsList/components/CourseProductItem/index.tsx +10 -21
  120. package/js/widgets/SyllabusCourseRunsList/components/CourseRunEnrollment/index.joanie.spec.tsx +2 -2
  121. package/package.json +45 -44
  122. package/scss/components/_index.scss +4 -2
  123. package/js/components/PaymentButton/_styles.scss +0 -27
  124. package/js/components/SaleTunnel/GenericPaymentButton/index.tsx +0 -340
  125. package/js/components/SaleTunnel/SaleTunnelNotValidated/index.tsx +0 -70
  126. package/js/widgets/SyllabusCourseRunsList/components/CourseProductItem/components/ProductSignatureHeader/index.tsx +0 -41
  127. /package/js/components/{SaleTunnel/CreditCardSelector → CreditCardSelector}/_styles.scss +0 -0
@@ -1,183 +1,339 @@
1
1
  import { createIntl } from 'react-intl';
2
- import { CourseRunFactory, ProductFactory, TargetCourseFactory } from 'utils/test/factories/joanie';
2
+ import {
3
+ CertificateProductFactory,
4
+ CourseRunFactory,
5
+ CredentialProductFactory,
6
+ EnrollmentFactory,
7
+ ProductFactory,
8
+ TargetCourseFactory,
9
+ } from 'utils/test/factories/joanie';
10
+ import { CourseStateFactory } from 'utils/test/factories/richie';
11
+ import { Priority } from 'types';
3
12
  import { ProductHelper } from '.';
4
13
 
5
14
  describe('ProductHelper', () => {
6
- it('should return the product date range processed from course runs dates', () => {
7
- const product = ProductFactory({
8
- target_courses: [
9
- TargetCourseFactory({
10
- course_runs: [
11
- CourseRunFactory({
12
- start: new Date('2023-08-13').toISOString(),
13
- end: new Date('2024-04-27').toISOString(),
14
- }).one(),
15
- CourseRunFactory({
16
- start: new Date('2023-09-17').toISOString(),
17
- end: new Date('2024-05-29').toISOString(),
18
- }).one(),
19
- ],
20
- }).one(),
21
- TargetCourseFactory({
22
- course_runs: [
23
- CourseRunFactory({
24
- start: new Date('2024-01-19').toISOString(),
25
- end: new Date('2025-02-17').toISOString(),
26
- }).one(),
27
- ],
28
- }).one(),
29
- ],
30
- }).one();
31
-
32
- expect(ProductHelper.getDateRange(product)).toEqual([
33
- new Date('2023-08-13'),
34
- new Date('2025-02-17'),
35
- ]);
15
+ beforeEach(() => {
16
+ jest.resetAllMocks();
36
17
  });
37
18
 
38
- it('should return undefined minDate when there is a course run with an undefined start date', () => {
39
- const product = ProductFactory({
40
- target_courses: [
41
- TargetCourseFactory({
42
- course_runs: [
43
- CourseRunFactory({
44
- start: undefined,
45
- end: new Date('2024-04-27').toISOString(),
46
- }).one(),
47
- CourseRunFactory({
48
- start: new Date('2023-09-17').toISOString(),
49
- end: new Date('2024-05-29').toISOString(),
50
- }).one(),
51
- ],
52
- }).one(),
53
- TargetCourseFactory({
54
- course_runs: [
55
- CourseRunFactory({
56
- start: new Date('2024-01-19').toISOString(),
57
- end: new Date('2025-02-17').toISOString(),
58
- }).one(),
59
- ],
60
- }).one(),
61
- ],
62
- }).one();
63
-
64
- expect(ProductHelper.getDateRange(product)).toEqual([undefined, new Date('2025-02-17')]);
65
- });
19
+ describe('getDateRange', () => {
20
+ it('should return the product date range processed from course runs dates', () => {
21
+ const product = ProductFactory({
22
+ target_courses: [
23
+ TargetCourseFactory({
24
+ course_runs: [
25
+ CourseRunFactory({
26
+ start: new Date('2023-08-13').toISOString(),
27
+ end: new Date('2024-04-27').toISOString(),
28
+ }).one(),
29
+ CourseRunFactory({
30
+ start: new Date('2023-09-17').toISOString(),
31
+ end: new Date('2024-05-29').toISOString(),
32
+ }).one(),
33
+ ],
34
+ }).one(),
35
+ TargetCourseFactory({
36
+ course_runs: [
37
+ CourseRunFactory({
38
+ start: new Date('2024-01-19').toISOString(),
39
+ end: new Date('2025-02-17').toISOString(),
40
+ }).one(),
41
+ ],
42
+ }).one(),
43
+ ],
44
+ }).one();
45
+
46
+ expect(ProductHelper.getDateRange(product)).toEqual([
47
+ new Date('2023-08-13'),
48
+ new Date('2025-02-17'),
49
+ ]);
50
+ });
51
+
52
+ it('should return undefined minDate when there is a course run with an undefined start date', () => {
53
+ const product = ProductFactory({
54
+ target_courses: [
55
+ TargetCourseFactory({
56
+ course_runs: [
57
+ CourseRunFactory({
58
+ start: undefined,
59
+ end: new Date('2024-04-27').toISOString(),
60
+ }).one(),
61
+ CourseRunFactory({
62
+ start: new Date('2023-09-17').toISOString(),
63
+ end: new Date('2024-05-29').toISOString(),
64
+ }).one(),
65
+ ],
66
+ }).one(),
67
+ TargetCourseFactory({
68
+ course_runs: [
69
+ CourseRunFactory({
70
+ start: new Date('2024-01-19').toISOString(),
71
+ end: new Date('2025-02-17').toISOString(),
72
+ }).one(),
73
+ ],
74
+ }).one(),
75
+ ],
76
+ }).one();
77
+
78
+ expect(ProductHelper.getDateRange(product)).toEqual([undefined, new Date('2025-02-17')]);
79
+ });
80
+
81
+ it('should return undefined maxDate when there is a course run with an undefined end date', () => {
82
+ const product = ProductFactory({
83
+ target_courses: [
84
+ TargetCourseFactory({
85
+ course_runs: [
86
+ CourseRunFactory({
87
+ start: new Date('2023-08-13').toISOString(),
88
+ end: undefined,
89
+ }).one(),
90
+ CourseRunFactory({
91
+ start: new Date('2023-09-17').toISOString(),
92
+ end: new Date('2024-05-29').toISOString(),
93
+ }).one(),
94
+ ],
95
+ }).one(),
96
+ TargetCourseFactory({
97
+ course_runs: [
98
+ CourseRunFactory({
99
+ start: new Date('2024-01-19').toISOString(),
100
+ end: new Date('2025-02-17').toISOString(),
101
+ }).one(),
102
+ ],
103
+ }).one(),
104
+ ],
105
+ }).one();
66
106
 
67
- it('should return undefined maxDate when there is a course run with an undefined end date', () => {
68
- const product = ProductFactory({
69
- target_courses: [
70
- TargetCourseFactory({
71
- course_runs: [
72
- CourseRunFactory({
73
- start: new Date('2023-08-13').toISOString(),
74
- end: undefined,
75
- }).one(),
76
- CourseRunFactory({
77
- start: new Date('2023-09-17').toISOString(),
78
- end: new Date('2024-05-29').toISOString(),
79
- }).one(),
80
- ],
81
- }).one(),
82
- TargetCourseFactory({
83
- course_runs: [
84
- CourseRunFactory({
85
- start: new Date('2024-01-19').toISOString(),
86
- end: new Date('2025-02-17').toISOString(),
87
- }).one(),
88
- ],
89
- }).one(),
90
- ],
91
- }).one();
92
-
93
- expect(ProductHelper.getDateRange(product)).toEqual([new Date('2023-08-13'), undefined]);
107
+ expect(ProductHelper.getDateRange(product)).toEqual([new Date('2023-08-13'), undefined]);
108
+ });
94
109
  });
95
110
 
96
- it('should return the product languages processed from course runs languages', () => {
97
- const product = ProductFactory({
98
- target_courses: [
99
- TargetCourseFactory({
100
- course_runs: [
101
- CourseRunFactory({
102
- languages: ['fr', 'en'],
103
- }).one(),
104
- CourseRunFactory({
105
- languages: ['fr', 'de'],
106
- }).one(),
107
- ],
108
- }).one(),
109
- TargetCourseFactory({
110
- course_runs: [
111
- CourseRunFactory({
112
- languages: ['fr', 'es'],
113
- }).one(),
114
- ],
115
- }).one(),
116
- ],
117
- }).one();
118
-
119
- expect(ProductHelper.getLanguages(product)).toEqual(['fr', 'en', 'de', 'es']);
111
+ describe('getLanguages', () => {
112
+ it('should return the product languages processed from course runs languages', () => {
113
+ const product = ProductFactory({
114
+ target_courses: [
115
+ TargetCourseFactory({
116
+ course_runs: [
117
+ CourseRunFactory({
118
+ languages: ['fr', 'en'],
119
+ }).one(),
120
+ CourseRunFactory({
121
+ languages: ['fr', 'de'],
122
+ }).one(),
123
+ ],
124
+ }).one(),
125
+ TargetCourseFactory({
126
+ course_runs: [
127
+ CourseRunFactory({
128
+ languages: ['fr', 'es'],
129
+ }).one(),
130
+ ],
131
+ }).one(),
132
+ ],
133
+ }).one();
134
+
135
+ expect(ProductHelper.getLanguages(product)).toEqual(['fr', 'en', 'de', 'es']);
136
+ });
137
+
138
+ it('should return an empty array when there is no language', () => {
139
+ const product = ProductFactory({
140
+ target_courses: [
141
+ TargetCourseFactory({
142
+ course_runs: [
143
+ CourseRunFactory({
144
+ languages: [],
145
+ }).one(),
146
+ ],
147
+ }).one(),
148
+ ],
149
+ }).one();
150
+
151
+ expect(ProductHelper.getLanguages(product)).toEqual([]);
152
+ });
153
+
154
+ it('should return sorted human readable languages according to the active language', () => {
155
+ const product = ProductFactory({
156
+ target_courses: [
157
+ TargetCourseFactory({
158
+ course_runs: [
159
+ CourseRunFactory({
160
+ languages: ['fr', 'en'],
161
+ }).one(),
162
+ CourseRunFactory({
163
+ languages: ['fr', 'de'],
164
+ }).one(),
165
+ ],
166
+ }).one(),
167
+ TargetCourseFactory({
168
+ course_runs: [
169
+ CourseRunFactory({
170
+ languages: ['fr', 'es'],
171
+ }).one(),
172
+ ],
173
+ }).one(),
174
+ ],
175
+ }).one();
176
+
177
+ const intl = createIntl({ locale: 'en' });
178
+ expect(ProductHelper.getLanguages(product, true, intl)).toEqual(
179
+ 'English, French, German and Spanish',
180
+ );
181
+ });
182
+
183
+ it('should return an empty string when there is no language', () => {
184
+ const product = ProductFactory({
185
+ target_courses: [
186
+ TargetCourseFactory({
187
+ course_runs: [
188
+ CourseRunFactory({
189
+ languages: [],
190
+ }).one(),
191
+ ],
192
+ }).one(),
193
+ ],
194
+ }).one();
195
+
196
+ const intl = createIntl({ locale: 'en' });
197
+ expect(ProductHelper.getLanguages(product, true, intl)).toEqual('');
198
+ });
120
199
  });
121
200
 
122
- it('should return an empty array when there is no language', () => {
123
- const product = ProductFactory({
124
- target_courses: [
125
- TargetCourseFactory({
126
- course_runs: [
127
- CourseRunFactory({
128
- languages: [],
129
- }).one(),
130
- ],
131
- }).one(),
132
- ],
133
- }).one();
134
-
135
- expect(ProductHelper.getLanguages(product)).toEqual([]);
201
+ describe('hasRemainingSeats', () => {
202
+ it('should return false when the product is undefined', () => {
203
+ expect(ProductHelper.hasRemainingSeats(undefined)).toBe(false);
204
+ });
205
+
206
+ it('should return true when the product has no order group', () => {
207
+ const product = ProductFactory({
208
+ remaining_order_count: null,
209
+ }).one();
210
+
211
+ expect(ProductHelper.hasRemainingSeats(product)).toBe(true);
212
+ });
213
+
214
+ it('should return true when the product has remaining seats', () => {
215
+ const product = ProductFactory({
216
+ remaining_order_count: 10,
217
+ }).one();
218
+
219
+ expect(ProductHelper.hasRemainingSeats(product)).toBe(true);
220
+ });
221
+
222
+ it('should return false when the product does not have remaining seats', () => {
223
+ const product = ProductFactory({
224
+ remaining_order_count: 0,
225
+ }).one();
226
+
227
+ expect(ProductHelper.hasRemainingSeats(product)).toBe(false);
228
+ });
136
229
  });
137
230
 
138
- it('should return sorted human readable languages according to the active language', () => {
139
- const product = ProductFactory({
140
- target_courses: [
141
- TargetCourseFactory({
142
- course_runs: [
143
- CourseRunFactory({
144
- languages: ['fr', 'en'],
145
- }).one(),
146
- CourseRunFactory({
147
- languages: ['fr', 'de'],
148
- }).one(),
149
- ],
150
- }).one(),
151
- TargetCourseFactory({
152
- course_runs: [
153
- CourseRunFactory({
154
- languages: ['fr', 'es'],
155
- }).one(),
156
- ],
157
- }).one(),
158
- ],
159
- }).one();
160
-
161
- const intl = createIntl({ locale: 'en' });
162
- expect(ProductHelper.getLanguages(product, true, intl)).toEqual(
163
- 'English, French, German and Spanish',
164
- );
231
+ describe('hasOpenedTargetCourse', () => {
232
+ const openedCourseRunFactory = CourseRunFactory({
233
+ state: CourseStateFactory({
234
+ priority: Priority.ONGOING_OPEN,
235
+ }).one(),
236
+ });
237
+
238
+ const closedCourseRunFactory = CourseRunFactory({
239
+ state: CourseStateFactory({
240
+ priority: Priority.ARCHIVED_CLOSED,
241
+ }).one(),
242
+ });
243
+
244
+ it('should return false when the product is undefined', () => {
245
+ expect(ProductHelper.hasOpenedTargetCourse(undefined)).toBe(false);
246
+ });
247
+
248
+ it('should throw an error when the product is a certificate and the enrollment is undefined', () => {
249
+ const product = CertificateProductFactory().one();
250
+
251
+ expect(() => ProductHelper.hasOpenedTargetCourse(product, undefined)).toThrowError(
252
+ 'Unable to check if the certificate product relies on an opened course run without enrollment.',
253
+ );
254
+ });
255
+
256
+ it('should return true when the product is a certificate and the related course run is opened', () => {
257
+ const product = CertificateProductFactory().one();
258
+ const courseRun = openedCourseRunFactory.one();
259
+ const enrollment = EnrollmentFactory({ course_run: courseRun }).one();
260
+
261
+ expect(ProductHelper.hasOpenedTargetCourse(product, enrollment)).toBe(true);
262
+ });
263
+
264
+ it('should return false when the product is a certificate and the related course run is not opened', () => {
265
+ const product = CertificateProductFactory().one();
266
+ const courseRun = closedCourseRunFactory.one();
267
+ const enrollment = EnrollmentFactory({ course_run: courseRun }).one();
268
+
269
+ expect(ProductHelper.hasOpenedTargetCourse(product, enrollment)).toBe(false);
270
+ });
271
+
272
+ it('should return false when the product is a credential and has not target courses', () => {
273
+ const product = CredentialProductFactory({ target_courses: [] }).one();
274
+
275
+ expect(ProductHelper.hasOpenedTargetCourse(product)).toBe(false);
276
+ });
277
+
278
+ it('should return false when the product is a credential and at least one target course has no opened course run', () => {
279
+ const targetCourseOpen = TargetCourseFactory({
280
+ course_runs: [openedCourseRunFactory.one(), closedCourseRunFactory.one()],
281
+ }).one();
282
+ const targetCourseClosed = TargetCourseFactory({
283
+ course_runs: [closedCourseRunFactory.one(), closedCourseRunFactory.one()],
284
+ }).one();
285
+ const product = CredentialProductFactory({
286
+ target_courses: [targetCourseOpen, targetCourseClosed],
287
+ }).one();
288
+
289
+ expect(ProductHelper.hasOpenedTargetCourse(product)).toBe(false);
290
+ });
291
+
292
+ it('should return false when the product is a credential and target courses has one opened course run', () => {
293
+ const targetCourseOpen = TargetCourseFactory({
294
+ course_runs: [openedCourseRunFactory.one(), closedCourseRunFactory.one()],
295
+ }).many(1);
296
+ const product = CredentialProductFactory({ target_courses: targetCourseOpen }).one();
297
+
298
+ expect(ProductHelper.hasOpenedTargetCourse(product)).toBe(true);
299
+ });
165
300
  });
166
301
 
167
- it('should return an empty string when there is no language', () => {
168
- const product = ProductFactory({
169
- target_courses: [
170
- TargetCourseFactory({
171
- course_runs: [
172
- CourseRunFactory({
173
- languages: [],
174
- }).one(),
175
- ],
176
- }).one(),
177
- ],
178
- }).one();
179
-
180
- const intl = createIntl({ locale: 'en' });
181
- expect(ProductHelper.getLanguages(product, true, intl)).toEqual('');
302
+ describe('isPurchasable', () => {
303
+ it('should return false when the product is undefined', () => {
304
+ expect(ProductHelper.isPurchasable(undefined)).toBe(false);
305
+ });
306
+
307
+ it('should return false when the product does not have opened target courses', () => {
308
+ jest.spyOn(ProductHelper, 'hasOpenedTargetCourse').mockReturnValue(false);
309
+ jest.spyOn(ProductHelper, 'hasRemainingSeats').mockReturnValue(true);
310
+ const product = ProductFactory().one();
311
+
312
+ expect(ProductHelper.isPurchasable(product)).toBe(false);
313
+ });
314
+
315
+ it('should return false when the product does not remaining seats', () => {
316
+ jest.spyOn(ProductHelper, 'hasOpenedTargetCourse').mockReturnValue(true);
317
+ jest.spyOn(ProductHelper, 'hasRemainingSeats').mockReturnValue(false);
318
+ const product = ProductFactory().one();
319
+
320
+ expect(ProductHelper.isPurchasable(product)).toBe(false);
321
+ });
322
+
323
+ it('should return false when the product does not have opened target courses and remaining seats', () => {
324
+ jest.spyOn(ProductHelper, 'hasOpenedTargetCourse').mockReturnValue(false);
325
+ jest.spyOn(ProductHelper, 'hasRemainingSeats').mockReturnValue(false);
326
+ const product = ProductFactory().one();
327
+
328
+ expect(ProductHelper.isPurchasable(product)).toBe(false);
329
+ });
330
+
331
+ it('should return true when the product has opened target courses and remaining seats', () => {
332
+ jest.spyOn(ProductHelper, 'hasOpenedTargetCourse').mockReturnValue(true);
333
+ jest.spyOn(ProductHelper, 'hasRemainingSeats').mockReturnValue(true);
334
+ const product = ProductFactory().one();
335
+
336
+ expect(ProductHelper.isPurchasable(product)).toBe(true);
337
+ });
182
338
  });
183
339
  });
@@ -2,6 +2,8 @@ import { IntlShape } from 'react-intl';
2
2
  import { CourseProductRelation, Product, TargetCourse } from 'types/Joanie';
3
3
  import { Maybe } from 'types/utils';
4
4
  import { IntlHelper } from 'utils/IntlHelper';
5
+ import * as Joanie from 'types/Joanie';
6
+ import { isOpenedCourseRunCertificate, isOpenedCourseRunCredential } from 'utils/CourseRuns';
5
7
 
6
8
  /**
7
9
  * Helper class for products
@@ -45,4 +47,34 @@ export class ProductHelper {
45
47
  static getActiveOrderGroups(courseProductRelation: CourseProductRelation) {
46
48
  return courseProductRelation.order_groups?.filter((orderGroup) => orderGroup.is_active);
47
49
  }
50
+
51
+ static hasRemainingSeats(product: Maybe<Product>) {
52
+ if (!product) return false;
53
+ return typeof product?.remaining_order_count !== 'number' || product.remaining_order_count > 0;
54
+ }
55
+
56
+ static hasOpenedTargetCourse(product: Maybe<Product>, enrollment?: Maybe<Joanie.Enrollment>) {
57
+ if (!product) return false;
58
+
59
+ if (product.type === Joanie.ProductType.CERTIFICATE) {
60
+ if (!enrollment?.course_run) {
61
+ throw new Error(
62
+ 'Unable to check if the certificate product relies on an opened course run without enrollment.',
63
+ );
64
+ }
65
+
66
+ return isOpenedCourseRunCertificate(enrollment.course_run.state);
67
+ }
68
+
69
+ return (
70
+ product.target_courses.length > 0 &&
71
+ product.target_courses.every(({ course_runs }) =>
72
+ course_runs.some((targetCourseRun) => isOpenedCourseRunCredential(targetCourseRun.state)),
73
+ )
74
+ );
75
+ }
76
+
77
+ static isPurchasable(product: Maybe<Product>, enrollment?: Maybe<Joanie.Enrollment>) {
78
+ return this.hasOpenedTargetCourse(product, enrollment) && this.hasRemainingSeats(product);
79
+ }
48
80
  }