-
Notifications
You must be signed in to change notification settings - Fork 253
test: add tests for isElse function #225
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,11 +1,71 @@ | ||
| 'use strict'; | ||
|
|
||
| describe('ifElse', () => { | ||
| // const { ifElse } = require('./ifElse'); | ||
| const { ifElse } = require('./ifElse'); | ||
|
|
||
| it('should ', () => { | ||
| it('should not return anything', () => { | ||
| expect( | ||
| ifElse( | ||
| () => true, | ||
| () => {}, | ||
| () => {} | ||
| ) | ||
| ).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should call 'first' if condition returns 'true'", () => { | ||
| const f = jest.fn(); | ||
|
|
||
| ifElse(() => true, f, () => {}); | ||
|
|
||
| expect(f).toBeCalled(); | ||
| }); | ||
|
|
||
| it("shouldn't call 'second' if condition returns 'true'", () => { | ||
| const f = jest.fn(); | ||
|
|
||
| ifElse(() => true, () => {}, f); | ||
|
|
||
| expect(f).not.toBeCalled(); | ||
| }); | ||
|
|
||
| it("should call 'second' if condition returns 'false'", () => { | ||
|
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, this one is incomplete. You need to verify two more things:
You can use another mock function here as well to assert that it was not called, and check the arguments with |
||
| const f = jest.fn(); | ||
|
|
||
| ifElse(() => false, () => {}, f); | ||
|
|
||
| expect(f).toBeCalled(); | ||
| }); | ||
|
|
||
| it("shouldn't call 'first' if condition returns 'false'", () => { | ||
| const f = jest.fn(); | ||
|
|
||
| ifElse(() => false, f, () => {}); | ||
|
|
||
| expect(f).not.toBeCalled(); | ||
| }); | ||
|
|
||
| // write tests here | ||
| it("should call 'condition' without arguments", () => { | ||
| const f = jest.fn(() => true); | ||
|
|
||
| ifElse(f, () => {}, () => {}); | ||
|
|
||
| expect(f).toBeCalledWith(); | ||
| }); | ||
|
|
||
| it("should call 'first' without arguments", () => { | ||
| const f = jest.fn(); | ||
|
|
||
| ifElse(() => true, f); | ||
|
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. While this test passes correctly because the Consider changing this to |
||
|
|
||
| expect(f).toBeCalledWith(); | ||
| }); | ||
|
|
||
| it("should call 'second' without arguments", () => { | ||
| const f = jest.fn(); | ||
|
|
||
| ifElse(() => false, () => {}, f); | ||
|
|
||
| expect(f).toBeCalledWith(); | ||
| }); | ||
| }); | ||
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 test is a good start, but it doesn't fully verify the behavior. According to the requirements, you should also check that:
secondcallback is not executed when the condition is true.conditionandfirstcallbacks are executed with no arguments.To do this, you could use mock functions for all callbacks and then use assertions like
expect(mock).not.toBeCalled()andexpect(mock).toBeCalledWith().