Skip to content

Commit

Permalink
fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dereje1 committed Jan 17, 2023
1 parent 1502141 commit 98edd13
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
50 changes: 26 additions & 24 deletions tests/client/src/components/modal/TagsForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe('The tags form component', () => {
props = {
addTag: jest.fn(),
closeTagsForm: jest.fn(),
suggestedTags: ['suggested tag 1', 'suggested tag 2'],
exisitingTags: ['existing tag 1', 'existing tag 2'],
};
});

Expand All @@ -23,52 +25,52 @@ describe('The tags form component', () => {

test('will update the value for characters <= 15', () => {
const wrapper = shallow(<TagsForm {...props} />);
let textField = wrapper.find('ForwardRef(TextField)');
expect(textField.props().value).toBe('');
let autoComplete = wrapper.find('ForwardRef(Autocomplete)');
expect(autoComplete.props().value).toBe('');
// trigger value change
textField.props().onChange({ target: { value: '15 characters..' } });
textField = wrapper.find('ForwardRef(TextField)');
expect(textField.props().value).toBe('15 characters..');
autoComplete.props().onInputChange('', '15 characters..');
autoComplete = wrapper.find('ForwardRef(Autocomplete)');
expect(autoComplete.props().value).toBe('15 characters..');
});

test('will not update the value for characters > 15', () => {
const wrapper = shallow(<TagsForm {...props} />);
let textField = wrapper.find('ForwardRef(TextField)');
expect(textField.props().value).toBe('');
let autoComplete = wrapper.find('ForwardRef(Autocomplete)');
expect(autoComplete.props().value).toBe('');
// trigger value change
textField.props().onChange({ target: { value: '16 characters..and more' } });
textField = wrapper.find('ForwardRef(TextField)');
expect(textField.props().value).toBe('');
autoComplete.props().onInputChange('', '16 characters..and more');
autoComplete = wrapper.find('ForwardRef(Autocomplete)');
expect(autoComplete.props().value).toBe('');
});

test('will submit on done if value is present', () => {
const wrapper = shallow(<TagsForm {...props} />);
const textField = wrapper.find('ForwardRef(TextField)');
const autoComplete = wrapper.find('ForwardRef(Autocomplete)');
// // trigger value change
textField.props().onChange({ target: { value: '15 characters..' } });
autoComplete.props().onInputChange('', '15 characters..');
const done = wrapper.find('Memo(ForwardRef(DoneIcon))');
// trigger submit
done.props().onMouseDown({ preventDefault: jest.fn() });
done.props().onClick();
expect(props.addTag).toHaveBeenCalledWith('15 characters..');
expect(props.addTag).toHaveBeenCalledWith('15 CHARACTERS..');
});

test('will submit on enter key if value is present', () => {
const wrapper = shallow(<TagsForm {...props} />);
let textField = wrapper.find('ForwardRef(TextField)');
let autoComplete = wrapper.find('ForwardRef(Autocomplete)');
// // trigger value change
textField.props().onChange({ target: { value: '15 characters..' } });
autoComplete.props().onInputChange('', '15 characters..');
// trigger enter
textField = wrapper.find('ForwardRef(TextField)');
textField.props().onKeyDown({ key: 'Enter' });
expect(props.addTag).toHaveBeenCalledWith('15 characters..');
autoComplete = wrapper.find('ForwardRef(Autocomplete)');
autoComplete.props().onKeyDown({ key: 'Enter' });
expect(props.addTag).toHaveBeenCalledWith('15 CHARACTERS..');
});

test('will not submit on done if value is empty', () => {
const wrapper = shallow(<TagsForm {...props} />);
const textField = wrapper.find('ForwardRef(TextField)');
const autoComplete = wrapper.find('ForwardRef(Autocomplete)');
// // trigger value change
textField.props().onChange({ target: { value: ' ' } });
autoComplete.props().onInputChange('', ' ');
const done = wrapper.find('Memo(ForwardRef(DoneIcon))');
// trigger submit
done.props().onMouseDown({ preventDefault: jest.fn() });
Expand All @@ -78,12 +80,12 @@ describe('The tags form component', () => {

test('will not submit on any other key even if value is present', () => {
const wrapper = shallow(<TagsForm {...props} />);
let textField = wrapper.find('ForwardRef(TextField)');
let autoComplete = wrapper.find('ForwardRef(Autocomplete)');
// // trigger value change
textField.props().onChange({ target: { value: '15 characters..' } });
autoComplete.props().onInputChange('', '15 characters..');
// trigger enter
textField = wrapper.find('ForwardRef(TextField)');
textField.props().onKeyDown({ key: 'other key' });
autoComplete = wrapper.find('ForwardRef(Autocomplete)');
autoComplete.props().onKeyDown({ key: 'other key' });
expect(props.addTag).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ exports[`The tags form component will render 1`] = `
"display": "flex",
"marginLeft": "auto",
"marginRight": "auto",
"width": 150,
"width": 300,
}
}
>
<ForwardRef(TextField)
autoFocus={true}
<ForwardRef(Autocomplete)
autoComplete={true}
disableClearable={true}
filterOptions={[Function]}
freeSolo={true}
fullWidth={true}
id="Tags_form"
label="Add a Tag"
id="free-solo-tags"
onBlur={[MockFunction]}
onChange={[Function]}
onInputChange={[Function]}
onKeyDown={[Function]}
type="text"
options={
Array [
"SUGGESTED TAG 1",
"SUGGESTED TAG 2",
]
}
renderInput={[Function]}
value=""
variant="standard"
/>
<Memo(ForwardRef(DoneIcon))
color="success"
Expand Down
2 changes: 2 additions & 0 deletions tests/server/crudroutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
const pins = require('../../server/models/pins'); // schema for pins
const users = require('../../server/models/user'); // schema for pins
const pinLinks = require('../../server/models/pinlinks'); // schema for pins
const savedTags = require('../../server/models/tags');
const {
user, rawPinsStub, allPinsResponse,
} = require('./stub');
Expand Down Expand Up @@ -762,6 +763,7 @@ describe('Updating tags for a pin', () => {
exec: jest.fn().mockResolvedValue({ ...rawPinsStub[1] }),
}),
);
savedTags.create = jest.fn().mockResolvedValue([]);
await updateTags(req, res);
expect(pins.findById).toHaveBeenCalledTimes(1);
expect(pins.findById).toHaveBeenCalledWith(1);
Expand Down

0 comments on commit 98edd13

Please sign in to comment.