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

Convert ExpandedStoryNotes to be a functional component #883

Merged
merged 7 commits into from
Nov 6, 2023
Original file line number Diff line number Diff line change
@@ -1,77 +1,60 @@
import React, { Fragment } from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import NotesList from '../note/NotesList';
import { editingStoryPropTypesShape } from '../../../models/beta/story';
import ExpandedStorySection from './ExpandedStorySection';

class ExpandedStoryNotes extends React.Component {
constructor(props) {
super(props);
const ExpandedStoryNotes = ({ story, onCreate, onDelete, disabled }) => {
const [value, setValue] = useState('');

this.state = {
value: ''
};

this.handleChange = this.handleChange.bind(this);
this.handleSave = this.handleSave.bind(this);
const handleChange = (event) => {
setValue(event.target.value);
};

handleChange(event) {
this.setState({ value: event.target.value })
}

handleSave() {
const { onCreate } = this.props;
const handleSave = () => {
onCreate(value);
setValue('');
};

onCreate(this.state.value);
this.setState({ value: '' });
}
const hasAnEmptyValue = () => {
return !value.trim();
};

hasAnEmptyValue() {
return !this.state.value.trim()
}
const notesForm = () => (
<>
<textarea
className="form-control input-sm create-note-text"
value={value}
onChange={handleChange}
/>

notesForm() {
return (
<Fragment>
<textarea
className="form-control input-sm create-note-text"
value={this.state.value}
onChange={this.handleChange}
<div className='create-note-button'>
<input
type='button'
value={I18n.t('add note')}
onClick={handleSave}
disabled={hasAnEmptyValue()}
/>
</div>
</>
);

<div className='create-note-button'>
<input
type='button'
value={I18n.t('add note')}
onClick={this.handleSave}
disabled={this.hasAnEmptyValue()}
/>
</div>
</Fragment>
);
}
if(disabled && !story.notes.length) return null;

render() {
const { story, onDelete, disabled } = this.props;

if(disabled && !story.notes.length) return null

return (
<ExpandedStorySection
title={I18n.t('story.notes')}
identifier="notes"
>
<NotesList
notes={story.notes}
onDelete={onDelete}
disabled={disabled}
/>
return (
<ExpandedStorySection
title={I18n.t('story.notes')}
identifier="notes"
>
<NotesList
notes={story.notes}
onDelete={onDelete}
disabled={disabled}
/>

{!disabled && this.notesForm()}
</ExpandedStorySection>
);
}
{!disabled && notesForm()}
</ExpandedStorySection>
);
};

ExpandedStoryNotes.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ describe('<ExpandedStoryNotes />', () => {
let onCreate;
let onDelete;

const newStory = notes => ({
const newStory = (notes) => ({
...storyFactory({ notes: notes || [] }),
_editing: {
...storyFactory({ notes: notes || [] })
}
})
...storyFactory({ notes: notes || [] }),
},
});

beforeEach(() => {
onCreate = sinon.stub();
onDelete = sinon.stub();
})
});

it('renders component title', () => {
const story = newStory();
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('<ExpandedStoryNotes />', () => {

const onCreateSpy = sinon.spy();

const wrapper = shallow(
const wrapper = mount(
Copy link
Member

Choose a reason for hiding this comment

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

What was the reason for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tests were breaking with shallow rendering, so I used mount to access the entire component.

<ExpandedStoryNotes
story={story}
onCreate={onCreateSpy}
Expand All @@ -83,7 +83,7 @@ describe('<ExpandedStoryNotes />', () => {

it('disables the add note button if text area is empty', () => {
const story = newStory();

const wrapper = shallow(
<ExpandedStoryNotes
story={story}
Expand All @@ -92,20 +92,20 @@ describe('<ExpandedStoryNotes />', () => {
disabled={false}
/>
);

const textArea = wrapper.find('.create-note-text');
const button = wrapper.find('.create-note-button input');

textArea.simulate('change', { target: { value: '' } });
expect(button.prop('disabled')).toBe(true);
});
});

describe('when component is disabled', () => {
it('does not render a create note button', () => {
const notes = [{id: 0, note: 'foo'}]
const notes = [{ id: 0, note: 'foo' }];
const story = newStory(notes);

const wrapper = shallow(
<ExpandedStoryNotes
story={story}
Expand All @@ -114,14 +114,14 @@ describe('<ExpandedStoryNotes />', () => {
disabled={true}
/>
);

expect(wrapper.exists('.create-note-button')).toBe(false);
});

it('does not render a create note text area', () => {
const notes = [{id: 0, note: 'foo'}]
const notes = [{ id: 0, note: 'foo' }];
const story = newStory(notes);

const wrapper = shallow(
<ExpandedStoryNotes
story={story}
Expand All @@ -130,14 +130,14 @@ describe('<ExpandedStoryNotes />', () => {
disabled={true}
/>
);

expect(wrapper.exists('.create-note-text')).toBe(false);
});

describe('when there are no notes', () => {
it('renders nothing', () => {
const story = newStory();

const wrapper = shallow(
<ExpandedStoryNotes
story={story}
Expand All @@ -152,4 +152,3 @@ describe('<ExpandedStoryNotes />', () => {
});
});
});

Loading