|
| 1 | +import { act, renderHook } from '@testing-library/react-native'; |
| 2 | +import { modalStackManager, useModalStack } from '../../components/common/ModalStackManager'; |
| 3 | + |
| 4 | +describe('ModalStackManager & useModalStack', () => { |
| 5 | + beforeEach(() => { |
| 6 | + // Reset manager stack state before each test |
| 7 | + modalStackManager.clear(); |
| 8 | + }); |
| 9 | + |
| 10 | + describe('ModalStackManager Class (Core Stacking Logic)', () => { |
| 11 | + it('should correctly push and pop modals', () => { |
| 12 | + expect(modalStackManager.getStack()).toEqual([]); |
| 13 | + |
| 14 | + modalStackManager.push('modal-1'); |
| 15 | + expect(modalStackManager.getStack()).toEqual(['modal-1']); |
| 16 | + |
| 17 | + modalStackManager.push('modal-2'); |
| 18 | + expect(modalStackManager.getStack()).toEqual(['modal-1', 'modal-2']); |
| 19 | + |
| 20 | + modalStackManager.pop('modal-1'); |
| 21 | + expect(modalStackManager.getStack()).toEqual(['modal-2']); |
| 22 | + |
| 23 | + modalStackManager.pop('modal-2'); |
| 24 | + expect(modalStackManager.getStack()).toEqual([]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should auto-assign correct z-index values', () => { |
| 28 | + // Base z-index is 10000, step is 10 |
| 29 | + expect(modalStackManager.getZIndex('unregistered')).toBe(10000); |
| 30 | + |
| 31 | + modalStackManager.push('modal-1'); |
| 32 | + expect(modalStackManager.getZIndex('modal-1')).toBe(10000); |
| 33 | + |
| 34 | + modalStackManager.push('modal-2'); |
| 35 | + expect(modalStackManager.getZIndex('modal-1')).toBe(10000); |
| 36 | + expect(modalStackManager.getZIndex('modal-2')).toBe(10010); |
| 37 | + |
| 38 | + modalStackManager.push('modal-3'); |
| 39 | + expect(modalStackManager.getZIndex('modal-3')).toBe(10020); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should correctly identify the top-most modal', () => { |
| 43 | + expect(modalStackManager.isTop('modal-1')).toBe(false); |
| 44 | + |
| 45 | + modalStackManager.push('modal-1'); |
| 46 | + expect(modalStackManager.isTop('modal-1')).toBe(true); |
| 47 | + |
| 48 | + modalStackManager.push('modal-2'); |
| 49 | + expect(modalStackManager.isTop('modal-1')).toBe(false); |
| 50 | + expect(modalStackManager.isTop('modal-2')).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should handle duplicate push operations by moving the item to the top', () => { |
| 54 | + modalStackManager.push('modal-1'); |
| 55 | + modalStackManager.push('modal-2'); |
| 56 | + modalStackManager.push('modal-3'); |
| 57 | + expect(modalStackManager.getStack()).toEqual(['modal-1', 'modal-2', 'modal-3']); |
| 58 | + |
| 59 | + // Pushing existing 'modal-1' should move it to the top |
| 60 | + modalStackManager.push('modal-1'); |
| 61 | + expect(modalStackManager.getStack()).toEqual(['modal-2', 'modal-3', 'modal-1']); |
| 62 | + expect(modalStackManager.isTop('modal-1')).toBe(true); |
| 63 | + expect(modalStackManager.getZIndex('modal-1')).toBe(10020); |
| 64 | + expect(modalStackManager.getZIndex('modal-2')).toBe(10000); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('useModalStack Hook', () => { |
| 69 | + it('should register modal when visible and unregister on unmount', () => { |
| 70 | + const { result, unmount } = renderHook(({ visible }) => useModalStack('hook-modal', visible), { |
| 71 | + initialProps: { visible: true }, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(result.current.zIndex).toBe(10000); |
| 75 | + expect(result.current.isTop).toBe(true); |
| 76 | + expect(modalStackManager.getStack()).toEqual(['hook-modal']); |
| 77 | + |
| 78 | + unmount(); |
| 79 | + expect(modalStackManager.getStack()).toEqual([]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should reactively update when visibility changes', () => { |
| 83 | + const { result, rerender } = renderHook(({ visible }) => useModalStack('hook-modal', visible), { |
| 84 | + initialProps: { visible: false }, |
| 85 | + }); |
| 86 | + |
| 87 | + expect(result.current.isTop).toBe(false); |
| 88 | + expect(modalStackManager.getStack()).toEqual([]); |
| 89 | + |
| 90 | + rerender({ visible: true }); |
| 91 | + expect(result.current.zIndex).toBe(10000); |
| 92 | + expect(result.current.isTop).toBe(true); |
| 93 | + expect(modalStackManager.getStack()).toEqual(['hook-modal']); |
| 94 | + |
| 95 | + rerender({ visible: false }); |
| 96 | + expect(result.current.isTop).toBe(false); |
| 97 | + expect(modalStackManager.getStack()).toEqual([]); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should assign and update z-index and isTop for multiple stacked modals', () => { |
| 101 | + const { result: modal1 } = renderHook(({ visible }) => useModalStack('modal-a', visible), { |
| 102 | + initialProps: { visible: true }, |
| 103 | + }); |
| 104 | + |
| 105 | + expect(modal1.current.zIndex).toBe(10000); |
| 106 | + expect(modal1.current.isTop).toBe(true); |
| 107 | + |
| 108 | + // Now open modal B |
| 109 | + const { result: modal2, unmount: unmountB } = renderHook(({ visible }) => useModalStack('modal-b', visible), { |
| 110 | + initialProps: { visible: true }, |
| 111 | + }); |
| 112 | + |
| 113 | + // Modal B should be top |
| 114 | + expect(modal2.current.zIndex).toBe(10010); |
| 115 | + expect(modal2.current.isTop).toBe(true); |
| 116 | + |
| 117 | + // Modal A should no longer be top |
| 118 | + expect(modal1.current.zIndex).toBe(10000); |
| 119 | + expect(modal1.current.isTop).toBe(false); |
| 120 | + |
| 121 | + // Close modal B |
| 122 | + act(() => { |
| 123 | + unmountB(); |
| 124 | + }); |
| 125 | + |
| 126 | + // Modal A should become top again |
| 127 | + expect(modal1.current.zIndex).toBe(10000); |
| 128 | + expect(modal1.current.isTop).toBe(true); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}); |
0 commit comments