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

DG download "remove all" button disables once pressed #1177 #1184

Merged
merged 3 commits into from Mar 29, 2022

Conversation

sam-glendenning
Copy link
Contributor

Description

Once pressed, the remove all button is disabled and displays an indeterminate loading spinner until the request completes. This prevents excessive requests as the user was previously able to click this button several times, which could cause an error.

I found it difficult to get Jest's fake timers to work properly, which resulted in a unit test that isn't the best but works for now. I think this is due to multiple levels of promises trying to resolve and becoming conflicted. So anytime I try to use jest.useFakeTimers() I get an exceeded timeout error. See my comment below which points out the test in question. Any feedback on how to properly test this would be appreciated!

Testing instructions

This behaviour usually only presents itself when there are a large number of items in the cart. Try adding a lot of individual datafiles to the cart. This will hopefully allow for the new behaviour to display for long enough to see in action.

  • Review code
  • Check Actions build
  • Review changes to test coverage

Agile board tracking

Closes #1177

Once pressed, the remove all button is disabled and displays an indeterminate loading spinner until the request completes.
@sam-glendenning sam-glendenning added bug Something isn't working MVP needed for minimum viable product datagateway-download Issues relating to the download plugin user feedback Issues that were raised by users labels Mar 28, 2022
Comment on lines 271 to 276
setTimeout(() => {
wrapper.update();
expect(wrapper.exists('[data-testid="no-selections-message"]')).toBe(
true
);
}, 2001);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what I'm not a fan of. It manually adds a non-mocked timeout to the test to force the wrapper to update and display the correct thing on the DOM. We want to use fake timers to adhere to good testing practice. Any help on this one would be appreciated!

Copy link
Member

Choose a reason for hiding this comment

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

Jest's fake timers and promises inherently don't work well with one another - see https://github.com/ral-facilities/scigateway/blob/169b596d761fc7426136faca946db692849f3f19/src/App.test.tsx#L32 for an example of me working around it. See this issue for info: jestjs/jest#10221. One option is to try using the legacy Jest fake timers, as for example there are a few tests in SG which work with old timers and not with the new ones (like the one I linked). However, the only way I could get things working was to manually wait for real timers. Perhaps using a dedicated sleep function or even just a sleep line of code like await new Promise((r) => setTimeout(r, 2000)); is more semantic, so you use that line then you wrapper.update/assert to your heart's content rather than within the timeout itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for that, I'll use that example as a reference for another attempt. I had a go with the legacy timers, to no avail. Perhaps this will help. I'll ping you again when I have something ready for review :)

@codecov
Copy link

codecov bot commented Mar 28, 2022

Codecov Report

Merging #1184 (9fd64e9) into master (ac9ec6a) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1184   +/-   ##
=======================================
  Coverage   97.79%   97.80%           
=======================================
  Files         131      131           
  Lines        6677     6683    +6     
  Branches     1967     1968    +1     
=======================================
+ Hits         6530     6536    +6     
  Misses        134      134           
  Partials       13       13           
Flag Coverage Δ
common 98.11% <ø> (ø)
dataview 98.03% <ø> (ø)
download 96.87% <100.00%> (+0.01%) ⬆️
search 97.51% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...d/src/downloadCart/downloadCartTable.component.tsx 98.71% <100.00%> (+0.05%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ac9ec6a...9fd64e9. Read the comment docs.

Having to use real timers to simulate a hanging request still but the code is a little cleaner
@louise-davies
Copy link
Member

As discussed - I'll likely be refactoring this again in #1185 so I'll take a look at the unit test again there

@sam-glendenning sam-glendenning merged commit a689cc4 into master Mar 29, 2022
@sam-glendenning sam-glendenning deleted the bugfix/remove-all-button-disables-#1177 branch March 29, 2022 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working datagateway-download Issues relating to the download plugin MVP needed for minimum viable product user feedback Issues that were raised by users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disable Remove All button in download table after it's clicked when the request is processing
2 participants