-
Notifications
You must be signed in to change notification settings - Fork 253
ifElse – dodanie testów dla wszystkich scenariuszy działania funkcji #222
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
93a03cb
21264a1
967446c
b474b9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,16 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * @param condition | ||
| * @param first | ||
| * @param second | ||
| * @param {Function} condition | ||
| * @param {Function} first | ||
| * @param {Function} second | ||
| */ | ||
| function ifElse(condition, first, second) { | ||
| if (condition() === true) { | ||
| if (condition()) { | ||
| first(); | ||
| } else { | ||
| second(); | ||
| } | ||
| } | ||
|
|
||
| module.exports = { ifElse }; | ||
| module.exports = ifElse; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,46 @@ | ||
| 'use strict'; | ||
|
|
||
| const { ifElse } = require('./ifElse'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import/export mismatch: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import line uses destructuring ( |
||
|
|
||
| describe('ifElse', () => { | ||
| // const { ifElse } = require('./ifElse'); | ||
| let firstMock; | ||
| let secondMock; | ||
| let conditionTrue; | ||
| let conditionFalse; | ||
|
|
||
| beforeEach(() => { | ||
| firstMock = jest.fn(); | ||
| secondMock = jest.fn(); | ||
| conditionTrue = () => true; | ||
|
||
| conditionFalse = () => false; | ||
|
||
| }); | ||
|
|
||
| it('should call first callback if condition returns true', () => { | ||
| ifElse(conditionTrue, firstMock, secondMock); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the "should call first callback if condition returns true" test you should assert the |
||
|
|
||
| expect(firstMock).toHaveBeenCalled(); | ||
|
||
| expect(secondMock).not.toHaveBeenCalled(); | ||
|
||
| }); | ||
|
|
||
| it('should ', () => { | ||
| it('should call second callback if condition returns false', () => { | ||
| ifElse(conditionFalse, firstMock, secondMock); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the "should call second callback if condition returns false" test you likewise need to assert the condition was called exactly once and with no args. After converting |
||
|
|
||
| expect(secondMock).toHaveBeenCalled(); | ||
|
||
| expect(firstMock).not.toHaveBeenCalled(); | ||
|
||
| }); | ||
|
|
||
| // write tests here | ||
| it('should work when condition is a dynamic function', () => { | ||
| const dynamicCondition = jest.fn(() => true); | ||
| ifElse(dynamicCondition, firstMock, secondMock); | ||
|
|
||
| expect(dynamicCondition).toHaveBeenCalled(); | ||
|
||
| expect(firstMock).toHaveBeenCalled(); | ||
| expect(secondMock).not.toHaveBeenCalled(); | ||
|
||
| }); | ||
|
|
||
| it('should not return any value', () => { | ||
| const result = ifElse(conditionTrue, firstMock, secondMock); | ||
| expect(result).toBeUndefined(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test checking that the function returns no value (
Comment on lines
+57
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 'should not return any value' test asserts the return is undefined which meets the return requirement. To make this test stronger, call |
||
| }); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Export mismatch (blocking): The tests import using destructuring:
const { ifElse } = require('./ifElse');which expects the module to export an object with anifElseproperty. Here you export the function directly (module.exports = ifElse;), soifElsewill beundefinedin the tests. Update the export to a named export object, for example:or change the tests to
const ifElse = require('./ifElse');so the import matches the current export. This must be fixed for the tests to run.