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
50 changes: 39 additions & 11 deletions src/01-simple-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,60 @@
// 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 a = 2;
const b = 10;
const sum = simpleCalculator({ a, b, action: Action.Add });
expect(sum).toBe(a + b);
}, 30);

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

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

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

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

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

test('should return null for invalid arguments', () => {
// Write your test here
const invalidA = simpleCalculator({ a: 'hi', b: 2, action: Action.Add });
const invalidB = simpleCalculator({
a: 10,
b: 'invalid',
action: Action.Subtract,
});
const invalidArgs = simpleCalculator({
a: 'hi',
b: 'invalid',
action: 'action',
});
expect(invalidA).toBeNull();
expect(invalidB).toBeNull();
expect(invalidArgs).toBeNull();
});
});
});
34 changes: 21 additions & 13 deletions src/02-table-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
// 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: 3, b: 4, action: Action.Subtract, expected: -1 },
{ a: 2, b: 3, action: Action.Multiply, expected: 6 },
{ a: 4, b: 2, action: Action.Multiply, expected: 8 },
{ a: 6, b: 2, action: Action.Divide, expected: 3 },
{ a: 10, b: 5, action: Action.Divide, expected: 2 },
{ a: 2, b: 3, action: Action.Exponentiate, expected: 8 },
{ a: 3, b: 2, action: Action.Exponentiate, expected: 9 },
];

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 compute $a $action $b = $expected',
({ a, b, action, expected }) => {
const result = simpleCalculator({ a, b, action });
expect(result).toBe(expected);
},
);
});
43 changes: 43 additions & 0 deletions src/03-error-handling-async/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
throwError,
throwCustomError,
resolveValue,
MyAwesomeError,
rejectCustomError,
} from './index';

describe('resolveValue', () => {
test('should resolve provided value', async () => {
await expect(resolveValue('test')).resolves.toBe('test');
});
});

describe('throwError', () => {
test('should throw error with provided message', () => {
expect(() => throwError('Custom error message')).toThrow(
'Custom error message',
);
});

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

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

describe('rejectCustomError', () => {
test('should reject custom error', async () => {
await expect(rejectCustomError()).rejects.toThrow(MyAwesomeError);
await expect(rejectCustomError()).rejects.toThrow(
'This is my awesome custom error!',
);
});
});
30 changes: 0 additions & 30 deletions src/03-error-handling-async/index.test.ts

This file was deleted.

76 changes: 63 additions & 13 deletions src/04-test-class/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,94 @@
// Uncomment the code below and write your tests
// import { getBankAccount } from '.';
import {
getBankAccount,
InsufficientFundsError,
SynchronizationFailedError,
TransferFailedError,
} from '.';
import lodash from 'lodash';

describe('BankAccount', () => {
afterEach(() => {
jest.restoreAllMocks();
});

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 acc1 = getBankAccount(30);
const acc2 = getBankAccount(40);
expect(() => acc1.transfer(50, acc2)).toThrow(InsufficientFundsError);
});

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

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

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 acc1 = getBankAccount(100);
const acc2 = getBankAccount(50);
acc1.transfer(30, acc2);
expect(acc1.getBalance()).toBe(70);
expect(acc2.getBalance()).toBe(80);
});

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

jest
.spyOn(lodash, 'random')
.mockImplementationOnce(() => 100)
.mockImplementationOnce(() => 1);

const result = await account.fetchBalance();
expect(typeof result).toBe('number');
});

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

jest
.spyOn(lodash, 'random')
.mockImplementationOnce(() => 42)
.mockImplementationOnce(() => 1);

await account.synchronizeBalance();
expect(account.getBalance()).toBe(42);
});

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

jest
.spyOn(lodash, 'random')
.mockImplementationOnce(() => 0)
.mockImplementationOnce(() => 0);

await expect(account.synchronizeBalance()).rejects.toThrow(
SynchronizationFailedError,
);
});
});
});
39 changes: 33 additions & 6 deletions src/05-partial-mocking/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
// 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', () => {
let consoleLogSpy: jest.SpyInstance;

beforeEach(() => {
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
});

afterEach(() => {
consoleLogSpy.mockRestore();
});

afterAll(() => {
jest.unmock('./index');
});

test('mockOne, mockTwo, mockThree should not log into console', () => {
// Write your test here
mockOne();
mockTwo();
mockThree();

expect(consoleLogSpy).not.toHaveBeenCalled();
expect(mockOne).toHaveBeenCalled();
expect(mockTwo).toHaveBeenCalled();
expect(mockThree).toHaveBeenCalled();
});

test('unmockedFunction should log into console', () => {
// Write your test here
const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();

unmockedFunction();

expect(consoleLogSpy).toHaveBeenCalledWith('I am not mocked');
});
});
});
Loading