|
1 | 1 | import {describe, expect, it} from 'vitest' |
2 | | -import Portal, {registerPortalRoot} from '../Portal/index' |
| 2 | +import Portal, {registerPortalRoot, PortalContext} from '../Portal/index' |
3 | 3 |
|
4 | 4 | import {render} from '@testing-library/react' |
5 | 5 | import BaseStyles from '../BaseStyles' |
| 6 | +import React from 'react' |
6 | 7 |
|
7 | 8 | describe('Portal', () => { |
8 | 9 | it('renders a default portal into document.body (no BaseStyles present)', () => { |
@@ -100,4 +101,86 @@ describe('Portal', () => { |
100 | 101 |
|
101 | 102 | baseElement.innerHTML = '' |
102 | 103 | }) |
| 104 | + |
| 105 | + it('renders into custom portal when PortalContext is supplied with portalContainerName', () => { |
| 106 | + // Create and register a custom portal root |
| 107 | + const customPortalRoot = document.createElement('div') |
| 108 | + customPortalRoot.id = 'customContextPortal' |
| 109 | + document.body.appendChild(customPortalRoot) |
| 110 | + registerPortalRoot(customPortalRoot, 'customContext') |
| 111 | + |
| 112 | + const toRender = ( |
| 113 | + <PortalContext.Provider value={{portalContainerName: 'customContext'}}> |
| 114 | + <Portal>context-portal-content</Portal> |
| 115 | + </PortalContext.Provider> |
| 116 | + ) |
| 117 | + |
| 118 | + render(toRender) |
| 119 | + |
| 120 | + expect(customPortalRoot.textContent.trim()).toEqual('context-portal-content') |
| 121 | + |
| 122 | + // Cleanup |
| 123 | + document.body.removeChild(customPortalRoot) |
| 124 | + }) |
| 125 | + |
| 126 | + it('renders into default portal when PortalContext does not specify portalContainerName', () => { |
| 127 | + const toRender = ( |
| 128 | + <PortalContext.Provider value={{}}> |
| 129 | + <Portal>default-portal-content</Portal> |
| 130 | + </PortalContext.Provider> |
| 131 | + ) |
| 132 | + |
| 133 | + const {baseElement} = render(toRender) |
| 134 | + const generatedRoot = baseElement.querySelector('#__primerPortalRoot__') |
| 135 | + |
| 136 | + expect(generatedRoot).toBeInstanceOf(HTMLElement) |
| 137 | + expect(generatedRoot?.textContent.trim()).toEqual('default-portal-content') |
| 138 | + |
| 139 | + baseElement.innerHTML = '' |
| 140 | + }) |
| 141 | + |
| 142 | + it('renders into default portal when PortalContext portalContainerName is undefined', () => { |
| 143 | + const toRender = ( |
| 144 | + <PortalContext.Provider value={{portalContainerName: undefined}}> |
| 145 | + <Portal>undefined-context-content</Portal> |
| 146 | + </PortalContext.Provider> |
| 147 | + ) |
| 148 | + |
| 149 | + const {baseElement} = render(toRender) |
| 150 | + const generatedRoot = baseElement.querySelector('#__primerPortalRoot__') |
| 151 | + |
| 152 | + expect(generatedRoot).toBeInstanceOf(HTMLElement) |
| 153 | + expect(generatedRoot?.textContent.trim()).toEqual('undefined-context-content') |
| 154 | + |
| 155 | + baseElement.innerHTML = '' |
| 156 | + }) |
| 157 | + |
| 158 | + it('containerName prop overrides PortalContext portalContainerName', () => { |
| 159 | + // Create and register custom portal roots |
| 160 | + const contextPortalRoot = document.createElement('div') |
| 161 | + contextPortalRoot.id = 'contextPortal' |
| 162 | + document.body.appendChild(contextPortalRoot) |
| 163 | + registerPortalRoot(contextPortalRoot, 'contextPortal') |
| 164 | + |
| 165 | + const propPortalRoot = document.createElement('div') |
| 166 | + propPortalRoot.id = 'propPortal' |
| 167 | + document.body.appendChild(propPortalRoot) |
| 168 | + registerPortalRoot(propPortalRoot, 'propPortal') |
| 169 | + |
| 170 | + const toRender = ( |
| 171 | + <PortalContext.Provider value={{portalContainerName: 'contextPortal'}}> |
| 172 | + <Portal containerName="propPortal">prop-overrides-context</Portal> |
| 173 | + </PortalContext.Provider> |
| 174 | + ) |
| 175 | + |
| 176 | + render(toRender) |
| 177 | + |
| 178 | + // Should render in the portal specified by the prop, not the context |
| 179 | + expect(propPortalRoot.textContent.trim()).toEqual('prop-overrides-context') |
| 180 | + expect(contextPortalRoot.textContent.trim()).toEqual('') |
| 181 | + |
| 182 | + // Cleanup |
| 183 | + document.body.removeChild(contextPortalRoot) |
| 184 | + document.body.removeChild(propPortalRoot) |
| 185 | + }) |
103 | 186 | }) |
0 commit comments