1
+ /* eslint-disable react/prop-types */
1
2
import MockAdapter from 'axios-mock-adapter' ;
2
3
import {
3
- render , waitFor , within ,
4
+ act , render , screen , waitFor , within ,
4
5
} from '@testing-library/react' ;
5
6
import userEvent from '@testing-library/user-event' ;
6
7
@@ -17,25 +18,56 @@ import { courseSectionVerticalMock } from '../__mocks__';
17
18
import { COMPONENT_TYPES } from '../../generic/block-type-utils/constants' ;
18
19
import AddComponent from './AddComponent' ;
19
20
import messages from './messages' ;
21
+ import { IframeProvider } from '../context/iFrameContext' ;
22
+ import { messageTypes } from '../constants' ;
20
23
21
24
let store ;
22
25
let axiosMock ;
23
26
const blockId = '123' ;
24
27
const handleCreateNewCourseXBlockMock = jest . fn ( ) ;
28
+ const usageKey = 'lb:Axim:TEST:html:571fe018-f3ce-45c9-8f53-5dafcb422fddest-usage-key' ;
25
29
26
- // Mock ComponentPicker to call onComponentSelected on load
30
+ // Mock ComponentPicker to call onComponentSelected on click
27
31
jest . mock ( '../../library-authoring/component-picker' , ( ) => ( {
28
- ComponentPicker : ( props ) => props . onComponentSelected ( { usageKey : 'test-usage-key' , blockType : 'html' } ) ,
32
+ ComponentPicker : ( props ) => {
33
+ const onClick = ( ) => {
34
+ if ( props . componentPickerMode === 'single' ) {
35
+ props . onComponentSelected ( {
36
+ usageKey,
37
+ blockType : 'html' ,
38
+ } ) ;
39
+ } else {
40
+ props . onChangeComponentSelection ( [ {
41
+ usageKey,
42
+ blockType : 'html' ,
43
+ } ] ) ;
44
+ }
45
+ } ;
46
+ return (
47
+ < button type = "submit" onClick = { onClick } >
48
+ Dummy button
49
+ </ button >
50
+ ) ;
51
+ } ,
52
+ } ) ) ;
53
+
54
+ const mockSendMessageToIframe = jest . fn ( ) ;
55
+ jest . mock ( '../context/hooks' , ( ) => ( {
56
+ useIframe : ( ) => ( {
57
+ sendMessageToIframe : mockSendMessageToIframe ,
58
+ } ) ,
29
59
} ) ) ;
30
60
31
61
const renderComponent = ( props ) => render (
32
62
< AppProvider store = { store } >
33
63
< IntlProvider locale = "en" >
34
- < AddComponent
35
- blockId = { blockId }
36
- handleCreateNewCourseXBlock = { handleCreateNewCourseXBlockMock }
37
- { ...props }
38
- />
64
+ < IframeProvider >
65
+ < AddComponent
66
+ blockId = { blockId }
67
+ handleCreateNewCourseXBlock = { handleCreateNewCourseXBlockMock }
68
+ { ...props }
69
+ />
70
+ </ IframeProvider >
39
71
</ IntlProvider >
40
72
</ AppProvider > ,
41
73
) ;
@@ -413,18 +445,64 @@ describe('<AddComponent />', () => {
413
445
} ) ;
414
446
415
447
it ( 'shows library picker on clicking v2 library content btn' , async ( ) => {
416
- const { findByRole } = renderComponent ( ) ;
417
- const libBtn = await findByRole ( 'button' , {
448
+ renderComponent ( ) ;
449
+ const libBtn = await screen . findByRole ( 'button' , {
418
450
name : new RegExp ( `${ messages . buttonText . defaultMessage } Library content` , 'i' ) ,
419
451
} ) ;
420
-
421
452
userEvent . click ( libBtn ) ;
453
+
454
+ // click dummy button to execute onComponentSelected prop.
455
+ const dummyBtn = await screen . findByRole ( 'button' , { name : 'Dummy button' } ) ;
456
+ userEvent . click ( dummyBtn ) ;
457
+
422
458
expect ( handleCreateNewCourseXBlockMock ) . toHaveBeenCalled ( ) ;
423
459
expect ( handleCreateNewCourseXBlockMock ) . toHaveBeenCalledWith ( {
424
460
type : COMPONENT_TYPES . libraryV2 ,
425
461
parentLocator : '123' ,
426
462
category : 'html' ,
427
- libraryContentKey : 'test-usage-key' ,
463
+ libraryContentKey : usageKey ,
464
+ } ) ;
465
+ } ) ;
466
+
467
+ it ( 'closes library component picker on close' , async ( ) => {
468
+ renderComponent ( ) ;
469
+ const libBtn = await screen . findByRole ( 'button' , {
470
+ name : new RegExp ( `${ messages . buttonText . defaultMessage } Library content` , 'i' ) ,
471
+ } ) ;
472
+ userEvent . click ( libBtn ) ;
473
+
474
+ expect ( screen . queryByRole ( 'button' , { name : 'Dummy button' } ) ) . toBeInTheDocument ( ) ;
475
+ // click dummy button to execute onComponentSelected prop.
476
+ const closeBtn = await screen . findByRole ( 'button' , { name : 'Close' } ) ;
477
+ userEvent . click ( closeBtn ) ;
478
+
479
+ expect ( screen . queryByRole ( 'button' , { name : 'Dummy button' } ) ) . not . toBeInTheDocument ( ) ;
480
+ } ) ;
481
+
482
+ it ( 'shows component picker on window message' , async ( ) => {
483
+ renderComponent ( ) ;
484
+ const message = {
485
+ data : {
486
+ type : messageTypes . showMultipleComponentPicker ,
487
+ } ,
488
+ } ;
489
+ // Dispatch showMultipleComponentPicker message event to open the picker modal.
490
+ act ( ( ) => {
491
+ window . dispatchEvent ( new MessageEvent ( 'message' , message ) ) ;
492
+ } ) ;
493
+
494
+ // click dummy button to execute onChangeComponentSelection prop.
495
+ const dummyBtn = await screen . findByRole ( 'button' , { name : 'Dummy button' } ) ;
496
+ userEvent . click ( dummyBtn ) ;
497
+
498
+ const submitBtn = await screen . findByRole ( 'button' , { name : 'Add selected components' } ) ;
499
+ userEvent . click ( submitBtn ) ;
500
+
501
+ expect ( mockSendMessageToIframe ) . toHaveBeenCalledWith ( messageTypes . addSelectedComponentsToBank , {
502
+ selectedComponents : [ {
503
+ blockType : 'html' ,
504
+ usageKey,
505
+ } ] ,
428
506
} ) ;
429
507
} ) ;
430
508
0 commit comments