@@ -9,60 +9,135 @@ import { getConfig, initializeMockApp } from '@edx/frontend-platform';
99import { AppProvider } from '@edx/frontend-platform/react' ;
1010
1111import { initializeStore } from '../../store' ;
12- import { useContainerSizeForParent } from './hooks' ;
12+ import { DiscussionContext } from '../common/context' ;
13+ import { useContainerSizeForParent , useCurrentDiscussionTopic } from './hooks' ;
1314
1415let store ;
1516initializeMockApp ( ) ;
1617describe ( 'Hooks' , ( ) => {
17- function ComponentWithHook ( ) {
18- const refContainer = useRef ( null ) ;
19- useContainerSizeForParent ( refContainer ) ;
20- return (
21- < div >
22- < div ref = { refContainer } />
23- </ div >
24- ) ;
25- }
18+ describe ( 'useContainerSizeForParent' , ( ) => {
19+ function ComponentWithHook ( ) {
20+ const refContainer = useRef ( null ) ;
21+ useContainerSizeForParent ( refContainer ) ;
22+ return (
23+ < div >
24+ < div ref = { refContainer } />
25+ </ div >
26+ ) ;
27+ }
2628
27- function renderComponent ( ) {
28- return render (
29- < IntlProvider locale = "en" >
30- < ResponsiveContext . Provider value = { { width : 1280 } } >
29+ function renderComponent ( ) {
30+ return render (
31+ < IntlProvider locale = "en" >
32+ < ResponsiveContext . Provider value = { { width : 1280 } } >
33+ < AppProvider store = { store } >
34+ < MemoryRouter initialEntries = { [ '/' ] } >
35+ < ComponentWithHook />
36+ </ MemoryRouter >
37+ </ AppProvider >
38+ </ ResponsiveContext . Provider >
39+ </ IntlProvider > ,
40+ ) ;
41+ }
42+
43+ let parent ;
44+ beforeEach ( ( ) => {
45+ store = initializeStore ( ) ;
46+ parent = window . parent ;
47+ } ) ;
48+ afterEach ( ( ) => {
49+ window . parent = parent ;
50+ } ) ;
51+ test ( 'useContainerSizeForParent enabled' , async ( ) => {
52+ delete window . parent ;
53+ window . parent = { ...window , postMessage : jest . fn ( ) } ;
54+ const { unmount } = renderComponent ( ) ;
55+ // Once for LMS and one for learning MFE
56+ await waitFor ( ( ) => expect ( window . parent . postMessage ) . toHaveBeenCalledTimes ( 2 ) ) ;
57+ // Test that size is reset on unmount
58+ unmount ( ) ;
59+ await waitFor ( ( ) => expect ( window . parent . postMessage ) . toHaveBeenCalledTimes ( 4 ) ) ;
60+ expect ( window . parent . postMessage ) . toHaveBeenLastCalledWith (
61+ { type : 'plugin.resize' , payload : { height : null } } ,
62+ getConfig ( ) . LMS_BASE_URL ,
63+ ) ;
64+ } ) ;
65+ test ( 'useContainerSizeForParent disabled' , async ( ) => {
66+ window . parent . postMessage = jest . fn ( ) ;
67+ renderComponent ( ) ;
68+ await waitFor ( ( ) => expect ( window . parent . postMessage ) . not . toHaveBeenCalled ( ) ) ;
69+ } ) ;
70+ } ) ;
71+
72+ describe ( 'useCurrentDiscussionTopic' , ( ) => {
73+ function ComponentWithHook ( ) {
74+ const topic = useCurrentDiscussionTopic ( ) ;
75+ return (
76+ < div >
77+ { String ( topic ) }
78+ </ div >
79+ ) ;
80+ }
81+
82+ function renderComponent ( { topicId, category } ) {
83+ return render (
84+ < IntlProvider locale = "en" >
3185 < AppProvider store = { store } >
32- < MemoryRouter initialEntries = { [ '/' ] } >
86+ < DiscussionContext . Provider
87+ value = { {
88+ topicId,
89+ category,
90+ } }
91+ >
3392 < ComponentWithHook />
34- </ MemoryRouter >
93+ </ DiscussionContext . Provider >
3594 </ AppProvider >
36- </ ResponsiveContext . Provider >
37- </ IntlProvider > ,
38- ) ;
39- }
95+ </ IntlProvider > ,
96+ ) ;
97+ }
4098
41- let parent ;
42- beforeEach ( ( ) => {
43- store = initializeStore ( ) ;
44- parent = window . parent ;
45- } ) ;
46- afterEach ( ( ) => {
47- window . parent = parent ;
48- } ) ;
49- test ( 'useContainerSizeForParent enabled' , async ( ) => {
50- delete window . parent ;
51- window . parent = { ...window , postMessage : jest . fn ( ) } ;
52- const { unmount } = renderComponent ( ) ;
53- // Once for LMS and one for learning MFE
54- await waitFor ( ( ) => expect ( window . parent . postMessage ) . toHaveBeenCalledTimes ( 2 ) ) ;
55- // Test that size is reset on unmount
56- unmount ( ) ;
57- await waitFor ( ( ) => expect ( window . parent . postMessage ) . toHaveBeenCalledTimes ( 4 ) ) ;
58- expect ( window . parent . postMessage ) . toHaveBeenLastCalledWith (
59- { type : 'plugin.resize' , payload : { height : null } } ,
60- getConfig ( ) . LMS_BASE_URL ,
61- ) ;
62- } ) ;
63- test ( 'useContainerSizeForParent disabled' , async ( ) => {
64- window . parent . postMessage = jest . fn ( ) ;
65- renderComponent ( ) ;
66- await waitFor ( ( ) => expect ( window . parent . postMessage ) . not . toHaveBeenCalled ( ) ) ;
99+ beforeEach ( ( ) => {
100+ store = initializeStore ( {
101+ blocks : {
102+ blocks : {
103+ 'some-unit-key' : { topics : [ 'some-topic-0' ] , parent : 'some-sequence-key' } ,
104+ 'some-sequence-key' : { topics : [ 'some-topic-0' ] } ,
105+ 'another-sequence-key' : { topics : [ 'some-topic-1' , 'some-topic-2' ] } ,
106+ 'empty-key' : { topics : [ ] } ,
107+ } ,
108+ } ,
109+ config : { provider : 'openedx' } ,
110+ } ) ;
111+ } ) ;
112+
113+ test ( 'when topicId is in context' , ( ) => {
114+ const { queryByText } = renderComponent ( { topicId : 'some-topic' } ) ;
115+ expect ( queryByText ( 'some-topic' ) ) . toBeInTheDocument ( ) ;
116+ } ) ;
117+
118+ test ( 'when the category is a unit' , ( ) => {
119+ const { queryByText } = renderComponent ( { category : 'some-unit-key' } ) ;
120+ expect ( queryByText ( 'some-topic-0' ) ) . toBeInTheDocument ( ) ;
121+ } ) ;
122+
123+ test ( 'when the category is a sequence with one unit' , ( ) => {
124+ const { queryByText } = renderComponent ( { category : 'some-sequence-key' } ) ;
125+ expect ( queryByText ( 'some-topic-0' ) ) . toBeInTheDocument ( ) ;
126+ } ) ;
127+
128+ test ( 'when the category is a sequence with multiple units' , ( ) => {
129+ const { queryByText } = renderComponent ( { category : 'another-sequence-key' } ) ;
130+ expect ( queryByText ( 'null' ) ) . toBeInTheDocument ( ) ;
131+ } ) ;
132+
133+ test ( 'when the category is invalid' , ( ) => {
134+ const { queryByText } = renderComponent ( { category : 'invalid-key' } ) ;
135+ expect ( queryByText ( 'null' ) ) . toBeInTheDocument ( ) ;
136+ } ) ;
137+
138+ test ( 'when the category has no topics' , ( ) => {
139+ const { queryByText } = renderComponent ( { category : 'empty-key' } ) ;
140+ expect ( queryByText ( 'null' ) ) . toBeInTheDocument ( ) ;
141+ } ) ;
67142 } ) ;
68143} ) ;
0 commit comments