Skip to content
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

Unit test for browsing page #2851

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

shubham-html-css-js
Copy link
Contributor

Configured Package.json to support unit testing without interfering with Patternfly icon import and wrote unit tests for some browsing page components

@shubham-html-css-js
Copy link
Contributor Author

I don't know why it still says I want to push 30 commits even though all other commits have been merged(I am still learning github),but we have previous commits merged,so you just have to review the latest commit related to unit test

Copy link
Member

@portante portante left a comment

Choose a reason for hiding this comment

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

30 commits for an initial PR might indicate that the starting point was not from the already merged code base in branch main.

Merge conflicts are being reported for this PR, so perhaps we could have it rebased?

Copy link
Member

@dbutenhof dbutenhof left a comment

Choose a reason for hiding this comment

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

Great start, Shubham! Thanks for working on the unit tests; it's not always as much fun as new features, but it's important!

It sounds like you pushed this PR from a local branch where you'd already been working, without rebasing against main. The rebase should make them go away, so long as they really were merged.

I usually create a new branch from upstream main when starting a PR instead of re-using one; if you reuse, be sure you update your main and rebase first.

@@ -0,0 +1,30 @@
import { Provider } from "react-redux";
import store from "store/store";
import { MOCK_DATA } from "utils/mockData";
Copy link
Member

Choose a reason for hiding this comment

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

This is specifically a mock for /api/v1/datasets/list with specific metadata requested. I think it would be better to give this a more specific name than just "MOCK_DATA", especially with the suggestion that a file named "mockData.js" might provide mocks for other APIs as well?

@webbnh
Copy link
Member

webbnh commented May 18, 2022

I don't know why it still says I want to push 30 commits even though all other commits have been merged(I am still learning github),but we have previous commits merged,so you just have to review the latest commit related to unit test

Based on the GitHub "Network" graph on the "Insights" page, it would appear that you created your unit_test_browsing_page branch from the end of your browsing_page_dashboard_dev branch, and that has about 30 commits on it since it branched from main. So, your PR, which requests merging the unit_test_browsing_page branch into main, has 30 commits to merge. Now, hopefully, the contents of all but the last of them would have already been present, so the merge would be a no-op, but that's not evident until the merge is attempted (and, even if it would be a no-op in the end, the individual merges might have been painful). Now, you have since merged main into your unit_test_browsing_page branch, which should make the subsequent merge into main fairly painless, but this isn't the usual route that we take.

When you start new work, you should start a new branch, and generally that branch should start from the current end of the main branch. (So, before checking out and creating your new branch, you should issue a command like git remote update (and, I like to add the --prune switch to remove local tracking branches for upstream branches which have been deleted). And, then, you should make sure that it's the main branch that you're checking out from. (You can reuse local development branches, but there's generally no need to, and it doesn't generally fit well with our preference for a "linear history".)

Second, you generally should not merge from main to your development branch. If there is something on main that you need, or if your development branch is otherwise out of date, you should rebase your development branch, in effect "moving" the root of it to the new end of the main branch, as thought it had been checked out from there initially. (This process has effects similar to merging from the main branch (including the possibility of having to manually resolve merge conflicts), but it produces a different, linear history, and it leaves your branch in a state where it can be rebased again, as needed.)

As for your present situation, there are several ways that you could recover. One would be to reset your branch back prior to the merge from main, and then rebase it, changing the root from browsing_page_dashboard_dev branch to the end of main (this is a fairly advanced but fairly straightforward operation). But, since you've only got one commit that you're interested in, the simplest thing would be to create a new branch rooted at the end of main and "cherry-pick" the one commit from your old branch on to it.

If you rename your old branch first, and then create the new branch with the old name, I believe that you'll be able to force-push it to your fork and it will replace the branch there...and the PR will be updated with the result.

Copy link
Member

@webbnh webbnh left a comment

Choose a reason for hiding this comment

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

Shubham, this is a great start!

However, I'm thinking that this PR should be split: there are a number of pre-cursor changes which are not directly related to unit testing, although they provide features which the unit testing depends on; they should be pulled out into a separate PR which this PR should be based on.

You do a lot of searching for UI elements by various bits of text. These searches seem to be done via case-blind regular expressions containing literal strings. It seems like we should know the exact text, so regular expressions (particularly the case-mapping) shouldn't be necessary. And, it also seems like there should be a more rigorous way of addressing these elements than by picking bits of UI text...but, since that's what the user actually sees, perhaps that's just the thing, but...it seems odd to me.

And, I've got a few other nits, comments, suggestions, and questions.

dashboard/package.json Show resolved Hide resolved
@@ -10,7 +10,7 @@ import { TableWithFavorite } from 'modules/components/TableComponent';
function App() {
useEffect(() => {
const faviconLogo = document.getElementById("favicon");
faviconLogo.setAttribute("href", favicon);
faviconLogo?.setAttribute("href", favicon);
Copy link
Member

Choose a reason for hiding this comment

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

This change doesn't seem like it should be in a unit testing commit/PR.

@@ -0,0 +1,26 @@
import { Provider } from "react-redux";
import store from "store/store";
import App from "../../../App";
Copy link
Member

Choose a reason for hiding this comment

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

In this context, do we have a "root" configured, so that we don't have to use relative import paths? (I.e., can we just import this from "App" or from ./Appbecause the root issrc`?) Ditto for the other modules.

test("Alert message is displayed on initial load", () => {
render(<AppWrapper />);
const alert = screen.getByText(/want to see your own data/i);
expect(alert).toBeInTheDocument();
Copy link
Member

Choose a reason for hiding this comment

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

Are there any other attributes that we should be checking?

@@ -22,7 +22,7 @@ function SearchBox({
placeholder="Search Controllers"
onChange={(e) => setControllerValue(e)}
></TextInput>
<Button variant="control" onClick={searchController}>
<Button variant="control" onClick={searchController} aria-label="searchButton">
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem like a unit-test-related change.

test("data is filtered based on date range selected from date picker", async () => {
render(<AppWrapper />);
await screen.findByText("dhcp1");
const datePickerInput = screen.getAllByPlaceholderText(/yyyy-mm-dd/i);
Copy link
Member

Choose a reason for hiding this comment

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

Can you target them by aria-label value? That seems like a more robust discriminator.

render(<AppWrapper />);
await screen.findByText("dhcp1");
const datePickerInput = screen.getAllByPlaceholderText(/yyyy-mm-dd/i);
fireEvent.change(datePickerInput[0], { target: { value: "2022-02-16" } });
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we be checking that the UI has updated from YYYY-MM-DD to 2022-02-16?

Also, can the code here look at the values in the React-store? I think those would be good things to check, as well.

@shubham-html-css-js shubham-html-css-js force-pushed the unit_test_browsing_page branch 2 times, most recently from fa14411 to 0f1d0ee Compare May 24, 2022 13:04
Copy link
Member

@dbutenhof dbutenhof left a comment

Choose a reason for hiding this comment

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

Thanks, Shubham! I can resolve some of my previous conversations, but Webb has some good points/questions and I'd prefer to see a better name for the mocked data.

@@ -0,0 +1,32 @@
export const MOCK_DATA = [
Copy link
Member

Choose a reason for hiding this comment

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

I'm still a bit concerned about the generic name MOCK_DATA ... this mocks the response of /datasets/list, but we'll certainly want to have other mocks in the future to test additional parts of the dashboard.

Copy link
Member

Choose a reason for hiding this comment

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

The /datasets/list response format has changed. There's now a "results" field in the JSON as well as a "next_url" for paging. Also, the dashboard now relies on the resource_id.

{
  "next_url": "",
  "results": [
    {
      "metadata": {
        "dataset.created": "2022-07-27T15:39:06+00:00"
      },
      "name": "pbench-user-benchmark_xyz.22_2022.07.27T15.39.06",
      "resource_id": "c54ab5929dc6230937402b682751c1ac"
    },
    {
      "metadata": {
        "dataset.created": "2022-07-27T15:01:57+00:00"
      },
      "name": "Short timer",
      "resource_id": "8c19617fc80580eaf6bb6808a1f8c46f"
    },
    {
      "metadata": {
        "dataset.created": "2022-07-27T13:45:02+00:00"
      },
      "name": "Soon FOUR",
      "resource_id": "4e28f76ccc9572f7e6a27898678db39d"
    },
    {
      "metadata": {
        "dataset.created": "2022-07-27T13:45:11+00:00"
      },
      "name": "Soonish",
      "resource_id": "892e506d3302ebaa466b9bc4c02f1780"
    },
    {
      "metadata": {
        "dataset.created": "2022-07-20T16:28:32+00:00"
      },
      "name": "Soon ONE",
      "resource_id": "6a4b55f64f8dd5f997be0da70a5e0d2c"
    },
    {
      "metadata": {
        "dataset.created": "2022-07-27T13:44:53+00:00"
      },
      "name": "Soon THREE",
      "resource_id": "e368132b16f8a45407c77c0a54a427fb"
    }
  ],
  "total": 6
}

@dbutenhof
Copy link
Member

You need to git fetch upstream main and git rebase (or equivalent) to merge your package.json changes with the latest on main, then git push --force origin unit_test_browsing_page (that's a long branch name!)

@webbnh
Copy link
Member

webbnh commented May 25, 2022

You need to git fetch upstream main

I've come to prefer git remote update --prune, but I guess git fetch works too, if you only want to update the single branch. (One of the things that --prune does is remind me to delete my development branches after they've been merged into main.)

that's a long branch name!

I don't know about you, but I've got Git's command line "completer" configured, so I just type a couple of characters and then tab-complete the branch name...so it doesn't matter how long it is. 😉

Copy link
Member

@webbnh webbnh left a comment

Choose a reason for hiding this comment

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

It looks like you got the extraneous commits off your branch (yay!), but apparently your branch has conflicts in dashboard/package.json, so it cannot be merged, and the CI tests are refusing to run; so you should address that as soon as possible. (E.g., update your local tracking branch for main, rebase your branch on the end of main again, and resolve the conflicts during the rebase.)

Otherwise, it's just small stuff, but there are some previous comments of mine which you haven't addressed, yet.

Comment on lines +29 to +38
const datasetNameOne = screen.queryByText("pbench_user_benchmark1");
const datasetNameTwo = screen.queryByText("pbench_user_benchmark2");
const datasetNameThree = screen.queryByText("pbench_user_benchmark3");
const datasetNameFour = screen.queryByText("pbench_user_benchmark4");
const datasetNameFive = screen.queryByText("pbench_user_benchmark5");
expect(datasetNameOne).toBeInTheDocument();
expect(datasetNameTwo).toBeInTheDocument();
expect(datasetNameThree).toBeInTheDocument();
expect(datasetNameFour).not.toBeInTheDocument();
expect(datasetNameFive).not.toBeInTheDocument();
Copy link
Member

Choose a reason for hiding this comment

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

It looks to me like these assertions will fit on one line, so there's no need to use local variables for them (in-lining the expressions will cut the number of new lines of code in half...).

  expect(screen.queryByText("pbench_user_benchmark1")).toBeInTheDocument();
...
  expect(screen.queryByText("pbench_user_benchmark5")).not.toBeInTheDocument();

Copy link
Member

Choose a reason for hiding this comment

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

A similar comment applies to dashboard/src/modules/components/TableComponent/Table.test.js, and perhaps elsewhere.

waitFor,
fireEvent,
} = require("@testing-library/react");
const { render, screen, fireEvent } = require("@testing-library/react");
Copy link
Member

Choose a reason for hiding this comment

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

Teach me something: why do we use require() here instead of import?

@@ -51,7 +51,7 @@ const TableWithFavorite = () => {
dispatch(getFavoritedDatasets());
}, [dispatch]);

const { publicData, favoriteRepoNames } = useSelector(
const { publicData, favoriteRepoNames,tableData } = useSelector(
Copy link
Member

Choose a reason for hiding this comment

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

Looks like you need to run Prettier again. 🙃

It would be good to develop a habit of running Prettier before each commit.

dashboard/package.json Show resolved Hide resolved
@@ -30,7 +30,7 @@
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Copy link
Member

Choose a reason for hiding this comment

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

You've got some superfluous blank characters, here.

@portante
Copy link
Member

Please consider https://opensource.com/article/20/11/multiple-git-repositories. This gives us all a common way to organize and work together using the same names.

You need to git fetch upstream main

While it is nice to target one branch, it is more often helpful to keep track of everything in upstream (when defined as recommended in the blog above), since that avoid missing the creation of remote branches for shared projects or releases.

and git rebase (or equivalent) to merge your package.json changes with the latest on main, then git push --force origin unit_test_browsing_page (that's a long branch name!)

You can also use git push origin +unit_test_browsing_page where the leading + means --force.

@dbutenhof
Copy link
Member

While it is nice to target one branch, it is more often helpful to keep track of everything in upstream (when defined as recommended in the blog above), since that avoid missing the creation of remote branches for shared projects or releases.

Yup; but I don't use most of those branches routinely, and keeping main up to date is important. I tend to update remotes with --prune once in a while (maybe slightly more often than git gc), while I use git fetch upstream main regularly.

You can also use git push origin +unit_test_browsing_page where the leading + means --force.

Now that I did not know, and that's useful...

Copy link
Member

@portante portante left a comment

Choose a reason for hiding this comment

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

Please update commit log message with Jira issue ID "PBENCH-708".

@shubham-html-css-js shubham-html-css-js self-assigned this Jun 5, 2022
@shubham-html-css-js shubham-html-css-js added Dashboard Of and relating to the Dashboard GUI Unit tests testing labels Jun 5, 2022
@portante portante added this to the v0.72 milestone Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dashboard Of and relating to the Dashboard GUI testing Unit tests
Projects
Status: To Do
Development

Successfully merging this pull request may close these issues.

4 participants