Skip to content

Commit 0f96d4a

Browse files
committed
portal container
1 parent 2a45b4d commit 0f96d4a

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

src/components/Portal/Portal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ export class Portal extends PureComponent<PortalProps, State> {
6868
if (this.portalTargetEl && this.context != null) {
6969
const {cssCustomProperties, textColor} = this.context;
7070
if (cssCustomProperties != null) {
71-
const style = `${cssCustomProperties};color:${textColor};`;
71+
const style = textColor
72+
? `${cssCustomProperties};color:${textColor};`
73+
: `${cssCustomProperties}`;
7274
this.portalTargetEl.setAttribute('style', style);
7375
} else {
7476
this.portalTargetEl.removeAttribute('style');

src/components/Portal/tests/Portal.test.tsx

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, {createRef} from 'react';
2-
import {mount} from 'test-utilities';
2+
import {mount, mountWithApp} from 'test-utilities';
33
// eslint-disable-next-line no-restricted-imports
44
import {mountWithAppProvider} from 'test-utilities/legacy';
55

6-
import {Portal} from '../Portal';
6+
import {Portal, UNIQUE_CONTAINER_ID} from '../Portal';
77
import {portal} from '../../shared';
88

99
jest.mock('react-dom', () => ({
@@ -28,6 +28,41 @@ describe('<Portal />', () => {
2828
createPortalSpy.mockRestore();
2929
});
3030

31+
describe('container', () => {
32+
it('creates a portal container with the UNIQUE_CONTAINER_ID', () => {
33+
const appendChildSpy = jest.spyOn(document.body, 'appendChild');
34+
mountWithApp(<Portal />);
35+
expect(appendChildSpy).toHaveBeenLastCalledWith(
36+
expect.objectContaining({
37+
id: UNIQUE_CONTAINER_ID,
38+
}),
39+
);
40+
appendChildSpy.mockReset();
41+
});
42+
43+
it('sets CSS custom properties on the portal node', () => {
44+
const setSpy = jest.spyOn(Element.prototype, 'setAttribute');
45+
const portal = mountWithAppProvider(<Portal />, {
46+
features: {newDesignLanguage: true},
47+
theme: {
48+
colors: {surface: '#000000'},
49+
},
50+
});
51+
52+
expect(setSpy).toHaveBeenLastCalledWith(
53+
'style',
54+
portal.context().cssCustomProperties,
55+
);
56+
setSpy.mockRestore();
57+
});
58+
59+
it('removes CSS custom properties from the portal node', () => {
60+
const removeSpy = jest.spyOn(Element.prototype, 'removeAttribute');
61+
mountWithAppProvider(<Portal />);
62+
expect(removeSpy).toHaveBeenCalledWith('style');
63+
});
64+
});
65+
3166
describe('children', () => {
3267
it('get passed into the portal creation method', () => {
3368
const children = <div />;
@@ -104,25 +139,4 @@ describe('<Portal />', () => {
104139
mount(<Portal />);
105140
}).not.toThrow();
106141
});
107-
108-
it('sets CSS custom properties on the portal node', () => {
109-
const setSpy = jest.spyOn(Element.prototype, 'setAttribute');
110-
const portal = mountWithAppProvider(<Portal />, {
111-
features: {newDesignLanguage: true},
112-
theme: {
113-
colors: {surface: '#000000'},
114-
},
115-
});
116-
117-
expect(setSpy).toHaveBeenCalledWith(
118-
'style',
119-
portal.context().cssCustomProperties,
120-
);
121-
});
122-
123-
it('removes CSS custom properties from the portal node', () => {
124-
const removeSpy = jest.spyOn(Element.prototype, 'removeAttribute');
125-
mountWithAppProvider(<Portal />);
126-
expect(removeSpy).toHaveBeenCalledWith('style');
127-
});
128142
});

0 commit comments

Comments
 (0)