@@ -791,7 +791,7 @@ describe('elements/content-preview/ContentPreview', () => {
791791 beforeEach ( ( ) => {
792792 props = {
793793 collection : [ { } , { } ] ,
794- fileId : file . id ,
794+ fileId : 123 ,
795795 onLoad : jest . fn ( ) ,
796796 token : 'token' ,
797797 } ;
@@ -824,6 +824,16 @@ describe('elements/content-preview/ContentPreview', () => {
824824 instance . onPreviewLoad ( data ) ;
825825 expect ( instance . state . isLoading ) . toBe ( false ) ;
826826 } ) ;
827+
828+ test ( 'should call dynamicOnPreviewLoadAction if it is defined' , ( ) => {
829+ instance . dynamicOnPreviewLoadAction = jest . fn ( ) ;
830+ instance . onPreviewLoad ( data ) ;
831+ expect ( instance . dynamicOnPreviewLoadAction ) . toBeCalled ( ) ;
832+ } ) ;
833+
834+ test ( 'should not call dynamicOnPreviewLoadAction if it is not defined' , ( ) => {
835+ expect ( ( ) => instance . onPreviewLoad ( data ) ) . not . toThrow ( ) ;
836+ } ) ;
827837 } ) ;
828838
829839 describe ( 'onPreviewMetric()' , ( ) => {
@@ -1285,6 +1295,7 @@ describe('elements/content-preview/ContentPreview', () => {
12851295 annotationFileVersionId | selectedVersionId | locationType | setStateCount
12861296 ${ '123' } | ${ '124' } | ${ 'page' } | ${ 1 }
12871297 ${ '124' } | ${ '124' } | ${ 'page' } | ${ 0 }
1298+ ${ '123' } | ${ '124' } | ${ 'frame' } | ${ 0 }
12881299 ${ '123' } | ${ '124' } | ${ '' } | ${ 0 }
12891300 ${ undefined } | ${ '124' } | ${ 'page' } | ${ 0 }
12901301 ` (
@@ -1317,6 +1328,112 @@ describe('elements/content-preview/ContentPreview', () => {
13171328 expect ( emit ) . toBeCalledWith ( 'scrolltoannotation' , { id : annotation . id , target : annotation . target } ) ;
13181329 } ,
13191330 ) ;
1331+
1332+ test . each `
1333+ annotationFileVersionId | selectedVersionId | locationType | deferScrollToOnload
1334+ ${ '123' } | ${ '124' } | ${ 'frame' } | ${ true }
1335+ ${ '123' } | ${ '124' } | ${ 'page' } | ${ false }
1336+ ` (
1337+ 'should not call emit scrolltoannotation if deferScrollToOnload is $deferScrollToOnload' ,
1338+ ( { annotationFileVersionId, selectedVersionId, locationType, deferScrollToOnload } ) => {
1339+ const annotation = {
1340+ id : '123' ,
1341+ file_version : { id : annotationFileVersionId } ,
1342+ target : { location : { type : locationType } } ,
1343+ } ;
1344+ const wrapper = getWrapper ( ) ;
1345+ const instance = wrapper . instance ( ) ;
1346+ wrapper . setState ( { selectedVersion : { id : selectedVersionId } } ) ;
1347+ const emit = jest . fn ( ) ;
1348+ jest . spyOn ( instance , 'getViewer' ) . mockReturnValue ( { emit } ) ;
1349+ instance . setState = jest . fn ( ) ;
1350+
1351+ instance . handleAnnotationSelect ( annotation , deferScrollToOnload ) ;
1352+ if ( deferScrollToOnload ) {
1353+ expect ( emit ) . not . toBeCalled ( ) ;
1354+ expect ( instance . dynamicOnPreviewLoadAction ) . toBeDefined ( ) ;
1355+ } else {
1356+ expect ( emit ) . toBeCalledWith ( 'scrolltoannotation' , { id : annotation . id , target : annotation . target } ) ;
1357+ expect ( instance . dynamicOnPreviewLoadAction ) . not . toBeDefined ( ) ;
1358+ }
1359+ } ,
1360+ ) ;
1361+ test ( 'should set dynamicOnPreviewLoadAction and execute scrollToFrameAnnotation properly if deferScrollToOnload is true' , ( ) => {
1362+ const annotation = {
1363+ id : '123' ,
1364+ file_version : { id : '123' } ,
1365+ target : { location : { type : 'frame' } } ,
1366+ } ;
1367+ const wrapper = getWrapper ( ) ;
1368+ const instance = wrapper . instance ( ) ;
1369+ wrapper . setState ( { selectedVersion : { id : 123 } } ) ;
1370+ const emit = jest . fn ( ) ;
1371+ jest . spyOn ( instance , 'getViewer' ) . mockReturnValue ( { emit } ) ;
1372+ instance . setState = jest . fn ( ) ;
1373+ instance . handleAnnotationSelect ( annotation , true ) ;
1374+ expect ( instance . dynamicOnPreviewLoadAction ) . toBeDefined ( ) ;
1375+ let handleLoadedData ;
1376+ const mockAddEventListener = jest . fn ( ) . mockImplementation ( ( listener , callback ) => {
1377+ expect ( listener ) . toBe ( 'loadeddata' ) ;
1378+ expect ( callback ) . toBeDefined ( ) ;
1379+ handleLoadedData = callback ;
1380+ } ) ;
1381+ const mockRemoveEventListener = jest . fn ( ) . mockImplementation ( ( listener , callback ) => {
1382+ expect ( listener ) . toBe ( 'loadeddata' ) ;
1383+ expect ( callback ) . toEqual ( handleLoadedData ) ;
1384+ handleLoadedData = null ;
1385+ } ) ;
1386+ jest . spyOn ( document , 'querySelector' ) . mockReturnValue ( {
1387+ addEventListener : mockAddEventListener ,
1388+ removeEventListener : mockRemoveEventListener ,
1389+ } ) ;
1390+ instance . dynamicOnPreviewLoadAction ( ) ;
1391+ expect ( mockAddEventListener ) . toBeCalledWith ( 'loadeddata' , handleLoadedData ) ;
1392+ handleLoadedData ( ) ;
1393+ expect ( emit ) . toBeCalledWith ( 'scrolltoannotation' , { id : annotation . id , target : annotation . target } ) ;
1394+ expect ( instance . dynamicOnPreviewLoadAction ) . toBeNull ( ) ;
1395+ expect ( mockRemoveEventListener ) . toHaveBeenCalledTimes ( 1 ) ;
1396+ } ) ;
1397+
1398+ test ( 'dynamicOnPreviewLoadAction should return if video player does not exist' , ( ) => {
1399+ const annotation = {
1400+ id : '123' ,
1401+ file_version : { id : '123' } ,
1402+ target : { location : { type : 'frame' } } ,
1403+ } ;
1404+ const wrapper = getWrapper ( ) ;
1405+ const instance = wrapper . instance ( ) ;
1406+ wrapper . setState ( { selectedVersion : { id : 123 } } ) ;
1407+ const emit = jest . fn ( ) ;
1408+ jest . spyOn ( instance , 'getViewer' ) . mockReturnValue ( { emit } ) ;
1409+ instance . setState = jest . fn ( ) ;
1410+ jest . spyOn ( document , 'querySelector' ) . mockReturnValue ( null ) ;
1411+ instance . handleAnnotationSelect ( annotation , true ) ;
1412+ instance . dynamicOnPreviewLoadAction ( ) ;
1413+ expect ( instance . dynamicOnPreviewLoadAction ) . toBeNull ( ) ;
1414+ expect ( emit ) . not . toBeCalled ( ) ;
1415+ } ) ;
1416+
1417+ test ( 'dynamicOnPreviewLoadAction should still call emit scrolltoannotation if target location type is not frame' , ( ) => {
1418+ const annotation = {
1419+ id : '123' ,
1420+ file_version : { id : '123' } ,
1421+ target : { location : { type : 'page' } } ,
1422+ } ;
1423+ const wrapper = getWrapper ( ) ;
1424+ const instance = wrapper . instance ( ) ;
1425+ wrapper . setState ( { selectedVersion : { id : 123 } } ) ;
1426+ const emit = jest . fn ( ) ;
1427+ jest . spyOn ( instance , 'getViewer' ) . mockReturnValue ( { emit } ) ;
1428+ const scrollToFrameAnnotation = jest . fn ( ) ;
1429+ jest . spyOn ( instance , 'scrollToFrameAnnotation' ) . mockReturnValue ( scrollToFrameAnnotation ) ;
1430+ instance . setState = jest . fn ( ) ;
1431+ instance . handleAnnotationSelect ( annotation , true ) ;
1432+ instance . dynamicOnPreviewLoadAction ( ) ;
1433+ expect ( scrollToFrameAnnotation ) . not . toBeCalled ( ) ;
1434+ expect ( emit ) . toBeCalledWith ( 'scrolltoannotation' , { id : annotation . id , target : annotation . target } ) ;
1435+ expect ( instance . dynamicOnPreviewLoadAction ) . toBeNull ( ) ;
1436+ } ) ;
13201437 } ) ;
13211438
13221439 describe ( 'getThumbnail()' , ( ) => {
@@ -1351,4 +1468,77 @@ describe('elements/content-preview/ContentPreview', () => {
13511468 expect ( pageThumbnail ) . toBeNull ( ) ;
13521469 } ) ;
13531470 } ) ;
1471+
1472+ describe ( 'scrollToFrameAnnotation()' , ( ) => {
1473+ let annotation ;
1474+ let addEventListener ;
1475+ let removeEventListener ;
1476+ let mockVideoPlayer ;
1477+ beforeEach ( ( ) => {
1478+ addEventListener = jest . fn ( ) ;
1479+ removeEventListener = jest . fn ( ) ;
1480+ mockVideoPlayer = {
1481+ addEventListener,
1482+ removeEventListener,
1483+ readyState : 0 ,
1484+ } ;
1485+
1486+ annotation = {
1487+ id : '123' ,
1488+ file_version : { id : '123' } ,
1489+ target : { location : { type : 'frame' } } ,
1490+ } ;
1491+
1492+ jest . spyOn ( document , 'querySelector' ) . mockImplementation ( selector => {
1493+ expect ( selector ) . toBe ( '.bp-media-container video' ) ;
1494+ return mockVideoPlayer ;
1495+ } ) ;
1496+ } ) ;
1497+
1498+ afterEach ( ( ) => {
1499+ jest . clearAllMocks ( ) ;
1500+ } ) ;
1501+
1502+ test ( 'should return if the video player is not found' , ( ) => {
1503+ const wrapper = getWrapper ( ) ;
1504+ const instance = wrapper . instance ( ) ;
1505+ const emit = jest . fn ( ) ;
1506+ jest . spyOn ( instance , 'getViewer' ) . mockReturnValue ( { emit } ) ;
1507+ jest . spyOn ( document , 'querySelector' ) . mockReturnValue ( null ) ;
1508+ instance . scrollToFrameAnnotation ( annotation . id , annotation . target ) ;
1509+ expect ( instance . dynamicOnPreviewLoadAction ) . toBeNull ( ) ;
1510+ expect ( emit ) . not . toBeCalled ( ) ;
1511+ } ) ;
1512+
1513+ test ( 'should emit scrolltoannotation if video player is ready to seek and do not set video player event listener' , ( ) => {
1514+ const wrapper = getWrapper ( ) ;
1515+ const instance = wrapper . instance ( ) ;
1516+ const emit = jest . fn ( ) ;
1517+ jest . spyOn ( instance , 'getViewer' ) . mockReturnValue ( { emit } ) ;
1518+ mockVideoPlayer . readyState = 4 ;
1519+ instance . scrollToFrameAnnotation ( annotation . id , annotation . target ) ;
1520+ expect ( emit ) . toBeCalledWith ( 'scrolltoannotation' , { id : annotation . id , target : annotation . target } ) ;
1521+ expect ( mockVideoPlayer . addEventListener ) . not . toBeCalled ( ) ;
1522+ } ) ;
1523+
1524+ test ( 'should not throw an error if the video player is ready to seek and the viewer is not found' , ( ) => {
1525+ const wrapper = getWrapper ( ) ;
1526+ const instance = wrapper . instance ( ) ;
1527+ jest . spyOn ( instance , 'getViewer' ) . mockReturnValue ( null ) ;
1528+ mockVideoPlayer . readyState = 4 ;
1529+ instance . scrollToFrameAnnotation ( annotation . id , annotation . target ) ;
1530+ } ) ;
1531+
1532+ test ( 'should add loadeddata event listener to video player if video player is not ready to seek' , ( ) => {
1533+ const wrapper = getWrapper ( ) ;
1534+ const instance = wrapper . instance ( ) ;
1535+ wrapper . setState ( { selectedVersion : { id : 123 } } ) ;
1536+ const emit = jest . fn ( ) ;
1537+ jest . spyOn ( instance , 'getViewer' ) . mockReturnValue ( { emit } ) ;
1538+ mockVideoPlayer . readyState = 0 ;
1539+ instance . scrollToFrameAnnotation ( annotation . id , annotation . target ) ;
1540+ expect ( emit ) . not . toBeCalled ( ) ;
1541+ expect ( addEventListener ) . toBeCalledWith ( 'loadeddata' , expect . any ( Function ) ) ;
1542+ } ) ;
1543+ } ) ;
13541544} ) ;
0 commit comments