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
32 changes: 23 additions & 9 deletions src/01-simple-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
// 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
const total = simpleCalculator({ a: 4, b: 2, action: Action.Add });
expect(total).toBe(6);
});

test('should subtract two numbers', () => {
// Write your test here
const total = simpleCalculator({ a: 4, b: 2, action: Action.Subtract });
expect(total).toBe(2);
});

test('should multiply two numbers', () => {
// Write your test here
const total = simpleCalculator({ a: 4, b: 2, action: Action.Multiply });
expect(total).toBe(8);
});

test('should divide two numbers', () => {
// Write your test here
const total = simpleCalculator({ a: 4, b: 2, action: Action.Divide });
expect(total).toBe(2);
});

test('should exponentiate two numbers', () => {
// Write your test here
const total = simpleCalculator({ a: 4, b: 2, action: Action.Exponentiate });
expect(total).toBe(16);
});

test('should return null for invalid action', () => {
// Write your test here
const total = simpleCalculator({
a: 4,
b: 2,
action: '',
});
expect(total).toBe(null);
});

test('should return null for invalid arguments', () => {
// Write your test here
const total = simpleCalculator({
a: '',
b: 2,
action: Action.Exponentiate,
});
expect(total).toBe(null);
});
});
26 changes: 16 additions & 10 deletions src/02-table-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// 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 },
// cases from previous test
{ a: 4, b: 2, action: Action.Add, expected: 6 },
{ a: 4, b: 2, action: Action.Subtract, expected: 2 },
{ a: 4, b: 2, action: Action.Multiply, expected: 8 },
{ a: 4, b: 2, action: Action.Divide, expected: 2 },
{ a: 4, b: 2, action: Action.Exponentiate, expected: 16 },
{ a: 4, b: 2, action: '', expected: null },
{ a: '', b: 2, action: Action.Exponentiate, 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)('table tests', ({ a, b, action, expected }) => {
expect(simpleCalculator({ a, b, action })).toBe(expected);
});
});
22 changes: 15 additions & 7 deletions src/03-error-handling-async/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
// 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';
await expect(resolveValue(value)).resolves.toBe(value);
});
});

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

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

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);
});
});
90 changes: 78 additions & 12 deletions src/04-test-class/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,110 @@
// Uncomment the code below and write your tests
// import { getBankAccount } from '.';
import {
getBankAccount,
InsufficientFundsError,
SynchronizationFailedError,
TransferFailedError,
} from '.';

describe('BankAccount', () => {
test('should create account with initial balance', () => {
// Write your test here
const initialBalance = 10;
const bankAccount = getBankAccount(initialBalance);
expect(bankAccount.getBalance()).toBe(initialBalance);
});

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

expect(() => bankAccount.withdraw(withdrawAmount)).toThrow(
InsufficientFundsError,
);
});

test('should throw error when transferring more than balance', () => {
// Write your test here
const initialBalance = 10;
const transferAmount = 15;
const fromBankAccount = getBankAccount(initialBalance);
const toBankAccount = getBankAccount(initialBalance);

expect(() =>
fromBankAccount.transfer(transferAmount, toBankAccount),
).toThrow(InsufficientFundsError);
});

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

expect(() => bankAccount.transfer(transferAmount, bankAccount)).toThrow(
TransferFailedError,
);
});

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

expect(bankAccount.deposit(depositAmount).getBalance()).toEqual(
initialBalance + depositAmount,
);
});

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

expect(bankAccount.withdraw(withdrawAmount).getBalance()).toEqual(
initialBalance - withdrawAmount,
);
});

test('should transfer money', () => {
// Write your test here
const initialBalance = 10;
const bankAccount = getBankAccount(initialBalance);
const toAccount = getBankAccount(0);

expect(bankAccount.transfer(10, toAccount).getBalance()).toBe(0);
});

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

expect(bankAccount.fetchBalance()).resolves.toBeDefined();
});

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

// Mock the method not assigned it
const fetchBalanceSpy = jest.spyOn(bankAccount, 'fetchBalance');

// bankAccount.fetchBalance = () => Promise.resolve(newBalance);
// const balance = await bankAccount.fetchBalance();
// if (balance) {
// await expect(bankAccount.getBalance()).toBe(balance);
// }
await bankAccount.synchronizeBalance();
expect(bankAccount.getBalance()).toEqual(newBalance);
});

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

const balance = await bankAccount.fetchBalance();

if (!balance) {
await expect(() => bankAccount.synchronizeBalance).toThrow(
SynchronizationFailedError,
);
}
});
});
35 changes: 30 additions & 5 deletions src/05-partial-mocking/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// 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 {
__esModule: true,
...originalModule,
mockOne: jest.fn(),
mockTwo: jest.fn(),
mockThree: jest.fn(),
};
});

describe('partial mocking', () => {
Expand All @@ -11,10 +19,27 @@ describe('partial mocking', () => {
});

test('mockOne, mockTwo, mockThree should not log into console', () => {
// Write your test here
const consoleSpy = jest.spyOn(global.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(global.console, 'log');

unmockedFunction();

expect(consoleSpy).toHaveBeenCalled();
expect(consoleSpy).toHaveBeenCalledTimes(1);
expect(consoleSpy).toHaveBeenCalledWith('I am not mocked');
consoleSpy.mockRestore();
});
});
Loading