Skip to content
Open
Show file tree
Hide file tree
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
49 changes: 40 additions & 9 deletions src/01-simple-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,63 @@
// Uncomment the code below and write your tests
// import { simpleCalculator, Action } from './index';
import { simpleCalculator, Action } from './index';

describe('simpleCalculator tests', () => {
test('should add two numbers', () => {
// Write your test here
expect(simpleCalculator({ a: 7, b: 6, action: Action.Add })).toBe(13);
expect(simpleCalculator({ a: -7, b: 6, action: Action.Add })).toBe(-1);
expect(simpleCalculator({ a: 2, b: 2, action: Action.Add })).toBe(4);
});

test('should subtract two numbers', () => {
// Write your test here
expect(simpleCalculator({ a: 7, b: 6, action: Action.Subtract })).toBe(1);
expect(simpleCalculator({ a: -7, b: 6, action: Action.Subtract })).toBe(
-13,
);
expect(simpleCalculator({ a: 2, b: 2, action: Action.Subtract })).toBe(0);
});

test('should multiply two numbers', () => {
// Write your test here
expect(simpleCalculator({ a: 7, b: 6, action: Action.Multiply })).toBe(42);
expect(simpleCalculator({ a: -7, b: 6, action: Action.Multiply })).toBe(
-42,
);
expect(simpleCalculator({ a: 0, b: 2, action: Action.Multiply })).toBe(0);
});

test('should divide two numbers', () => {
// Write your test here
expect(simpleCalculator({ a: 15, b: 3, action: Action.Divide })).toBe(5);
expect(simpleCalculator({ a: -15, b: 3, action: Action.Divide })).toBe(-5);
expect(simpleCalculator({ a: 0, b: 2, action: Action.Divide })).toBe(0);
expect(simpleCalculator({ a: 2, b: 0, action: Action.Divide })).toBe(
Infinity,
);
});

test('should exponentiate two numbers', () => {
// Write your test here
expect(simpleCalculator({ a: 2, b: 3, action: Action.Exponentiate })).toBe(
8,
);
expect(simpleCalculator({ a: -2, b: 3, action: Action.Exponentiate })).toBe(
-8,
);
expect(simpleCalculator({ a: 2, b: -3, action: Action.Exponentiate })).toBe(
0.125,
);
expect(simpleCalculator({ a: 0, b: 3, action: Action.Exponentiate })).toBe(
0,
);
});

test('should return null for invalid action', () => {
// Write your test here
expect(simpleCalculator({ a: 7, b: 6, action: '%' })).toBeNull();
});

test('should return null for invalid arguments', () => {
// Write your test here
expect(simpleCalculator({ a: '7', b: 6, action: Action.Add })).toBeNull();
expect(
simpleCalculator({ a: 7, b: '6', action: Action.Subtract }),
).toBeNull();
expect(
simpleCalculator({ a: null, b: 6, action: Action.Multiply }),
).toBeNull();
});
});
43 changes: 31 additions & 12 deletions src/02-table-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
// Uncomment the code below and write your tests
/* import { simpleCalculator, Action } from './index';
import { simpleCalculator, Action } from './index';

const testCases = [
{ a: 1, b: 2, action: Action.Add, expected: 3 },
{ a: 2, b: 2, action: Action.Add, expected: 4 },
{ a: 3, b: 2, action: Action.Add, expected: 5 },
// continue cases for other actions
]; */
{ a: 1, b: 2, action: Action.Add, expected: 3 },
{ a: 2, b: 2, action: Action.Add, expected: 4 },
{ a: 3, b: 2, action: Action.Add, expected: 5 },
{ a: 7, b: 6, action: Action.Add, expected: 13 },
{ a: -7, b: 6, action: Action.Add, expected: -1 },
{ a: 7, b: 6, action: Action.Subtract, expected: 1 },
{ a: -7, b: 6, action: Action.Subtract, expected: -13 },
{ a: 2, b: 2, action: Action.Subtract, expected: 0 },
{ a: 7, b: 6, action: Action.Multiply, expected: 42 },
{ a: -7, b: 6, action: Action.Multiply, expected: -42 },
{ a: 0, b: 2, action: Action.Multiply, expected: 0 },
{ a: 15, b: 3, action: Action.Divide, expected: 5 },
{ a: -15, b: 3, action: Action.Divide, expected: -5 },
{ a: 0, b: 2, action: Action.Divide, expected: 0 },
{ a: 2, b: 0, action: Action.Divide, expected: Infinity },
{ a: 2, b: 3, action: Action.Exponentiate, expected: 8 },
{ a: -2, b: 3, action: Action.Exponentiate, expected: -8 },
{ a: 2, b: -3, action: Action.Exponentiate, expected: 0.125 },
{ a: 0, b: 3, action: Action.Exponentiate, expected: 0 },
{ a: 7, b: 6, action: '%', expected: null },
{ a: '7', b: 6, action: Action.Add, expected: null },
{ a: 7, b: '6', action: Action.Add, expected: null },
{ a: null, b: 6, action: Action.Add, expected: null },
];

describe('simpleCalculator', () => {
// This test case is just to run this test suite, remove it when you write your own tests
test('should blah-blah', () => {
expect(true).toBe(true);
});
// Consider to use Jest table tests API to test all cases above
test.each(testCases)(
'should return $expected for a=$a, b=$b, action=$action',
({ a, b, action, expected }) => {
expect(simpleCalculator({ a, b, action })).toBe(expected);
},
);
});
21 changes: 14 additions & 7 deletions src/03-error-handling-async/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
// Uncomment the code below and write your tests
// import { throwError, throwCustomError, resolveValue, MyAwesomeError, rejectCustomError } from './index';
import {
throwError,
throwCustomError,
resolveValue,
MyAwesomeError,
rejectCustomError,
} from './index';

describe('resolveValue', () => {
test('should resolve provided value', async () => {
// Write your test here
const value = 'Hello, world!';
await expect(resolveValue(value)).resolves.toBe(value);
});
});

describe('throwError', () => {
test('should throw error with provided message', () => {
// Write your test here
const message = 'Error message';
expect(() => throwError(message)).toThrow(message);
});

test('should throw error with default message if message is not provided', () => {
// Write your test here
expect(() => throwError()).toThrow('Oops!');
});
});

describe('throwCustomError', () => {
test('should throw custom error', () => {
// Write your test here
expect(() => throwCustomError()).toThrow(MyAwesomeError);
});
});

describe('rejectCustomError', () => {
test('should reject custom error', async () => {
// Write your test here
await expect(rejectCustomError()).rejects.toThrow(MyAwesomeError);
});
});
84 changes: 72 additions & 12 deletions src/04-test-class/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,104 @@
// Uncomment the code below and write your tests
// import { getBankAccount } from '.';
import {
getBankAccount,
InsufficientFundsError,
TransferFailedError,
SynchronizationFailedError,
} from '.';
import { random } from 'lodash';

jest.mock('lodash', () => ({
random: jest.fn(),
}));

const initialBalance = 1000;
const balance = 100;

describe('BankAccount', () => {
test('should create account with initial balance', () => {
// Write your test here
const account = getBankAccount(initialBalance);

expect(account.getBalance()).toBe(initialBalance);
});

test('should throw InsufficientFundsError error when withdrawing more than balance', () => {
// Write your test here
const account = getBankAccount(initialBalance);

expect(() => account.withdraw(10000)).toThrow(InsufficientFundsError);
});

test('should throw error when transferring more than balance', () => {
// Write your test here
const account1 = getBankAccount(initialBalance);
const account2 = getBankAccount(initialBalance);

expect(() => account1.transfer(10000, account2)).toThrow(
InsufficientFundsError,
);
});

test('should throw error when transferring to the same account', () => {
// Write your test here
const account = getBankAccount(initialBalance);

expect(() => account.transfer(100, account)).toThrow(TransferFailedError);
});

test('should deposit money', () => {
// Write your test here
const account = getBankAccount(initialBalance);
const depositAmount = 100;

account.deposit(depositAmount);
expect(account.getBalance()).toBe(initialBalance + depositAmount);
});

test('should withdraw money', () => {
// Write your test here
const account = getBankAccount(initialBalance);
const withdrawAmount = 100;

account.withdraw(withdrawAmount);
expect(account.getBalance()).toBe(initialBalance - withdrawAmount);
});

test('should transfer money', () => {
// Write your test here
const account1 = getBankAccount(initialBalance);
const account2 = getBankAccount(initialBalance);
const transferAmount = 100;

account1.transfer(transferAmount, account2);
expect(account1.getBalance()).toBe(initialBalance - transferAmount);
expect(account2.getBalance()).toBe(initialBalance + transferAmount);
});

test('fetchBalance should return number in case if request did not failed', async () => {
// Write your tests here
const account = getBankAccount(initialBalance);
const balance = 100;
const requestSucceeded = 1;

(random as jest.Mock)
.mockReturnValueOnce(balance)
.mockReturnValueOnce(requestSucceeded);
await expect(account.fetchBalance()).resolves.toBe(balance);
});

test('should set new balance if fetchBalance returned number', async () => {
// Write your tests here
const account = getBankAccount(initialBalance);
const requestSucceeded = 1;

(random as jest.Mock)
.mockReturnValueOnce(balance)
.mockReturnValueOnce(requestSucceeded);
await account.synchronizeBalance();
expect(account.getBalance()).toBe(balance);
});

test('should throw SynchronizationFailedError if fetchBalance returned null', async () => {
// Write your tests here
const account = getBankAccount(initialBalance);
const requestFailed = 0;

(random as jest.Mock)
.mockReturnValueOnce(balance)
.mockReturnValueOnce(requestFailed);
await expect(account.synchronizeBalance()).rejects.toThrow(
SynchronizationFailedError,
);
expect(account.getBalance()).toBe(initialBalance);
});
});
32 changes: 23 additions & 9 deletions src/05-partial-mocking/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
// Uncomment the code below and write your tests
// import { mockOne, mockTwo, mockThree, unmockedFunction } from './index';
import { mockOne, mockTwo, mockThree, unmockedFunction } from './index';

jest.mock('./index', () => {
// const originalModule = jest.requireActual<typeof import('./index')>('./index');
const originalModule =
jest.requireActual<typeof import('./index')>('./index');

return {
...originalModule,
mockOne: jest.fn(),
mockTwo: jest.fn(),
mockThree: jest.fn(),
};
});

describe('partial mocking', () => {
afterAll(() => {
jest.unmock('./index');
});

test('mockOne, mockTwo, mockThree should not log into console', () => {
// Write your test here
const consoleSpy = jest.spyOn(console, 'log');
mockOne();
mockTwo();
mockThree();
expect(mockOne).toHaveBeenCalled();
expect(mockTwo).toHaveBeenCalled();
expect(mockThree).toHaveBeenCalled();
expect(consoleSpy).not.toHaveBeenCalled();
consoleSpy.mockRestore();
});

test('unmockedFunction should log into console', () => {
// Write your test here
const consoleSpy = jest.spyOn(console, 'log');
unmockedFunction();
expect(consoleSpy).toHaveBeenCalledWith('I am not mocked');
consoleSpy.mockRestore();
});
});
Loading