-
Notifications
You must be signed in to change notification settings - Fork 258
added unit tests for isElse function #247
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
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,9 +1,71 @@ | ||
| 'use strict'; | ||
|
|
||
| describe('ifElse', () => { | ||
| // const { ifElse } = require('./ifElse'); | ||
| const { ifElse } = require('./ifElse'); | ||
|
|
||
| it('should ', () => {}); | ||
| let truthyCondition, falsyCondition, first, second; | ||
|
|
||
| // write tests here | ||
| beforeAll(() => { | ||
| truthyCondition = jest.fn(() => true); | ||
| falsyCondition = jest.fn(() => false); | ||
|
|
||
| first = jest.fn(() => 'First'); | ||
| second = jest.fn(() => 'Second'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| first.mockClear(); | ||
| second.mockClear(); | ||
| truthyCondition.mockClear(); | ||
| falsyCondition.mockClear(); | ||
| }); | ||
|
|
||
| it('should be a function', () => { | ||
| expect(ifElse).toBeInstanceOf(Function); | ||
| }); | ||
|
|
||
| it('should return nothing', () => { | ||
| expect(ifElse(truthyCondition, first, second)).toEqual(undefined); | ||
| }); | ||
|
|
||
| it('should invoke first cb if truthy contidion', () => { | ||
| ifElse(truthyCondition, first, second); | ||
|
|
||
| expect(first).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should invoke second cb if falsy condition', () => { | ||
| ifElse(falsyCondition, first, second); | ||
|
|
||
| expect(second).toHaveBeenCalled(); | ||
|
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. Similar to the previous test, you can make this check more specific by verifying that the |
||
| }); | ||
|
|
||
| it('should invoke one of functions in any case', () => { | ||
| const randomCondition = jest.fn().mockReturnValue(!!(Math.random() > 0.5)); | ||
|
|
||
| ifElse(randomCondition, first, second); | ||
|
|
||
| const totalCalls = first.mock.calls.length + second.mock.calls.length; | ||
|
|
||
| expect(totalCalls).toBe(1); | ||
| }); | ||
|
|
||
| it('should not work without condition', () => { | ||
| expect(() => { | ||
| ifElse(undefined, first, second); | ||
| }).toThrow(); | ||
| }); | ||
|
|
||
| it('should invoke first or second functions without args', () => { | ||
|
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. This is a great step forward! You're now correctly checking that To fully meet the requirements, you also need to verify that the A good way to organize this would be to merge these checks into the tests for the truthy and falsy paths. For example, the 'should invoke first cb' test could verify that both |
||
| ifElse(truthyCondition, first, second); | ||
| expect(first).toHaveBeenCalledWith(); | ||
|
|
||
| ifElse(falsyCondition, first, second); | ||
| expect(second).toHaveBeenLastCalledWith(); | ||
| }); | ||
|
|
||
| it('should invoke condition without any args', () => { | ||
| ifElse(truthyCondition, first, second); | ||
| expect(truthyCondition).toHaveBeenCalledWith(); | ||
| }); | ||
| }); | ||
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.
This assertion is correct. To make it more precise according to the requirements, you could also verify that the callback is called with no arguments. You can do this by changing the assertion to
expect(first).toHaveBeenCalledWith();.