Skip to content
20 changes: 12 additions & 8 deletions src/01-simple-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
// 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: 1, b: 2, action: Action.Add })).toBe(3);
});

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

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

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

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

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

test('should return null for invalid arguments', () => {
// Write your test here
expect(
simpleCalculator({ a: '100', b: '0', action: Action.Exponentiate }),
).toBeNull();
});
});
38 changes: 29 additions & 9 deletions src/02-table-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
// 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: 3, b: 2, action: Action.Subtract, expected: 1 },
{ a: 4, b: 2, action: Action.Subtract, expected: 2 },
{ a: 3, b: 3, action: Action.Subtract, expected: 0 },
{ a: 3, b: 2, action: Action.Multiply, expected: 6 },
{ a: 3, b: 3, action: Action.Multiply, expected: 9 },
{ a: 4, b: 2, action: Action.Multiply, expected: 8 },
{ a: 4, b: 2, action: Action.Divide, expected: 2 },
{ a: 4, b: 4, action: Action.Divide, expected: 1 },
{ a: 6, b: 2, action: Action.Divide, expected: 3 },
{ a: 3, b: 2, action: Action.Exponentiate, expected: 9 },
{ a: 3, b: 0, action: Action.Exponentiate, expected: 1 },
{ a: 5, b: 2, action: Action.Exponentiate, expected: 25 },
{ a: 3, b: 1, action: Action, expected: null },
{ a: 2, b: 2, action: Action, expected: null },
{ a: 1, b: 3, action: Action, expected: null },
{ a: '3', b: '1', action: Action.Add, expected: null },
{ a: '2', b: '2', action: Action.Subtract, expected: null },
{ a: '1', b: '3', action: Action.Multiply, 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);
});
test.each(testCases)(
'should return $expected if $a $action $b',
({ a, b, action, expected }) => {
expect(simpleCalculator({ a, b, action })).toBe(expected);
},
);
// Consider to use Jest table tests API to test all cases above
});
33 changes: 27 additions & 6 deletions src/03-error-handling-async/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
// 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 = 'value';
const result = await resolveValue(value);
expect(result).toBe(value);
});
});

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

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);
expect(() => {
throwCustomError();
}).toThrow('This is my awesome custom error!');
});
});

describe('rejectCustomError', () => {
test('should reject custom error', async () => {
// Write your test here
try {
await rejectCustomError();
} catch (e) {
expect(e).toBeInstanceOf(MyAwesomeError);
}
});
});
68 changes: 57 additions & 11 deletions src/04-test-class/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,90 @@
// Uncomment the code below and write your tests
// import { getBankAccount } from '.';
import {
getBankAccount,
BankAccount,
InsufficientFundsError,
TransferFailedError,
SynchronizationFailedError,
} from '.';

import * as lodash from 'lodash';
jest.mock('lodash');

describe('BankAccount', () => {
let balance: number;
let bankAcc: BankAccount;
let anotherBankAcc: BankAccount;
let fetchBalance: number;
const spyLodashRandom = jest.spyOn(lodash, 'random');

beforeEach(() => {
jest.clearAllMocks();

balance = 1000;
bankAcc = getBankAccount(balance);
anotherBankAcc = getBankAccount(balance);

fetchBalance = 99;
spyLodashRandom.mockReturnValueOnce(fetchBalance);
});

test('should create account with initial balance', () => {
// Write your test here
expect(bankAcc).toBeInstanceOf(BankAccount);
expect(bankAcc.getBalance()).toBe(balance);
});

test('should throw InsufficientFundsError error when withdrawing more than balance', () => {
// Write your test here
expect(() => {
bankAcc.withdraw(balance ** 2);
}).toThrow(InsufficientFundsError);
});

test('should throw error when transferring more than balance', () => {
// Write your test here
expect(() => {
bankAcc.transfer(balance ** 2, anotherBankAcc);
}).toThrow(InsufficientFundsError);
});

test('should throw error when transferring to the same account', () => {
// Write your test here
expect(() => {
bankAcc.transfer(balance, bankAcc);
}).toThrow(TransferFailedError);
});

test('should deposit money', () => {
// Write your test here
bankAcc.deposit(balance);
expect(bankAcc.getBalance()).toBe(balance + balance);
});

test('should withdraw money', () => {
// Write your test here
bankAcc.withdraw(balance);
expect(bankAcc.getBalance()).toBe(balance - balance);
});

test('should transfer money', () => {
// Write your test here
bankAcc.transfer(balance, anotherBankAcc);
expect(bankAcc.getBalance()).toBe(balance - balance);
expect(anotherBankAcc.getBalance()).toBe(balance + balance);
});

test('fetchBalance should return number in case if request did not failed', async () => {
// Write your tests here
spyLodashRandom.mockReturnValueOnce(1);
const result = await bankAcc.fetchBalance();
expect(result).toBe(fetchBalance);
});

test('should set new balance if fetchBalance returned number', async () => {
// Write your tests here
spyLodashRandom.mockReturnValueOnce(1);
await bankAcc.synchronizeBalance();
expect(bankAcc.getBalance()).toBe(fetchBalance);
});

test('should throw SynchronizationFailedError if fetchBalance returned null', async () => {
// Write your tests here
spyLodashRandom.mockReturnValueOnce(0);
try {
await bankAcc.synchronizeBalance();
} catch (e) {
expect(e).toBeInstanceOf(SynchronizationFailedError);
}
});
});
23 changes: 19 additions & 4 deletions src/05-partial-mocking/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
// 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 +20,16 @@ describe('partial mocking', () => {
});

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

test('unmockedFunction should log into console', () => {
// Write your test here
const consoleSpy = jest.spyOn(console, 'log');
unmockedFunction();
expect(consoleSpy).toBeCalled();
});
});
Loading