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
37 changes: 34 additions & 3 deletions src/ifElse.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
'use strict';

describe('ifElse', () => {
// const { ifElse } = require('./ifElse');
const { ifElse } = require('./ifElse');

it('should ', () => {});
let condition;
let first;
let second;

// write tests here
beforeEach(() => {
first = jest.fn(() => 1);
second = jest.fn(() => 2);
});

it('should call condition', () => {
condition = jest.fn(() => true);

ifElse(condition, first, second);

expect(condition).toHaveBeenCalledWith();
});

it('should call the first callback when the condition is true', () => {
condition = () => true;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

To keep your tests consistent, consider using jest.fn() for the condition here, similar to your first test. This would let you verify that condition is called in the true path within this same test case.


ifElse(condition, first, second);

expect(first).toHaveBeenCalledWith();
expect(second).not.toHaveBeenCalled();
});

it('should call the second callback when the condition is false', () => {
condition = () => false;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similar to the test for the true condition, making this condition a mock function would allow you to assert it gets called in the false path as well, making your test suite more robust.


ifElse(condition, first, second);

expect(first).not.toHaveBeenCalled();
expect(second).toHaveBeenCalledWith();
});
});
Loading