@@ -213,4 +213,142 @@ describe("eo-modal", () => {
213213 document . body . removeChild ( element ) ;
214214 } ) ;
215215 } ) ;
216+
217+ test ( "modal without sidebar content" , async ( ) => {
218+ const element = document . createElement ( "eo-modal" ) as Modal ;
219+ element . modalTitle = "Modal Title" ;
220+
221+ const bodyElement = document . createElement ( "div" ) ;
222+ bodyElement . textContent = "This is main content" ;
223+
224+ act ( ( ) => {
225+ element . appendChild ( bodyElement ) ;
226+ document . body . appendChild ( element ) ;
227+ } ) ;
228+
229+ await act ( async ( ) => {
230+ element . open ( ) ;
231+ } ) ;
232+
233+ expect ( element . visible ) . toBeTruthy ( ) ;
234+
235+ // 没有 sidebar 内容时,不应该有 has-sidebar 类
236+ const modalElement = element . shadowRoot ?. querySelector ( ".modal" ) ;
237+ expect ( modalElement ?. classList . contains ( "has-sidebar" ) ) . toBeFalsy ( ) ;
238+
239+ // sidebar 应该存在但被隐藏
240+ const sidebarElement = element . shadowRoot ?. querySelector ( ".modal-sidebar" ) ;
241+ expect ( sidebarElement ) . toBeTruthy ( ) ;
242+
243+ act ( ( ) => {
244+ document . body . removeChild ( element ) ;
245+ } ) ;
246+ } ) ;
247+
248+ test ( "modal with sidebar content" , async ( ) => {
249+ const element = document . createElement ( "eo-modal" ) as Modal ;
250+ element . modalTitle = "Modal Title with Sidebar" ;
251+
252+ const bodyElement = document . createElement ( "div" ) ;
253+ bodyElement . textContent = "This is main content" ;
254+
255+ const sidebarElement = document . createElement ( "div" ) ;
256+ sidebarElement . slot = "sidebar" ;
257+ sidebarElement . textContent = "This is sidebar content" ;
258+
259+ act ( ( ) => {
260+ element . appendChild ( bodyElement ) ;
261+ element . appendChild ( sidebarElement ) ;
262+ document . body . appendChild ( element ) ;
263+ } ) ;
264+
265+ await act ( async ( ) => {
266+ element . open ( ) ;
267+ } ) ;
268+
269+ // 等待 slotchange 事件触发和状态更新
270+ await act ( async ( ) => {
271+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) ) ;
272+ } ) ;
273+
274+ expect ( element . visible ) . toBeTruthy ( ) ;
275+
276+ // 有 sidebar 内容时,应该有 has-sidebar 类
277+ const modalElement = element . shadowRoot ?. querySelector ( ".modal" ) ;
278+ expect ( modalElement ?. classList . contains ( "has-sidebar" ) ) . toBeTruthy ( ) ;
279+
280+ // 检查 sidebar 插槽是否正确渲染内容
281+ const sidebarSlot = element . shadowRoot ?. querySelector (
282+ 'slot[name="sidebar"]'
283+ ) as HTMLSlotElement ;
284+ expect ( sidebarSlot ) . toBeTruthy ( ) ;
285+ expect ( sidebarSlot ?. assignedElements ( ) . length ) . toBeGreaterThan ( 0 ) ;
286+
287+ // 验证 modal-main-content 存在
288+ const mainContent = element . shadowRoot ?. querySelector (
289+ ".modal-main-content"
290+ ) ;
291+ expect ( mainContent ) . toBeTruthy ( ) ;
292+
293+ act ( ( ) => {
294+ document . body . removeChild ( element ) ;
295+ } ) ;
296+ } ) ;
297+
298+ test ( "sidebar content dynamically added" , async ( ) => {
299+ const element = document . createElement ( "eo-modal" ) as Modal ;
300+ element . modalTitle = "Modal Title" ;
301+
302+ const bodyElement = document . createElement ( "div" ) ;
303+ bodyElement . textContent = "This is main content" ;
304+
305+ act ( ( ) => {
306+ element . appendChild ( bodyElement ) ;
307+ document . body . appendChild ( element ) ;
308+ } ) ;
309+
310+ await act ( async ( ) => {
311+ element . open ( ) ;
312+ } ) ;
313+
314+ // 初始没有 has-sidebar 类
315+ let modalElement = element . shadowRoot ?. querySelector ( ".modal" ) ;
316+ expect ( modalElement ?. classList . contains ( "has-sidebar" ) ) . toBeFalsy ( ) ;
317+
318+ // 动态添加 sidebar 内容
319+ const sidebarElement = document . createElement ( "div" ) ;
320+ sidebarElement . slot = "sidebar" ;
321+ sidebarElement . textContent = "Dynamically added sidebar" ;
322+
323+ await act ( async ( ) => {
324+ element . appendChild ( sidebarElement ) ;
325+ } ) ;
326+
327+ // 等待 slotchange 事件触发
328+ await act ( async ( ) => {
329+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) ) ;
330+ } ) ;
331+
332+ // 现在应该有 has-sidebar 类
333+ modalElement = element . shadowRoot ?. querySelector ( ".modal" ) ;
334+ expect ( modalElement ?. classList . contains ( "has-sidebar" ) ) . toBeTruthy ( ) ;
335+
336+ // 移除 sidebar 内容
337+ await act ( async ( ) => {
338+ element . removeChild ( sidebarElement ) ;
339+ } ) ;
340+
341+ // 等待 slotchange 事件触发
342+ await act ( async ( ) => {
343+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) ) ;
344+ } ) ;
345+
346+ // 应该移除 has-sidebar 类
347+ modalElement = element . shadowRoot ?. querySelector ( ".modal" ) ;
348+ expect ( modalElement ?. classList . contains ( "has-sidebar" ) ) . toBeFalsy ( ) ;
349+
350+ act ( ( ) => {
351+ document . body . removeChild ( element ) ;
352+ } ) ;
353+ } ) ;
216354} ) ;
0 commit comments