From 94c431afb0954693030d33f9d2738f5149c3c090 Mon Sep 17 00:00:00 2001 From: Viviane Dias <9057801+vivianedias@users.noreply.github.com> Date: Thu, 26 May 2022 08:59:06 -0300 Subject: [PATCH] [single-article] Refactor class components to be functional component (#17681) * refactor(single-article): migrates class to use pure components * test(single-article): removes repeated test from file --- .../__tests__/singleArticle.test.jsx | 39 ++---- .../modCenter/singleArticle/index.jsx | 130 ++++++++---------- 2 files changed, 74 insertions(+), 95 deletions(-) diff --git a/app/javascript/modCenter/singleArticle/__tests__/singleArticle.test.jsx b/app/javascript/modCenter/singleArticle/__tests__/singleArticle.test.jsx index 91055d516..b698a544f 100644 --- a/app/javascript/modCenter/singleArticle/__tests__/singleArticle.test.jsx +++ b/app/javascript/modCenter/singleArticle/__tests__/singleArticle.test.jsx @@ -1,8 +1,8 @@ -/* eslint-disable jest/expect-expect */ import { h, Fragment } from 'preact'; import { axe } from 'jest-axe'; import { render, getNodeText, waitFor } from '@testing-library/preact'; import { SingleArticle } from '../index'; +import '@testing-library/jest-dom'; const getTestArticle = () => ({ id: 1, @@ -39,9 +39,10 @@ describe('', () => { }); it('renders the article title', () => { - const { queryByText } = render( + const articleProps = getTestArticle(); + const { getByRole } = render( - + ', () => { , ); - expect(queryByText(getTestArticle().title)).toBeDefined(); - }); - - it('renders the new clickable article title', () => { - const { container } = render( - - - - , - ); - const text = getNodeText( - container.getElementsByClassName('article-title-heading')[0], - ); - expect(text).toContain(getTestArticle().title); + expect( + getByRole('heading', { name: articleProps.title, level: 3 }), + ).toBeInTheDocument(); }); it('renders the tags', () => { - const { queryByText } = render( + const { getByText } = render( ', () => { , ); - expect(queryByText('discuss')).toBeDefined(); - expect(queryByText('javascript')).toBeDefined(); - expect(queryByText('beginners')).toBeDefined(); + expect(getByText('discuss')).toBeInTheDocument(); + expect(getByText('javascript')).toBeInTheDocument(); + expect(getByText('beginners')).toBeInTheDocument(); }); it('renders no tags or # symbol when article has no tags', () => { @@ -145,7 +132,7 @@ describe('', () => { }); it('renders the correct formatted published date', () => { - const { queryByText } = render( + const { getByText } = render( ', () => { , ); - expect(queryByText('Jun 22')).toBeDefined(); + expect(getByText('Jun 22')).toBeInTheDocument(); }); it('renders the correct formatted published date as a time if the date is the same day', () => { diff --git a/app/javascript/modCenter/singleArticle/index.jsx b/app/javascript/modCenter/singleArticle/index.jsx index 9c0fcd0a7..4f1086fc1 100644 --- a/app/javascript/modCenter/singleArticle/index.jsx +++ b/app/javascript/modCenter/singleArticle/index.jsx @@ -1,17 +1,23 @@ import PropTypes from 'prop-types'; -import { h, Component, Fragment } from 'preact'; +import { h, Fragment } from 'preact'; import { createPortal } from 'preact/compat'; import { FlagUserModal } from '../../packs/flagUserModal'; import { formatDate } from './util'; -export class SingleArticle extends Component { - activateToggle = () => { - const { id, title, path, toggleArticle } = this.props; +export const SingleArticle = ({ + id, + title, + publishedAt, + cachedTagList, + user, + key, + articleOpened, + path, + toggleArticle, +}) => { + const activateToggle = () => toggleArticle(id, title, path); - toggleArticle(id, title, path); - }; - - tagsFormat = (tag, key) => { + const tagsFormat = (tag, key) => { if (tag) { return ( @@ -22,69 +28,55 @@ export class SingleArticle extends Component { } }; - render() { - const { - id, - title, - publishedAt, - cachedTagList, - user, - key, - articleOpened, - path, - } = this.props; - const tags = cachedTagList.split(', ').map((tag) => { - this.tagsFormat(tag, key); - }); + const tags = cachedTagList.split(', ').map((tag) => tagsFormat(tag, key)); - const newAuthorNotification = user.articles_count <= 3 ? '👋 ' : ''; - const modContainer = id - ? document.getElementById(`mod-iframe-${id}`) - : document.getElementById('mod-container'); + const newAuthorNotification = user.articles_count <= 3 ? '👋 ' : ''; + const modContainer = id + ? document.getElementById(`mod-iframe-${id}`) + : document.getElementById('mod-container'); - return ( - - {modContainer && - createPortal( - , - document.getElementsByClassName('flag-user-modal-container')[0], - )} - - - - - - - {title} - - - {tags} - - - {newAuthorNotification} - {user.name} - - - {formatDate(publishedAt)} - - - - - - - ); - } -} + return ( + + {modContainer && + createPortal( + , + document.getElementsByClassName('flag-user-modal-container')[0], + )} + + + + + + + {title} + + + {tags} + + + {newAuthorNotification} + {user.name} + + + {formatDate(publishedAt)} + + + + + + + ); +}; SingleArticle.propTypes = { id: PropTypes.number.isRequired,