From 5f45f5ed0e3848a1e5c1a8b4bcfcf46f16f33730 Mon Sep 17 00:00:00 2001 From: Michael Odusanya <65921601+dengaku12@users.noreply.github.com> Date: Tue, 25 Aug 2020 11:23:30 -0400 Subject: [PATCH] Fixed save button behavior (#9238) * Fixed save button, but it's slow to update when clicking unsave * Update app/javascript/articles/components/SaveButton.jsx Co-authored-by: Nick Taylor * Update app/javascript/articles/components/SaveButton.jsx Co-authored-by: Nick Taylor * Update app/javascript/articles/components/SaveButton.jsx Co-authored-by: Nick Taylor * I added the suggested changes but the issue still persists * Added tests. Co-authored-by: Nick Taylor Co-authored-by: Nick Taylor --- .../articles/components/SaveButton.jsx | 16 ++-- .../components/__tests__/SaveButton.test.jsx | 87 +++++++++++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 app/javascript/articles/components/__tests__/SaveButton.test.jsx diff --git a/app/javascript/articles/components/SaveButton.jsx b/app/javascript/articles/components/SaveButton.jsx index 11beea1c7..386eb5c70 100644 --- a/app/javascript/articles/components/SaveButton.jsx +++ b/app/javascript/articles/components/SaveButton.jsx @@ -8,15 +8,16 @@ export class SaveButton extends Component { const { isBookmarked } = props; - this.state = { buttonText: isBookmarked ? 'Saved' : 'Save' }; + this.state = { + buttonText: isBookmarked ? 'Saved' : 'Save', + }; } render() { const { buttonText } = this.state; - const { article, isBookmarked, onClick } = this.props; - const mouseOver = (_e) => { + const mouseMove = (_e) => { this.setState({ buttonText: isBookmarked ? 'Unsave' : 'Save' }); }; @@ -26,7 +27,10 @@ export class SaveButton extends Component { const handleClick = (_e) => { onClick(_e); - this.setState({ buttonText: isBookmarked ? 'Unsave' : 'Saved' }); + this.setState({ + buttonText: isBookmarked ? 'Save' : 'Saved', + isBookmarked: !isBookmarked, + }); }; if (article.class_name === 'Article') { @@ -39,8 +43,8 @@ export class SaveButton extends Component { data-initial-feed data-reactable-id={article.id} onClick={handleClick} - onMouseOver={mouseOver} - onFocus={mouseOver} + onMouseMove={mouseMove} + onFocus={mouseMove} onMouseout={mouseOut} onBlur={mouseOut} > diff --git a/app/javascript/articles/components/__tests__/SaveButton.test.jsx b/app/javascript/articles/components/__tests__/SaveButton.test.jsx new file mode 100644 index 000000000..40245e59b --- /dev/null +++ b/app/javascript/articles/components/__tests__/SaveButton.test.jsx @@ -0,0 +1,87 @@ +import { h } from 'preact'; +import { fireEvent, render } from '@testing-library/preact'; +import { axe } from 'jest-axe'; +import { SaveButton } from '../SaveButton'; + +it('should have no a11y violations', async () => { + const article = { class_name: 'Article', id: 1 }; + const { container } = render(); + const results = await axe(container); + + expect(results).toHaveNoViolations(); +}); + +it('should render button as bookmarked', () => { + const article = { class_name: 'Article', id: 1 }; + const { queryByText } = render(); + const saveButton = queryByText('Saved'); + + expect(saveButton).not.toBeNull(); +}); + +it('should button as not being bookmarked', () => { + const article = { class_name: 'Article', id: 1 }; + const { queryByText } = render( + , + ); + const saveButton = queryByText('Save'); + + expect(saveButton).not.toBeNull(); +}); + +it('should bookmark when it previously was not', async () => { + const article = { class_name: 'Article', id: 1 }; + const { getByText, findByText } = render( + , + ); + const saveButton = getByText('Save'); + saveButton.click(); + + const savedButton = await findByText('Saved'); + + expect(savedButton).not.toBeNull(); +}); + +it('should unbookmark when it previously was bookmarked', async () => { + const article = { class_name: 'Article', id: 1 }; + const { getByText, findByText } = render( + , + ); + + const savedButton = getByText('Saved'); + savedButton.click(); + + const saveButton = await findByText('Save'); + + expect(saveButton).not.toBeNull(); +}); + +it('should change text to unbookmark when hovering over button and it is bookmarked', async () => { + const article = { class_name: 'Article', id: 1 }; + const { getByText, findByText } = render( + , + ); + + const savedButton = getByText('Saved'); + fireEvent.mouseMove(savedButton); + + const saveButton = await findByText('Unsave'); + + expect(saveButton).not.toBeNull(); +}); + +it('should not change button text when hovering over button and it is not bookmarked', async () => { + const article = { class_name: 'Article', id: 1 }; + const { getByText, findByText } = render( + , + ); + + const savedButton = getByText('Save'); + fireEvent.mouseMove(savedButton); + + // We're checking for the same button text again because we need to find the element again + // again after the mouse has moved over the button. + const saveButton = await findByText('Save'); + + expect(saveButton).not.toBeNull(); +});