Skip to content

Commit 3e851f5

Browse files
authored
fix: Enable golden-layout jest test and fix broken EventUtils test (#2467)
Noticed we had migrated a test into golden-layout, but it never ran. At a future point I then broke the test (not cleaning up event handlers on unmount), but it wasn't running so it wasn't caught. This enables the test and fixes the broken logic so the test passes.
1 parent 083df1f commit 3e851f5

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const baseConfig = require('../../jest.config.base.cjs');
2+
const packageJson = require('./package');
3+
4+
module.exports = {
5+
...baseConfig,
6+
displayName: packageJson.name,
7+
};

packages/golden-layout/src/utils/EventUtils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef } from 'react';
1+
import { useEffect, useRef } from 'react';
22
import EventEmitter from './EventEmitter';
33

44
type AsArray<P> = P extends unknown[] ? P : [P];
@@ -70,6 +70,14 @@ export function makeUseListenerFunction<TParameters = []>(
7070

7171
eventEmitterRef.current = eventEmitter;
7272
handlerRef.current = handler;
73+
74+
// Cleanup on unmount
75+
// Mounting the listener in useEffect causes a race condition with embed-widget
76+
// where the event is emitted during render before the useEffect runs after render.
77+
useEffect(
78+
() => () => eventEmitterRef.current?.off(event, handlerRef.current),
79+
[]
80+
);
7381
};
7482
}
7583

0 commit comments

Comments
 (0)