-
Notifications
You must be signed in to change notification settings - Fork 253
solution #239
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?
solution #239
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,70 @@ | ||
| 'use strict'; | ||
| "use strict"; | ||
|
|
||
| describe('ifElse', () => { | ||
| // const { ifElse } = require('./ifElse'); | ||
| describe(`ifElse`, () => { | ||
| const { ifElse } = require(`./ifElse`); | ||
|
|
||
| it('should ', () => { | ||
| it(`should be a function`, () => { | ||
| expect(ifElse).toBeInstanceOf(Function); | ||
| }); | ||
|
|
||
| it(`the first function must be called`, () => { | ||
| const condition = () => true; | ||
| let count = null; | ||
|
|
||
| const first = () => { | ||
| count = 1; | ||
| }; | ||
|
|
||
| const second = () => { | ||
| count = 2; | ||
| }; | ||
|
|
||
| ifElse(condition, first, second); | ||
| expect(count).toBe(1); | ||
| }); | ||
|
|
||
| it(`if function returns a 'truthy' | ||
| the second function must be called`, () => { | ||
| const condition = () => "hello"; | ||
| let count = null; | ||
|
|
||
| const first = () => { | ||
| count = 1; | ||
| }; | ||
|
|
||
| const second = () => { | ||
| count = 2; | ||
| }; | ||
|
|
||
| ifElse(condition, first, second); | ||
| expect(count).toBe(2); | ||
|
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. Based on the standard behavior for truthy values, this should expect |
||
| }); | ||
|
|
||
| it(`the second function must be called`, () => { | ||
|
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 test correctly handles the |
||
| const condition = () => false; | ||
| let count = null; | ||
|
|
||
| const first = () => { | ||
| count = 1; | ||
| }; | ||
|
|
||
| const second = () => { | ||
| count = 2; | ||
| }; | ||
|
|
||
| ifElse(condition, first, second); | ||
| expect(count).toBe(2); | ||
| }); | ||
|
|
||
| // write tests here | ||
| it(`should return undefined`, () => { | ||
| const result = ifElse( | ||
| () => Math.random() > 0.5, | ||
| // eslint-disable-next-line no-console | ||
| () => console.log(1), | ||
| // eslint-disable-next-line no-console | ||
| () => console.log(2), | ||
| ); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| }); | ||
| }); | ||
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.
The title of this test is misleading. In JavaScript, it's standard for conditions to check for 'truthiness', not just strict equality with
true. Therefore, if theconditionfunction returns any truthy value (like'hello'), thefirstfunction should be called.