Skip to content

feat(e2e,playwright):migrate common components help button tests - #51628

Merged
ojeytonwilliams merged 7 commits into
freeCodeCamp:mainfrom
teamoio:e2e-migrating-common-components-help-button-tests
Sep 26, 2023
Merged

ojeytonwilliams merged 7 commits into
freeCodeCamp:mainfrom
teamoio:e2e-migrating-common-components-help-button-tests

Conversation

@Fahdkassim

@Fahdkassim Fahdkassim commented Sep 22, 2023 •

Copy link
Copy Markdown
Contributor

Checklist:

Closes #XXXXX

@Fahdkassim
Fahdkassim requested a review from a team September 22, 2023 06:01
@github-actions github-actions Bot added the platform: learn UI side of the client application that needs familiarity with React, Gatsby etc. label Sep 22, 2023
@ghost

ghost commented Sep 22, 2023 •

Copy link
Copy Markdown

👀 Review this PR in a CodeSee Review Map

View the CodeSee Map of this change

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

@Fahdkassim

Copy link
Copy Markdown
Contributor Author

Hi 👋 ,
I have migrated the tests for the help button component from Cypress to Playwright. Kindly review this PR and share your feedback.
@ojeytonwilliams @Ahmadkashif @raisedadead

@Ahmadkashif Ahmadkashif left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you so much for your time and effort to improve this @Fahdkassim

This is a solid effort from your side.

Comment thread e2e/help-button.spec.ts Resolved
@Ahmadkashif

Copy link
Copy Markdown
Member

@ojeytonwilliams @Fahdkassim @naomi-lgbt @raisedadead

I have some insights to share based on this part of our codebase. This PR by @Fahdkassim appears to be a 1:1 correlation in playwright for what we had for cypress, for the same component.

I suggest the following:

  1. Lets try and come up with a practice to never write standalone tests for individual components like buttons, drop-downs etc. These components should always be tested as part of the e2e test for the whole page.
  2. We can have a folder in e2e like e2e/lib/testUtils which can have the functions for testing common, re-usable components like buttons, drop-downs and others. This is what it can look like approximately:

file: e2e/lib/testUtils/helpButton.tsx

export const testHelpButton = async (page: Page) => {
   const helpButton = page.getByTestId('get-help-dropdown');
   await expect(helpButton).toBeVisible();
   await helpButton.click();
   await expect(page.getByTestId('get-hint')).toBeVisible();
   await expect(page.getByTestId('ask-for-help')).toBeVisible();
   await expect(page.getByTestId('watch-a-video')).toBeVisible();
}

Keeping this in mind, the actual e2e test looks something like this:

import { test, expect, type Page } from '@playwright/test';
import { testHelpButton } from './lib/testUtils/helpButton';

test.describe('help-button tests for a page with three links', () => {
  let page: Page;
  // visit the page with the video link
  test.beforeAll(async ({ browser }) => {
    page = await browser.newPage();
    await page.goto(
      '/learn/responsive-web-design/basic-html-and-html5/say-hello-to-html-elements'
    );
  });

  test.afterAll(async () => {
    await page.close();
  });
  test('should render the button, menu and the three links when video is available', async () => {
     await testHelpButton(page);
     // continue using this function across all relevant e2e tests, where ever the React component is being reused, we test it with this one call.
  })

What do you think fellas?

@naomi-lgbt

Copy link
Copy Markdown
Member

Definitely a fan of DRYing out the tests.

@raisedadead

Copy link
Copy Markdown
Member

Lets try and come up with a practice to never write standalone tests for individual components

Do you mean never write e2e tests for these? If yes, I agree. This is unrelated, but the thing is, our unit testing (jest) for the components is severely lacking and is mostly an after-thought.

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

I agree that we use e2e tests in situations where unit tests would be preferred, but I don't see the case for reusable tests like testHelpButton. Regardless of how we test it (e2e or unit), there's nothing to be gained repeating that test.

There is a case for DRYing out any kind of setup, though. If we have multiple tests that need to start from the same state, it can be helpful to DRY out the steps to reach that state so that it's easier to maintain.

@Ahmadkashif

Copy link
Copy Markdown
Member

@raisedadead @ojeytonwilliams

In terms of deriving best practices for others to follow, what do you guys suggest should be the way forward?

A, either we never test such UI in e2e tests, OR
B, we test them once in the whole process via unit or e2e tests, OR
C, we test the UI component everytime it appears in a page, while we write e2e tests for the page?

…he playwright config file to read the env variables

feat(e2e) pulled in main and resolved conflicts
…efactored the code for the tests to assert the visibility of the elements through the playwright test id
@Fahdkassim
Fahdkassim force-pushed the e2e-migrating-common-components-help-button-tests branch from 6071757 to fbfdfdb Compare September 26, 2023 06:00
@ojeytonwilliams

Copy link
Copy Markdown
Contributor

I don't want to be overly dogmatic, since it's quite subjective, but B) sounds the best to me.

Also, I wouldn't say anything is off-limits when it comes to e2e testing, it's just that we don't want the tests to be flaky or slow. So, as long as the e2e tests are reliable and fast, that's good enough for me.

@Fahdkassim

Copy link
Copy Markdown
Contributor Author

I've improved the code in the test file by implementing a DRY approach. I've created a reusable function that is now called in every test block within the file. This enhancement allows us to efficiently test multiple pages without duplicating the code. Additionally, I've placed the reusable function at the top of the file. If you'd prefer, I can also organise this function in a separate file within a directory like 'e2e/lib/testUtils.' Let me know your preference.
@Ahmadkashif @ojeytonwilliams

Comment thread e2e/help-button.spec.ts Resolved
Comment thread e2e/help-button.spec.ts Resolved
Comment thread e2e/help-button.spec.ts Resolved
Comment thread e2e/help-button.spec.ts Resolved
Comment thread e2e/help-button.spec.ts Resolved
@Fahdkassim

Copy link
Copy Markdown
Contributor Author

Changes applied

@ojeytonwilliams ojeytonwilliams left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @Fahdkassim, this is very clean and easy to read!

LGTM 👍

@Ahmadkashif Ahmadkashif left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving this, as we now have a consensus on how to move forward.

Thanks for your time @Fahdkassim @ojeytonwilliams @raisedadead 👍

@Fahdkassim

Copy link
Copy Markdown
Contributor Author

Awesome 🙂 . I guess if there are no more blockers this PR should be merged 🚀

@ojeytonwilliams

Copy link
Copy Markdown
Contributor

Yep, it's all good. Thanks everyone!

@ojeytonwilliams
ojeytonwilliams merged commit fc29d1d into freeCodeCamp:main Sep 26, 2023
@Sembauke Sembauke mentioned this pull request May 28, 2024
61 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform: learn UI side of the client application that needs familiarity with React, Gatsby etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants