Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions src/ifElse.test.js
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'", () => {

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:

  1. The second callback is not executed when the condition is true.
  2. Both the condition and first callbacks 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() and expect(mock).toBeCalledWith().

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'", () => {

Choose a reason for hiding this comment

The 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:

  1. The first callback is not executed when the condition is false.
  2. Both the condition and second callbacks are executed with no arguments.

You can use another mock function here as well to assert that it was not called, and check the arguments with toBeCalledWith().

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this test passes correctly because the condition is true, the ifElse function is defined to accept three arguments. For consistency and to accurately represent the function's signature, it's best to provide all three arguments, even if one is just a placeholder empty function.

Consider changing this to ifElse(() => true, f, () => {});.


expect(f).toBeCalledWith();
});

it("should call 'second' without arguments", () => {
const f = jest.fn();

ifElse(() => false, () => {}, f);

expect(f).toBeCalledWith();
});
});