Skip to content

test: migrate Editors tests to RTL #1625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2025
Merged

Conversation

erikian
Copy link
Member

@erikian erikian commented Aug 4, 2024

Ref: #1408.

@erikian erikian requested review from codebytere and a team as code owners August 4, 2024 02:28
@coveralls
Copy link

coveralls commented Aug 4, 2024

Coverage Status

coverage: 80.201% (+0.04%) from 80.161%
when pulling dc72c78 on test/rtl-editors-component
into 714aca9 on main.

@erikian erikian force-pushed the test/rtl-editors-component branch 2 times, most recently from 5a02bbf to 9449eec Compare August 4, 2024 03:18
Copy link
Member

@dsanders11 dsanders11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As much as possible we should avoid relying on testid, to best align with the guiding principles of Testing Library. I think with a bit of refactoring we can avoid it in this case (which would remove the need for TestIdContainer in this PR).

diff --git a/rtl-spec/components/editors.spec.tsx b/rtl-spec/components/editors.spec.tsx
index 887c3433..1928e721 100644
--- a/rtl-spec/components/editors.spec.tsx
+++ b/rtl-spec/components/editors.spec.tsx
@@ -5,6 +5,7 @@ import { App } from '../../src/renderer/app';
 import { Editors } from '../../src/renderer/components/editors';
 import { Editor, EditorMosaic } from '../../src/renderer/editor-mosaic';
 import { AppState } from '../../src/renderer/state';
+import { getEditorTitle } from '../../src/renderer/utils/editor-utils';
 import {
   MonacoEditorMock,
   StateMock,
@@ -14,7 +15,7 @@ import { emitEvent } from '../../tests/utils';
 import { renderClassComponentWithInstanceRef } from '../test-utils/renderClassComponentWithInstanceRef';
 
 jest.mock('../../src/renderer/components/editor', () => ({
-  Editor: () => 'Editor',
+  Editor: () => 'EditorComponent',
 }));
 
 describe('Editors component', () => {
@@ -42,7 +43,7 @@ describe('Editors component', () => {
   it('renders', () => {
     const { renderResult } = renderEditors();
 
-    expect(renderResult.getByTestId('editors')).toBeInTheDocument();
+    expect(renderResult.getAllByText('EditorComponent')).not.toHaveLength(0);
   });
 
   it('does not execute command if not supported', () => {
@@ -91,19 +92,16 @@ describe('Editors component', () => {
   it('renders toolbars', () => {
     const { renderResult } = renderEditors();
 
-    const [
-      mainToolbar,
-      rendererToolbar,
-      htmlToolbar,
-      preloadToolbar,
-      stylesheetToolbar,
-    ] = renderResult.getAllByTestId('editors-toolbar');
-
-    expect(mainToolbar).toHaveTextContent('Main Process (main.js)');
-    expect(rendererToolbar).toHaveTextContent('Renderer Process (renderer.js)');
-    expect(htmlToolbar).toHaveTextContent('HTML (index.html)');
-    expect(preloadToolbar).toHaveTextContent('Preload (preload.js)');
-    expect(stylesheetToolbar).toHaveTextContent('Stylesheet (styles.css)');
+    const toolbars = renderResult.getAllByRole('toolbar');
+    const editors = Object.keys(editorValues);
+    expect(editors).not.toHaveLength(0);
+
+    for (const editorId of editors) {
+      const title = getEditorTitle(editorId as EditorId);
+      expect(
+        toolbars.find((toolbar) => toolbar.textContent?.includes(title)),
+      ).toBeInTheDocument();
+    }
   });
 
   it('onChange() updates the mosaic arrangement in the appState', () => {
diff --git a/src/renderer/components/editors.tsx b/src/renderer/components/editors.tsx
index c1cb0a7a..16050ea4 100644
--- a/src/renderer/components/editors.tsx
+++ b/src/renderer/components/editors.tsx
@@ -192,7 +192,7 @@ export const Editors = observer(
       const { appState } = this.props;
 
       return (
-        <div data-testid="editors-toolbar">
+        <div role="toolbar">
           {/* Left */}
           <div>
             <h5>{title}</h5>

There's an additional change in there to avoid relying on the order of the editors when checking for the toolbars.

@erikian erikian force-pushed the test/rtl-editors-component branch from 9449eec to d1e4367 Compare July 29, 2025 12:00
@erikian
Copy link
Member Author

erikian commented Jul 29, 2025

There's an additional change in there to avoid relying on the order of the editors when checking for the toolbars.

The proposed change checks if the rendered toolbars that we just got from the document with getAllByRole are still in the document, which should always be true, so we wouldn't be able to tell if some of the expected toolbars is missing.

I replaced that with a lookup for the specific toolbar titles inside the toolbars that are present in the document, which should also eliminate any issues with ordering. We could also just check if we have a certain number of toolbars (or any number larger than 0) if we don't want to hard-code the toolbar titles.

@erikian erikian requested a review from dsanders11 July 29, 2025 12:12
@dsanders11
Copy link
Member

@erikian, after your most recent changes, is TestIdContainer still needed here?

@erikian
Copy link
Member Author

erikian commented Jul 30, 2025

@erikian, after your most recent changes, is TestIdContainer still needed here?

It's not needed in editors.tsx — removed. I'm keeping it around for welcome-tour.tsx, though, since that component was already using a testid anyway and we'll probably need to resort to testid again at some point.

@dsanders11 dsanders11 merged commit 99ddf98 into main Jul 30, 2025
8 checks passed
@dsanders11 dsanders11 deleted the test/rtl-editors-component branch July 30, 2025 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy