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
35 changes: 26 additions & 9 deletions src/01-simple-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
// 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 input = { a: 3, b: 5, action: Action.Add };
const result = simpleCalculator(input);
expect(result).toBe(8);
});

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

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

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

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

test('should return null for invalid action', () => {
// Write your test here
const input = { a: 5, b: 10, action: 'invalid_action' };
const result = simpleCalculator(input);
expect(result).toBeNull();
});

test('should return null for invalid arguments', () => {
// Write your test here
const input1 = { a: 'string', b: 10, action: Action.Add };
const input2 = { a: 5, b: null, action: Action.Subtract };
const input3 = { a: 5, b: 10, action: 123 }; // invalid action type

expect(simpleCalculator(input1)).toBeNull();
expect(simpleCalculator(input2)).toBeNull();
expect(simpleCalculator(input3)).toBeNull();
});
});
42 changes: 30 additions & 12 deletions src/02-table-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
// 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: 5, b: 3, action: Action.Subtract, expected: 2 },
{ a: 6, b: 2, action: Action.Subtract, expected: 4 },
{ a: 3, b: 3, action: Action.Multiply, expected: 9 },
{ a: 8, b: 2, action: Action.Multiply, expected: 16 },
{ a: 10, b: 2, action: Action.Divide, expected: 5 },
{ a: 9, b: 3, action: Action.Divide, expected: 3 },
{ a: 2, b: 3, action: Action.Exponentiate, expected: 8 },
{ a: 5, b: 2, action: Action.Exponentiate, expected: 25 },
];

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 calculate $a $action $b to be $expected',
({ a, b, action, expected }) => {
const result = simpleCalculator({ a, b, action });
expect(result).toBe(expected);
},
);

test('should return null for invalid action', () => {
const result = simpleCalculator({ a: 1, b: 2, action: 'invalid' });
expect(result).toBeNull();
});

test('should return null for invalid arguments', () => {
const result = simpleCalculator({ a: '1', b: 2, action: Action.Add });
expect(result).toBeNull();
});
// Consider to use Jest table tests API to test all cases above
});
});
28 changes: 21 additions & 7 deletions src/03-error-handling-async/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
// 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 = 'test';
const result = await resolveValue(value);
expect(result).toBe(value);
});
});

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

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

describe('throwCustomError', () => {
test('should throw custom error', () => {
// Write your test here
expect(() => throwCustomError()).toThrowError(MyAwesomeError);
expect(() => throwCustomError()).toThrowError(
'This is my awesome custom error!',
);
});
});

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

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

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

test('should throw InsufficientFundsError error when withdrawing more than balance', () => {
// Write your test here
const account = getBankAccount(50);
expect(() => account.withdraw(100)).toThrow(InsufficientFundsError);
expect(() => account.withdraw(100)).toThrow(
'Insufficient funds: cannot withdraw more than 50',
);
});

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

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

test('should throw error when transferring to the same account', () => {
// Write your test here
const account = getBankAccount(50);
expect(() => account.transfer(10, account)).toThrow(TransferFailedError);
expect(() => account.transfer(10, account)).toThrow('Transfer failed');
});

test('should deposit money', () => {
// Write your test here
const account = getBankAccount(50);
account.deposit(50);
expect(account.getBalance()).toBe(100);
});

test('should withdraw money', () => {
// Write your test here
const account = getBankAccount(100);
account.withdraw(50);
expect(account.getBalance()).toBe(50);
});

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

account1.transfer(50, account2);
expect(account1.getBalance()).toBe(50);
expect(account2.getBalance()).toBe(100);
});

test('fetchBalance should return number in case if request did not failed', async () => {
// Write your tests here
test('fetchBalance should return number in case request did not fail', async () => {
(random as jest.Mock).mockReturnValueOnce(75).mockReturnValueOnce(1);
const account = getBankAccount(100);
const balance = await account.fetchBalance();
expect(balance).toBe(75);
});

test('should set new balance if fetchBalance returned number', async () => {
// Write your tests here
(random as jest.Mock).mockReturnValueOnce(75).mockReturnValueOnce(1);
const account = getBankAccount(100);
await account.synchronizeBalance();
expect(account.getBalance()).toBe(75);
});

test('should throw SynchronizationFailedError if fetchBalance returned null', async () => {
// Write your tests here
(random as jest.Mock).mockReturnValueOnce(50).mockReturnValueOnce(0);
const account = getBankAccount(100);
await expect(account.synchronizeBalance()).rejects.toThrow(
SynchronizationFailedError,
);
});
});
27 changes: 22 additions & 5 deletions src/05-partial-mocking/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// 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');
return {
mockOne: jest.fn(),
mockTwo: jest.fn(),
mockThree: jest.fn(),
unmockedFunction: jest.requireActual('./index').unmockedFunction,
};
});

describe('partial mocking', () => {
Expand All @@ -11,10 +15,23 @@ 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.toHaveBeenCalledWith('foo');
expect(consoleSpy).not.toHaveBeenCalledWith('bar');
expect(consoleSpy).not.toHaveBeenCalledWith('baz');

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