Commit adc5bad3 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Do not render profile modal contents when the modal is closed

parent f13d9fd3
...@@ -14,15 +14,11 @@ type ProfileModalProps = { ...@@ -14,15 +14,11 @@ type ProfileModalProps = {
export default function ProfileModal({ eventBus, config }: ProfileModalProps) { export default function ProfileModal({ eventBus, config }: ProfileModalProps) {
const [isHidden, setIsHidden] = useState(true); const [isHidden, setIsHidden] = useState(true);
const emitterRef = useRef<Emitter | null>(null); const emitterRef = useRef<Emitter | null>(null);
// Used only to track when was this modal first open, delaying the iframe to
// be loaded until strictly necessary.
const [hasOpened, setHasOpened] = useState(false);
useEffect(() => { useEffect(() => {
const emitter = eventBus.createEmitter(); const emitter = eventBus.createEmitter();
emitter.subscribe('openProfile', () => { emitter.subscribe('openProfile', () => {
setIsHidden(false); setIsHidden(false);
setHasOpened(true);
}); });
emitterRef.current = emitter; emitterRef.current = emitter;
...@@ -36,16 +32,13 @@ export default function ProfileModal({ eventBus, config }: ProfileModalProps) { ...@@ -36,16 +32,13 @@ export default function ProfileModal({ eventBus, config }: ProfileModalProps) {
emitterRef.current?.publish('closeProfile'); emitterRef.current?.publish('closeProfile');
}; };
if (!hasOpened) { if (isHidden) {
return null; return null;
} }
return ( return (
<div <div
className={classnames( className="fixed z-max top-0 left-0 right-0 bottom-0 p-3 bg-black/50"
'fixed z-max top-0 left-0 right-0 bottom-0 p-3 bg-black/50',
{ hidden: isHidden }
)}
data-testid="profile-outer" data-testid="profile-outer"
> >
<div className="relative w-full h-full" data-testid="profile-inner"> <div className="relative w-full h-full" data-testid="profile-inner">
......
...@@ -34,7 +34,7 @@ describe('ProfileModal', () => { ...@@ -34,7 +34,7 @@ describe('ProfileModal', () => {
components.forEach(component => component.unmount()); components.forEach(component => component.unmount());
}); });
it('does not render anything before the modal has been opened at least once', () => { it('does not render anything while the modal is closed', () => {
const wrapper = createComponent(); const wrapper = createComponent();
assert.equal(wrapper.find(outerSelector).length, 0); assert.equal(wrapper.find(outerSelector).length, 0);
}); });
...@@ -45,29 +45,25 @@ describe('ProfileModal', () => { ...@@ -45,29 +45,25 @@ describe('ProfileModal', () => {
emitter.publish('openProfile'); emitter.publish('openProfile');
wrapper.update(); wrapper.update();
const outer = wrapper.find(outerSelector); assert.isTrue(wrapper.find(outerSelector).exists());
assert.isFalse(outer.hasClass('hidden'));
const iframe = wrapper.find('iframe'); const iframe = wrapper.find('iframe');
assert.equal(iframe.prop('src'), profileURL); assert.equal(iframe.prop('src'), profileURL);
}); });
it('hides modal on closing', () => { it("removes the modal's content on closing", () => {
const wrapper = createComponent(); const wrapper = createComponent();
emitter.publish('openProfile'); emitter.publish('openProfile');
wrapper.update(); wrapper.update();
let outer = wrapper.find(outerSelector); assert.isTrue(wrapper.find(outerSelector).exists());
assert.isFalse(outer.hasClass('hidden'));
act(() => { act(() => {
wrapper.find('IconButton').prop('onClick')(); wrapper.find('IconButton').prop('onClick')();
}); });
wrapper.update(); wrapper.update();
outer = wrapper.find(outerSelector); assert.isFalse(wrapper.find(outerSelector).exists());
assert.isTrue(outer.hasClass('hidden'));
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment