1+ import { createElement , type ReactNode } from 'react' ;
12import { getConfig } from '@edx/frontend-platform' ;
3+ import { IntlProvider , useIntl } from '@edx/frontend-platform/i18n' ;
24
5+ import { renderHook } from '@src/setupTest' ;
36import { mockCourseAboutResponse } from '@src/__mocks__' ;
47import {
58 getTwitterShareUrl ,
@@ -26,18 +29,11 @@ Object.defineProperty(window, 'location', {
2629} ) ;
2730
2831describe ( 'Social Sharing Utils' , ( ) => {
29- const mockIntl = {
30- formatMessage : jest . fn ( ) . mockImplementation ( ( message , values ) => {
31- if ( ! values ) {
32- return message . defaultMessage ;
33- }
34-
35- return Object . entries ( values ) . reduce (
36- ( str , [ key , value ] ) => str . replace ( `{${ key } }` , value || '' ) ,
37- message . defaultMessage ,
38- ) ;
39- } ) ,
40- } ;
32+ const wrapper = ( { children } : { children : ReactNode } ) => (
33+ createElement ( IntlProvider , { locale : 'en' , messages : { } } , children )
34+ ) ;
35+ const intl = renderHook ( ( ) => useIntl ( ) , { wrapper } ) . result . current ;
36+ let formatMessageSpy : jest . SpyInstance ;
4137
4238 const createCourseData = ( overrides = { } ) => ( {
4339 ...mockCourseAboutResponse ,
@@ -46,6 +42,7 @@ describe('Social Sharing Utils', () => {
4642
4743 beforeEach ( ( ) => {
4844 jest . clearAllMocks ( ) ;
45+ formatMessageSpy = jest . spyOn ( intl , 'formatMessage' ) ;
4946 window . location . href = mockLocation . href ;
5047 } ) ;
5148
@@ -56,7 +53,7 @@ describe('Social Sharing Utils', () => {
5653 name : 'Introduction to Computer Science' ,
5754 } ) ;
5855
59- const result = getTwitterShareUrl ( courseData , mockIntl ) ;
56+ const result = getTwitterShareUrl ( courseData , intl ) ;
6057
6158 expect ( result ) . toContain ( 'https://twitter.com/intent/tweet?text=' ) ;
6259 expect ( result ) . toContain ( encodeURIComponent ( courseData . displayNumberWithDefault ) ) ;
@@ -70,9 +67,9 @@ describe('Social Sharing Utils', () => {
7067 displayNumberWithDefault : 'CS101' ,
7168 name : 'Test Course' ,
7269 } ) ;
73- getTwitterShareUrl ( courseData , mockIntl ) ;
70+ getTwitterShareUrl ( courseData , intl ) ;
7471
75- expect ( mockIntl . formatMessage ) . toHaveBeenCalledWith (
72+ expect ( formatMessageSpy ) . toHaveBeenCalledWith (
7673 messages . socialSharingTwitterText ,
7774 {
7875 courseNumber : courseData . displayNumberWithDefault ,
@@ -91,7 +88,7 @@ describe('Social Sharing Utils', () => {
9188 name : 'Advanced Mathematics' ,
9289 } ) ;
9390
94- const result = getEmailShareUrl ( courseData , mockIntl ) ;
91+ const result = getEmailShareUrl ( courseData , intl ) ;
9592
9693 expect ( result ) . toContain ( 'mailto:?subject=' ) ;
9794 expect ( result ) . toContain ( '&body=' ) ;
@@ -106,13 +103,13 @@ describe('Social Sharing Utils', () => {
106103 displayNumberWithDefault : 'MATH201' ,
107104 name : 'Advanced Mathematics' ,
108105 } ) ;
109- getEmailShareUrl ( courseData , mockIntl ) ;
106+ getEmailShareUrl ( courseData , intl ) ;
110107
111- expect ( mockIntl . formatMessage ) . toHaveBeenCalledWith (
108+ expect ( formatMessageSpy ) . toHaveBeenCalledWith (
112109 messages . socialSharingEmailSubject ,
113110 { siteName : getConfig ( ) . SITE_NAME } ,
114111 ) ;
115- expect ( mockIntl . formatMessage ) . toHaveBeenCalledWith (
112+ expect ( formatMessageSpy ) . toHaveBeenCalledWith (
116113 messages . socialSharingEmailBody ,
117114 {
118115 courseNumber : courseData . displayNumberWithDefault ,
@@ -129,8 +126,8 @@ describe('Social Sharing Utils', () => {
129126 name : 'Test Course' ,
130127 } ) ;
131128
132- const twitterUrl = getTwitterShareUrl ( courseData , mockIntl ) ;
133- const emailUrl = getEmailShareUrl ( courseData , mockIntl ) ;
129+ const twitterUrl = getTwitterShareUrl ( courseData , intl ) ;
130+ const emailUrl = getEmailShareUrl ( courseData , intl ) ;
134131
135132 expect ( twitterUrl ) . toContain ( encodeURIComponent (
136133 messages . socialSharingTwitterText . defaultMessage
@@ -168,7 +165,7 @@ describe('Social Sharing Utils', () => {
168165
169166 describe ( 'getSocialLinks' , ( ) => {
170167 it ( 'returns array of social link configurations' , ( ) => {
171- const result = getSocialLinks ( mockIntl ) ;
168+ const result = getSocialLinks ( intl ) ;
172169
173170 expect ( result ) . toHaveLength ( 3 ) ;
174171 expect ( result [ 0 ] . id ) . toBe ( 'twitter' ) ;
@@ -177,19 +174,19 @@ describe('Social Sharing Utils', () => {
177174 } ) ;
178175
179176 it ( 'includes correct icons for each social platform' , ( ) => {
180- const result = getSocialLinks ( mockIntl ) ;
177+ const result = getSocialLinks ( intl ) ;
181178
182179 expect ( result [ 0 ] . icon ) . toBeDefined ( ) ;
183180 expect ( result [ 1 ] . icon ) . toBeDefined ( ) ;
184181 expect ( result [ 2 ] . icon ) . toBeDefined ( ) ;
185182 } ) ;
186183
187184 it ( 'includes screen reader text for each platform' , ( ) => {
188- getSocialLinks ( mockIntl ) ;
185+ getSocialLinks ( intl ) ;
189186
190- expect ( mockIntl . formatMessage ) . toHaveBeenCalledWith ( messages . socialSharingTwitter ) ;
191- expect ( mockIntl . formatMessage ) . toHaveBeenCalledWith ( messages . socialSharingFacebook ) ;
192- expect ( mockIntl . formatMessage ) . toHaveBeenCalledWith ( messages . socialSharingEmail ) ;
187+ expect ( formatMessageSpy ) . toHaveBeenCalledWith ( messages . socialSharingTwitter ) ;
188+ expect ( formatMessageSpy ) . toHaveBeenCalledWith ( messages . socialSharingFacebook ) ;
189+ expect ( formatMessageSpy ) . toHaveBeenCalledWith ( messages . socialSharingEmail ) ;
193190 } ) ;
194191 } ) ;
195192
@@ -201,8 +198,8 @@ describe('Social Sharing Utils', () => {
201198 } ) ;
202199
203200 expect ( ( ) => {
204- getTwitterShareUrl ( courseData , mockIntl ) ;
205- getEmailShareUrl ( courseData , mockIntl ) ;
201+ getTwitterShareUrl ( courseData , intl ) ;
202+ getEmailShareUrl ( courseData , intl ) ;
206203 } ) . not . toThrow ( ) ;
207204 } ) ;
208205
@@ -212,8 +209,8 @@ describe('Social Sharing Utils', () => {
212209 name : '' ,
213210 } ) ;
214211
215- const twitterUrl = getTwitterShareUrl ( courseData , mockIntl ) ;
216- const emailUrl = getEmailShareUrl ( courseData , mockIntl ) ;
212+ const twitterUrl = getTwitterShareUrl ( courseData , intl ) ;
213+ const emailUrl = getEmailShareUrl ( courseData , intl ) ;
217214
218215 expect ( twitterUrl ) . toContain ( 'https://twitter.com/intent/tweet?text=' ) ;
219216 expect ( emailUrl ) . toContain ( 'mailto:?subject=' ) ;
@@ -226,8 +223,8 @@ describe('Social Sharing Utils', () => {
226223 } ) ;
227224
228225 const courseData = createCourseData ( ) ;
229- const twitterUrl = getTwitterShareUrl ( courseData , mockIntl ) ;
230- const emailUrl = getEmailShareUrl ( courseData , mockIntl ) ;
226+ const twitterUrl = getTwitterShareUrl ( courseData , intl ) ;
227+ const emailUrl = getEmailShareUrl ( courseData , intl ) ;
231228
232229 expect ( twitterUrl ) . toContain ( 'https://twitter.com/intent/tweet?text=' ) ;
233230 expect ( emailUrl ) . toContain ( 'mailto:?subject=' ) ;
@@ -241,8 +238,8 @@ describe('Social Sharing Utils', () => {
241238 name : 'Programming & Algorithms: "Advanced" Topics' ,
242239 } ) ;
243240
244- const twitterUrl = getTwitterShareUrl ( courseData , mockIntl ) ;
245- const emailUrl = getEmailShareUrl ( courseData , mockIntl ) ;
241+ const twitterUrl = getTwitterShareUrl ( courseData , intl ) ;
242+ const emailUrl = getEmailShareUrl ( courseData , intl ) ;
246243
247244 expect ( twitterUrl ) . toContain ( encodeURIComponent ( courseData . displayNumberWithDefault ) ) ;
248245 expect ( twitterUrl ) . toContain ( encodeURIComponent ( courseData . name ) ) ;
0 commit comments