584
await utils.actAsync(() => context.selectFiber(childID, 'Child'));
585
expect(inspectedElementID).toBe(parentID);
586
});
587
+
588
+ it('should toggle profiling when the keyboard shortcut is pressed', async () => {
589
+ // Context providers
590
+ const Profiler =
591
+ require('react-devtools-shared/src/devtools/views/Profiler/Profiler').default;
592
+ const {
593
+ TimelineContextController,
594
+ } = require('react-devtools-timeline/src/TimelineContext');
595
+ const {
596
+ SettingsContextController,
597
+ } = require('react-devtools-shared/src/devtools/views/Settings/SettingsContext');
598
+ const {
599
+ ModalDialogContextController,
600
+ } = require('react-devtools-shared/src/devtools/views/ModalDialog');
601
+
602
+ // Dom component for profiling to be enabled
603
+ const Component = () => null;
604
+ utils.act(() => render(<Component />));
605
+
606
+ const profilerContainer = document.createElement('div');
607
+ document.body.appendChild(profilerContainer);
608
+
609
+ // Create a root for the profiler
610
+ const profilerRoot = ReactDOMClient.createRoot(profilerContainer);
611
+
612
+ // Render the profiler
613
+ utils.act(() => {
614
+ profilerRoot.render(
615
+ <Contexts>
616
+ <SettingsContextController browserTheme="light">
617
+ <ModalDialogContextController>
618
+ <TimelineContextController>
619
+ <Profiler />
620
+ </TimelineContextController>
621
+ </ModalDialogContextController>
622
+ </SettingsContextController>
623
+ </Contexts>,
624
+ );
625
+ });
626
+
627
+ // Verify that the profiler is not profiling.
628
+ expect(store.profilerStore.isProfilingBasedOnUserInput).toBe(false);
629
+
630
+ // Trigger the keyboard shortcut.
631
+ const ownerWindow = profilerContainer.ownerDocument.defaultView;
632
+ const isMac =
633
+ typeof navigator !== 'undefined' &&
634
+ navigator.platform.toUpperCase().indexOf('MAC') >= 0;
635
+
636
+ const keyEvent = new KeyboardEvent('keydown', {
637
+ key: 'e',
638
+ metaKey: isMac,
639
+ ctrlKey: !isMac,
640
+ bubbles: true,
641
+ });
642
+
643
+ // Dispatch keyboard event to toggle profiling on
644
+ // Try utils.actAsync with recursivelyFlush=false
645
+ await utils.actAsync(() => {
646
+ ownerWindow.dispatchEvent(keyEvent);
647
+ }, false);
648
+ expect(store.profilerStore.isProfilingBasedOnUserInput).toBe(true);
649
+
650
+ // Dispatch keyboard event to toggle profiling off
651
+ await utils.actAsync(() => {
652
+ ownerWindow.dispatchEvent(keyEvent);
653
+ }, false);
654
+ expect(store.profilerStore.isProfilingBasedOnUserInput).toBe(false);
655
+
656
+ document.body.removeChild(profilerContainer);
657
+ });
658
});