-
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 all 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,11 +1,63 @@ | ||
| '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 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 = jest.fn(() => true); | ||
| conditionFalse = jest.fn(() => 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(conditionTrue).toHaveBeenCalledTimes(1); | ||
| expect(conditionTrue).toHaveBeenCalledWith(); | ||
|
|
||
| expect(firstMock).toHaveBeenCalledTimes(1); | ||
| expect(firstMock).toHaveBeenCalledWith(); | ||
|
|
||
|
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. Add an assertion that // ensure condition ran before first
expect(conditionTrue.mock.invocationCallOrder[0]).toBeLessThan(firstMock.mock.invocationCallOrder[0]);Place this near the true-branch assertions so the test verifies ordering as well as call counts/arguments. |
||
| expect(secondMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| 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(conditionFalse).toHaveBeenCalledTimes(1); | ||
| expect(conditionFalse).toHaveBeenCalledWith(); | ||
|
|
||
| it('should ', () => { | ||
| expect(secondMock).toHaveBeenCalledTimes(1); | ||
|
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 false-branch test, add an assertion that expect(conditionFalse.mock.invocationCallOrder[0]).toBeLessThan(secondMock.mock.invocationCallOrder[0]);This will ensure the test verifies the required call order for the false branch. |
||
| expect(secondMock).toHaveBeenCalledWith(); | ||
|
|
||
| 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).toHaveBeenCalledTimes(1); | ||
| expect(dynamicCondition).toHaveBeenCalledWith(); | ||
|
|
||
|
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. For the dynamic-condition test, add the same ordering assertion to ensure expect(dynamicCondition.mock.invocationCallOrder[0]).toBeLessThan(firstMock.mock.invocationCallOrder[0]);This confirms the implementation consistently calls the condition before the chosen branch even for dynamic functions. |
||
| expect(firstMock).toHaveBeenCalledTimes(1); | ||
| expect(firstMock).toHaveBeenCalledWith(); | ||
|
|
||
| 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 ( |
||
| expect(conditionTrue).toHaveBeenCalledTimes(1); | ||
|
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 not return any value" test, you already check expect(conditionTrue).toHaveBeenCalledWith();This strengthens the test to verify both call count and that no args were passed to |
||
| }); | ||
| }); | ||
|
|
||
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.
Import/export mismatch:
const { ifElse } = require('./ifElse');expects a named export (an object with anifElseproperty). The implementation exports the function directly (module.exports = ifElse), so this destructuring will result inifElsebeing undefined and the tests will fail to run. Fix either by changing the import toconst ifElse = require('./ifElse');or by exporting an object from the implementation (module.exports = { ifElse };).