Skip to content

HT2 #23

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

HT2 #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17,656 changes: 17,656 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

31 changes: 7 additions & 24 deletions src/components/article-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,15 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Article from './article'
import accordion from '../decorators/accordion'
//import useAccordion from '../custom-hooks/accordion'

/*
export default function ArticleList({ articles, fetchAll }) {
const { openItemId, toggleOpenItem } = useAccordion()
useEffect(() => {
fetchAll()
})

const articleItems = articles.map((article) => (
<li key={article.id}>
<Article
article={article}
onBtnClick={toggleOpenItem(article.id)}
isOpen={article.id === openItemId}
/>
</li>
))

return <ul className="test--article-list__container">{articleItems}</ul>
}
*/

//import AccodrionComponent from './accordion-component'

class ArticleList extends Component {
static propTypes = {
fetchAll: PropTypes.func,
articles: PropTypes.array,
toggleOpenItem: PropTypes.func,
openItemId: PropTypes.number
}

componentDidMount() {
this.props.fetchAll && this.props.fetchAll()
}
Expand Down
44 changes: 42 additions & 2 deletions src/components/article-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Enzyme, { mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import ArticleList from './article-list'
import articles from '../fixtures'
import CommentList from './comment-list'

Enzyme.configure({ adapter: new Adapter() })

Expand Down Expand Up @@ -31,9 +32,48 @@ describe('ArticleList', () => {

it('should fetch all articles on mount', function() {
const fn = jest.fn()

mount(<ArticleList articles={articles} fetchAll={fn} />)

expect(fn.mock.calls.length).toBe(1)
})
})

describe('Article', () => {
it('after expand article it body should appear with button for expand comments with collapsed comments', () => {
const container = mount(<ArticleList articles={articles} />)
container
.find('.test--article__btn')
.at(0)
.simulate('click')

expect(container.find('.test--article__body').length).toBe(1)
expect(container.find('.test--commentlist__btn').length).toBe(1)
expect(container.find('.test--comment__body').length).toBe(0)
})
})

describe('CommentList', () => {
it('all comments should by collapsed after expand first article', () => {
const container = mount(<ArticleList articles={articles} />)
container
.find('.test--article__btn')
.at(0)
.simulate('click')

expect(container.find('.test--comment__body').length).toBe(0)
})

it('should open 5 comments on click in first article', () => {
const container = mount(<ArticleList articles={articles} />)
container
.find('.test--article__btn')
.at(0)
.simulate('click')

container
.find('.test--commentlist__btn')
.at(0)
.simulate('click')

expect(container.find('.test--comment__body').length).toBe(5)
})
})
22 changes: 11 additions & 11 deletions src/components/article.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import React, { Component } from 'react'
import { findDOMNode } from 'react-dom'
import CommentList from './comment-list'
import PropTypes from 'prop-types'

class Article extends Component {
/*
componentDidCatch(error) {
console.log('---', error)
}
static propTypes = {
article: PropTypes.shape({
title: PropTypes.string,
text: PropTypes.string,
comments: PropTypes.array
}),
isOpen: PropTypes.bool,
onBtnClick: PropTypes.func
}

*/
render() {
const { article, isOpen, onBtnClick } = this.props
return (
Expand All @@ -34,11 +38,7 @@ class Article extends Component {
)
}

setCommentsRef = (ref) => {
// window.comments = ref
// console.log('---', 'comments', ref)
// console.log('---', 'comments DOM', findDOMNode(ref))
}
setCommentsRef = (ref) => {}
}

export default Article
35 changes: 17 additions & 18 deletions src/components/comment-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Comment from './comment'
import toggleOpen from '../decorators/toggle-open'
import NewCommentForm from './new-comment-form'

class CommentList extends Component {
static propTypes = {
Expand All @@ -10,19 +11,16 @@ class CommentList extends Component {
toggleOpen: PropTypes.func
}

/*
static defaultProps = {
comments: []
}
*/

render() {
const { isOpen, toggleOpen } = this.props
const text = isOpen ? 'hide comments' : 'show comments'
return (
<div>
<button onClick={toggleOpen}>{text}</button>
<button onClick={toggleOpen} className="test--commentlist__btn">
{text}
</button>
{this.getBody()}
<NewCommentForm />
</div>
)
}
Expand All @@ -31,17 +29,18 @@ class CommentList extends Component {
const { comments, isOpen } = this.props
if (!isOpen) return null

const body = comments.length ? (
<ul ref={this.setListRef}>
{comments.map((comment) => (
<li key={comment.id}>
<Comment comment={comment} />
</li>
))}
</ul>
) : (
<h3>No comments yet</h3>
)
const body =
comments && comments.length ? (
<ul ref={this.setListRef}>
{comments.map((comment) => (
<li key={comment.id}>
<Comment comment={comment} />
</li>
))}
</ul>
) : (
<h3>No comments yet</h3>
)

return <div>{body}</div>
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'

function Comment({ comment }) {
return (
<div>
<div className="test--comment__body">
{comment.text} <b>by {comment.user}</b>
</div>
)
Expand Down
43 changes: 43 additions & 0 deletions src/components/new-comment-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { Component } from 'react'

class NewCommentForm extends Component {
state = {
comment: '',
username: ''
}

render() {
return (
<div>
<p>
Comment: <textarea value={this.state.comment} onChange={this.commentChangeHandler} />
</p>
<p>
Username: <input value={this.state.username} onChange={this.usernameChangeHandler} />
</p>
<p>
<button
onClick={this.postComment}
disabled={this.state.comment && this.state.username ? null : 'disabled'}
>
Post comment
</button>
</p>
</div>
)
}

postComment = () => {
console.log('sumbit')
}

commentChangeHandler = (evt) => {
this.setState({ comment: evt.target.value })
}

usernameChangeHandler = (evt) => {
this.setState({ username: evt.target.value })
}
}

export default NewCommentForm