@@ -3,9 +3,25 @@ import { render, screen } from '@testing-library/react';
33
44import { FileViewerComponent as FileViewer } from "../index" ;
55import { msg0 , msg1 } from '../data.mock' ;
6+ import { SendbirdSdkContext } from '../../../lib/SendbirdSdkContext' ;
7+ import { MODAL_ROOT } from '../../../hooks/useModal' ;
68
79describe ( 'ui/FileViewer' , ( ) => {
8- it ( 'should display image' , function ( ) {
10+ let modalRoot ;
11+
12+ beforeAll ( ( ) => {
13+ // Create a modal root element and append it to the body
14+ modalRoot = document . createElement ( 'div' ) ;
15+ modalRoot . setAttribute ( 'id' , MODAL_ROOT ) ;
16+ document . body . appendChild ( modalRoot ) ;
17+ } ) ;
18+
19+ afterAll ( ( ) => {
20+ // Remove the modal root element after tests
21+ document . body . removeChild ( modalRoot ) ;
22+ } ) ;
23+
24+ it ( 'should display image' , function ( ) {
925 const {
1026 sender,
1127 type,
@@ -14,15 +30,17 @@ describe('ui/FileViewer', () => {
1430 } = msg0 ;
1531 const { profileUrl, nickname = '' } = sender ;
1632 render (
17- < FileViewer
18- profileUrl = { profileUrl }
19- nickname = { nickname }
20- type = { type }
21- url = { url }
22- name = { name }
23- onClose = { ( ) => { } }
24- onDelete = { ( ) => { } }
33+ < SendbirdSdkContext . Provider value = { { } } >
34+ < FileViewer
35+ profileUrl = { profileUrl }
36+ nickname = { nickname }
37+ type = { type }
38+ url = { url }
39+ name = { name }
40+ onClose = { ( ) => { } }
41+ onDelete = { ( ) => { } }
2542 />
43+ </ SendbirdSdkContext . Provider >
2644 ) ;
2745 expect (
2846 screen . getByAltText ( msg0 . name ) . className
@@ -35,7 +53,7 @@ describe('ui/FileViewer', () => {
3553 ) . toEqual ( msg0 . url ) ;
3654 } ) ;
3755
38- it ( 'should display video' , function ( ) {
56+ it ( 'should display video' , function ( ) {
3957 const {
4058 sender,
4159 type,
@@ -44,28 +62,33 @@ describe('ui/FileViewer', () => {
4462 } = msg1 ;
4563 const { profileUrl, nickname = '' } = sender ;
4664 const { container } = render (
47- < FileViewer
48- profileUrl = { profileUrl }
49- nickname = { nickname }
50- type = { type }
51- url = { url }
52- name = { name }
53- onClose = { ( ) => { } }
54- onDelete = { ( ) => { } }
55- />
65+ < SendbirdSdkContext . Provider value = { { } } >
66+ < FileViewer
67+ profileUrl = { profileUrl }
68+ nickname = { nickname }
69+ type = { type }
70+ url = { url }
71+ name = { name }
72+ onClose = { ( ) => { } }
73+ onDelete = { ( ) => { } }
74+ />
75+ </ SendbirdSdkContext . Provider >
5676 ) ;
57- expect (
58- container . getElementsByClassName ( 'sendbird-fileviewer__content__video' ) [ 0 ] . className
59- ) . not . toContain ( 'sendbird-fileviewer__content__img' ) ;
60- expect (
61- container . getElementsByClassName ( 'sendbird-fileviewer__content__video' ) [ 0 ] . className
62- ) . toBe ( 'sendbird-fileviewer__content__video' ) ;
63- expect (
64- container . getElementsByClassName ( 'sendbird-fileviewer__content__video' ) [ 0 ] . children [ 0 ] . src
65- ) . toEqual ( url ) ;
77+
78+ // Use document to search for the element inside the modal root
79+ const videoElement = document . querySelector ( `#${ MODAL_ROOT } .sendbird-fileviewer__content__video` ) ;
80+ if ( ! videoElement ) {
81+ throw new Error ( 'Video element not found' ) ;
82+ }
83+
84+ expect ( videoElement . className ) . not . toContain ( 'sendbird-fileviewer__content__img' ) ;
85+ expect ( videoElement . className ) . toBe ( 'sendbird-fileviewer__content__video' ) ;
86+
87+ const videoChild = videoElement . children [ 0 ] ;
88+ expect ( videoChild . src ) . toEqual ( url ) ;
6689 } ) ;
6790
68- it ( 'should handle unsupported msg' , function ( ) {
91+ it ( 'should handle unsupported msg' , function ( ) {
6992 const unsupportedMsg = { sender : { } } ;
7093 const profileUrl = '' ;
7194 const nickname = '' ;
@@ -75,25 +98,37 @@ describe('ui/FileViewer', () => {
7598 name = '' ,
7699 } = unsupportedMsg ;
77100 const { container } = render (
78- < FileViewer
79- profileUrl = { profileUrl }
80- nickname = { nickname }
81- type = { type }
82- url = { url }
83- name = { name }
84- onClose = { ( ) => { } }
85- onDelete = { ( ) => { } }
86- />
101+ < SendbirdSdkContext . Provider value = { { } } >
102+ < FileViewer
103+ profileUrl = { profileUrl }
104+ nickname = { nickname }
105+ type = { type }
106+ url = { url }
107+ name = { name }
108+ onClose = { ( ) => { } }
109+ onDelete = { ( ) => { } }
110+ />
111+ </ SendbirdSdkContext . Provider >
87112 ) ;
88- expect (
89- container . getElementsByClassName ( 'sendbird-fileviewer__content__unsupported' ) [ 0 ] . className
90- ) . toBe ( 'sendbird-fileviewer__content__unsupported' ) ;
91- expect (
92- container . getElementsByClassName ( 'sendbird-fileviewer__header__right' ) [ 0 ] . children [ 0 ] . className
93- ) . not . toBe ( 'sendbird-fileviewer__header__right__actions' ) ;
113+
114+ // Use document to search for the element inside the modal root
115+ const unsupportedElement = document . querySelector ( `#${ MODAL_ROOT } .sendbird-fileviewer__content__unsupported` ) ;
116+ if ( ! unsupportedElement ) {
117+ throw new Error ( 'Unsupported element not found' ) ;
118+ }
119+
120+ expect ( unsupportedElement . className ) . toBe ( 'sendbird-fileviewer__content__unsupported' ) ;
121+
122+ const headerRightElement = document . querySelector ( `#${ MODAL_ROOT } .sendbird-fileviewer__header__right` ) ;
123+ if ( ! headerRightElement ) {
124+ throw new Error ( 'Header right element not found' ) ;
125+ }
126+
127+ const headerRightActionElement = headerRightElement . children [ 0 ] ;
128+ expect ( headerRightActionElement . className ) . not . toBe ( 'sendbird-fileviewer__header__right__actions' ) ;
94129 } ) ;
95130
96- it ( 'should do a snapshot test of the FileViewer DOM' , function ( ) {
131+ it ( 'should do a snapshot test of the FileViewer DOM' , function ( ) {
97132 const {
98133 sender,
99134 type,
@@ -102,16 +137,18 @@ describe('ui/FileViewer', () => {
102137 } = msg0 ;
103138 const { profileUrl, nickname = '' } = sender ;
104139 const { asFragment } = render (
105- < FileViewer
106- profileUrl = { profileUrl }
107- nickname = { nickname }
108- type = { type }
109- url = { url }
110- name = { name }
111- onClose = { ( ) => { } }
112- onDelete = { ( ) => { } }
113- message = { msg0 }
114- />
140+ < SendbirdSdkContext . Provider value = { { } } >
141+ < FileViewer
142+ profileUrl = { profileUrl }
143+ nickname = { nickname }
144+ type = { type }
145+ url = { url }
146+ name = { name }
147+ onClose = { ( ) => { } }
148+ onDelete = { ( ) => { } }
149+ message = { msg0 }
150+ />
151+ </ SendbirdSdkContext . Provider >
115152 ) ;
116153 expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
117154 } ) ;
0 commit comments