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
83 changes: 26 additions & 57 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion src/01-simple-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
// 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: 2, b: 3, action: Action.Add };
const result = simpleCalculator(input);
expect(result).toBe(5);
});

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

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

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

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

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

test('should return null for invalid arguments', () => {
// Write your test here
const input = { a: '12', b: 2, action: Action.Add };
const result = simpleCalculator(input);
expect(result).toBeNull();
});
});
40 changes: 31 additions & 9 deletions src/02-table-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
// Uncomment the code below and write your tests
/* import { simpleCalculator, Action } from './index';
import { Action, simpleCalculator } 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 },
// continue cases for other actions
{ a: 3, b: 2, action: Action.Subtract, expected: 1 },
{ a: 3, b: 3, action: Action.Subtract, expected: 0 },
{ a: 9, b: 6, action: Action.Subtract, expected: 3 },
{ a: 2, b: 2, action: Action.Multiply, expected: 4 },
{ a: 8, b: 2, action: Action.Multiply, expected: 16 },
{ a: 3, b: 2, action: Action.Multiply, expected: 6 },
{ a: 12, b: 4, action: Action.Divide, expected: 3 },
{ a: 8, b: 2, action: Action.Divide, expected: 4 },
{ a: 9, b: 3, action: Action.Divide, expected: 3 },
{ a: 12, b: 2, action: Action.Exponentiate, expected: 144 },
{ a: 2, b: 3, action: Action.Exponentiate, expected: 8 },
{ a: 3, b: 2, action: Action.Exponentiate, expected: 9 },
// Invalid cases
{ a: 12, b: 2, action: 'divide', expected: null },
{ a: 12, b: 2, action: 'multiply', expected: null },
{ a: 110, b: 2, action: 'add', expected: null },
{ a: '112', b: '2', action: Action.Add, expected: null },
{ a: '113', b: '20', action: Action.Multiply, expected: null },
{ a: '114', b: '28', action: Action.Divide, 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)(
'given a: $a, b: $b, action: $action, returns $expected',
({ a, b, action, expected }) => {
expect(simpleCalculator({ a, b, action })).toBe(expected);
},
);
// Consider to use Jest table tests API to test all cases above
});
15 changes: 14 additions & 1 deletion src/03-error-handling-async/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
// 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 result = await resolveValue('Hello, World!');
expect(result).toBe('Hello, World!');
});
});

describe('throwError', () => {
test('should throw error with provided message', () => {
// Write your test here
const PROVIDED_MESSAGE = 'Invalid input';
expect(() => throwError(PROVIDED_MESSAGE)).toThrow(PROVIDED_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);
});
});
57 changes: 56 additions & 1 deletion src/04-test-class/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,99 @@
// Uncomment the code below and write your tests
// import { getBankAccount } from '.';
jest.mock('lodash', () => ({
random: jest.fn(),
}));

import { random } from 'lodash';
import {
getBankAccount,
InsufficientFundsError,
SynchronizationFailedError,
TransferFailedError,
} from '.';

const mockedRandom = jest.mocked(random);

describe('BankAccount', () => {
afterEach(() => {
mockedRandom.mockReset();
});

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

test('should throw InsufficientFundsError error when withdrawing more than balance', () => {
// Write your test here
const account = getBankAccount(10);
expect(() => account.withdraw(20)).toThrow(InsufficientFundsError);
});

test('should throw error when transferring more than balance', () => {
// Write your test here
const account = getBankAccount(10);
const secondAccount = getBankAccount(10);
expect(() => account.transfer(20, secondAccount)).toThrow(
InsufficientFundsError,
);
});

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

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

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

test('should transfer money', () => {
// Write your test here
const account = getBankAccount(30);
const secondAccount = getBankAccount(10);
account.transfer(10, secondAccount);
expect(account.getBalance()).toBe(20);
expect(secondAccount.getBalance()).toBe(20);
});

test('fetchBalance should return number in case if request did not failed', async () => {
// Write your tests here
const account = getBankAccount(30);
mockedRandom.mockReturnValueOnce(38).mockReturnValueOnce(1);
const balance = await account.fetchBalance();
expect(typeof balance).toBe('number');
expect(balance).not.toBeNull();
expect(balance).toBe(38);
});

test('should set new balance if fetchBalance returned number', async () => {
// Write your tests here
const account = getBankAccount(30);
mockedRandom.mockReturnValueOnce(88).mockReturnValueOnce(1);
await account.synchronizeBalance();
expect(typeof account.getBalance()).toBe('number');
expect(account.getBalance()).not.toBeNull();
expect(account.getBalance()).toBe(88);
});

test('should throw SynchronizationFailedError if fetchBalance returned null', async () => {
// Write your tests here
const account = getBankAccount(30);
mockedRandom.mockReturnValueOnce(13).mockReturnValueOnce(0);
await expect(account.synchronizeBalance()).rejects.toThrow(
SynchronizationFailedError,
);
});
});
28 changes: 26 additions & 2 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 {
...originalModule,
mockOne: jest.fn(),
mockTwo: jest.fn(),
mockThree: jest.fn(),
};
});

describe('partial mocking', () => {
Expand All @@ -12,9 +20,25 @@ describe('partial mocking', () => {

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

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').mockImplementation(() => {});
unmockedFunction();
expect(consoleSpy).toHaveBeenCalledWith('I am not mocked');
consoleSpy.mockRestore();
});
});
Loading