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

Add cypress tests #509

Closed
wants to merge 7 commits into from
Closed

Add cypress tests #509

wants to merge 7 commits into from

Conversation

vedhav
Copy link
Contributor

@vedhav vedhav commented Jan 3, 2024

A draft on adding cypress tests for teal.slice

How to run?

  1. Make sure node is installed.
  2. Go to the tests directory and run npm i to install the npm dependencies.
  3. You can run the test on all the module functions using the tests/test_e2e.R file. Make sure that the working directory is tests.
  4. You can also check a module individually by running this command in the shell: npm run test-e2e

Copy link
Contributor

@gogonzo gogonzo left a comment

Choose a reason for hiding this comment

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

I don't think testing framework should affect teal code. Isn't this better to get element by id instead of adding cy-* class?

@vedhav
Copy link
Contributor Author

vedhav commented Jan 17, 2024

Agree that the testing framework should not affect the core package code. However, in the future when some changes are made to the id or class they would break our tests, Adding the cy-* classes in the logic we test gives a signal to the developer modifying the class to think about fixing the tests too. If we detect the elements using id/class/data attributes Cypress recommends adding these cy_* to reduce flaky tests.
But ideally, cypress docs recommend implementing tests without any class/id etc by trying to find the elements by just visual indicators for example "Find the input element with label ADSL" but in our case, I was unable to figure out a way to implement tests without class selectors yet.
So, I'd say let's see if we can implement them without any class/id selection and fallback to this when we're sure it's not possible.

@gogonzo
Copy link
Contributor

gogonzo commented Jan 17, 2024

However, in the future when some changes are made to the id or class they would break our tests

Contrary, changing testing framework will leave some redundant class specification in the package.

Adding the cy-* classes in the logic we test gives a signal to the developer modifying the class to think about fixing the tests too

Fair point. On the other side any errors will be indicated by the cypress tests result.

Find the input element with label ADSL

We can use specific selectors to find the right elements. We will probably need to add class to the FilterState to distinct by the class of the filtered-variable (for numerics we have plot-slider, etc)

  • xpath selector //*[starts-with(@id, 'teal-main_ui-filter_panel-active-') and contains(@id, '-$dataname-') and contains(@id, '-inputs-')]
  • css selector [id^='teal-main_ui-filter_panel-active-'][id*='-$dataname-'][id*='-inputs-']

@gogonzo gogonzo self-assigned this Jan 26, 2024
beforeEach(() => {
cy.visit("http://127.0.0.1:5555/");
cy.waitForStabilityAndCatchError("body");
cy.get(".cy-active-summary-table tbody tr").as("activeSummary");
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like the fact id is duplicated with "cy_" prefix. I propose following convention: since we need to be aware that certain element is tested let's just add cy-testable class to the existing class (as a second class). Then selector for this like will be

Suggested change
cy.get(".cy-active-summary-table tbody tr").as("activeSummary");
cy.get(".cy-testable.teal_active_summary_filter_panel tbody tr").as("activeSummary");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍🏽 Let's try to find out if it's possible to implement the tests without any change to the classes in the package as we talked about. If unsuccessful, we can use this convention, I like it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I respect the idea of having a distinction between what's testable and what is not so app developer won't change this casually (I hope at least). It will be good to write down this convention somewhere that "testable" means don't touch it ;)

Also, I think we need to review all html classes to follow some "template". They seem to be random

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, I think we need to review all html classes to follow some "template". They seem to be random

Agreed

Copy link
Contributor

Choose a reason for hiding this comment

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

if testable is propagated in some good-practice guidelines then this should be ok

cy.waitForStabilityAndCatchError("body");

cy.get("@numericCard")
.get(".cy-numeric-selection-inputs input:first")
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking about parent-child naming convention for filters. In FilterState card can have a general class like filter-card, then RangeFilterState$ui_inputs will just have .numeric-filter-inputs.

Suggested change
.get(".cy-numeric-selection-inputs input:first")
.get(".filter-card > .numeric-filter > .cy-testable.filter-card input:first")

Let's sit down and design a complete wireframe for teal/teal.slice and let's think about classes/ids of each element

data_frame$date <- as.Date(data_frame$datetime)
})
datanames(data) <- c("data_frame", "iris")
app <- teal::init(
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be a generic shiny app instead of teal. Testing could be distorted by teal. I suggest to use shiny app from vignette. Should also test the output if and how data gets filtererd?

Copy link
Contributor

@gogonzo gogonzo 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:

  • .gitignore to not include cypress artifacts and node modules
  • .Rbuildignore to not include cypress at all

@m7pr
Copy link
Contributor

m7pr commented Feb 9, 2024

npm run test-e2e

Is ability to run node a blocker for potential future developers writing cypress tests? Are people from teams like SME be able to execute those?

image

I think we should not modify the class name, as tests will indicate something failed and then developer will realize that the change of the class id/name broke tests and this needs to be resolved on the test side. This is the same with regular R function names. If you change the function name then the test that assumed a specific function name will fail and then you will realize you need to edit tests. We do not prepend R function names with testthat_ to point that this function is covered by tests. What if at some point we have all classes covered by cypress - do we need to have cy- prefix then?

@@ -0,0 +1,150 @@
describe("data.frame filters", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

So for cypress tests you assume that there is a specific class id/name, so that's why you put all those divs( around in R code. Then you need to have node and know JavaScript to write tests? Is that right? Is JS part of CoreDev regular skillset?

@vedhav
Copy link
Contributor Author

vedhav commented May 6, 2024

Closing the PR as we chose to go with the shinytest2

@vedhav vedhav closed this May 6, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 6, 2024
@insights-engineering-bot insights-engineering-bot deleted the 447-introduce-cypress-tests branch May 12, 2024 03:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants