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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Logs
logs
*.log
.idea
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Expand Down
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, RawCalculatorInput } from './index';

describe('simpleCalculator tests', () => {
let value: RawCalculatorInput;

beforeEach(() => {
value = { a: 4, b: 2, action: Action.Add };
});

test('should add two numbers', () => {
// Write your test here
expect(simpleCalculator(value)).toEqual(6);
});

test('should subtract two numbers', () => {
// Write your test here
value.action = Action.Subtract;

expect(simpleCalculator(value)).toEqual(2);
});

test('should multiply two numbers', () => {
// Write your test here
value.action = Action.Multiply;

expect(simpleCalculator(value)).toEqual(8);
});

test('should divide two numbers', () => {
// Write your test here
value.action = Action.Divide;

expect(simpleCalculator(value)).toEqual(2);
});

test('should exponentiate two numbers', () => {
// Write your test here
value.action = Action.Exponentiate;

expect(simpleCalculator(value)).toEqual(16);
});

test('should return null for invalid action', () => {
// Write your test here
value.action = 'unknown';

expect(simpleCalculator(value)).toEqual(null);
});

test('should return null for invalid arguments', () => {
// Write your test here
value.a = 'unknown';

expect(simpleCalculator(value)).toEqual(null);
});
});
2 changes: 1 addition & 1 deletion src/01-simple-tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export enum Action {
Exponentiate = '^',
}

type RawCalculatorInput = {
export type RawCalculatorInput = {
a: unknown;
b: unknown;
action: unknown;
Expand Down
50 changes: 39 additions & 11 deletions src/02-table-tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
// 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
]; */
{
input: { a: 4, b: 2, action: Action.Add },
expected: 6,
testName: 'add two numbers',
},
{
input: { a: 4, b: 2, action: Action.Subtract },
expected: 2,
testName: 'subtract two numbers',
},
{
input: { a: 4, b: 2, action: Action.Multiply },
expected: 8,
testName: 'multiply two numbers',
},
{
input: { a: 4, b: 2, action: Action.Divide },
expected: 2,
testName: 'divide two numbers',
},
{
input: { a: 4, b: 2, action: Action.Exponentiate },
expected: 16,
testName: 'exponential two numbers',
},
{
input: { a: 4, b: 2, action: 'unknown' },
expected: null,
testName: 'return null if action invalid',
},
{
input: { a: 4, b: 'unknown', action: Action.Exponentiate },
expected: null,
testName: 'return null if value invalid',
},
];

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);
it.each(testCases)('should $testName', ({ input, expected }) => {
expect(simpleCalculator(input)).toEqual(expected);
});
// Consider to use Jest table tests API to test all cases above
});
21 changes: 14 additions & 7 deletions src/03-error-handling-async/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
// 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
await expect(resolveValue(4)).resolves.toBe(4);
});
});

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

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()).toThrow(new MyAwesomeError());
});
});

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

import lodash from 'lodash';

describe('BankAccount', () => {
const initialFounds = 1000;
let account: BankAccount;

beforeEach(() => {
account = getBankAccount(initialFounds);
});

test('should create account with initial balance', () => {
// Write your test here
expect(account).toBeDefined();
expect(account.getBalance()).toEqual(initialFounds);
});

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

test('should throw error when transferring more than balance', () => {
// Write your test here
const newAccount = new BankAccount(initialFounds);

expect(() => account.transfer(2000, newAccount)).toThrow(
new InsufficientFundsError(initialFounds),
);
});

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

test('should deposit money', () => {
// Write your test here
const deposit = 20;

expect(account.deposit(deposit).getBalance()).toEqual(
initialFounds + deposit,
);
});

test('should withdraw money', () => {
// Write your test here
const withdraw = 20;

expect(account.withdraw(withdraw).getBalance()).toEqual(
initialFounds - withdraw,
);
});

test('should transfer money', () => {
// Write your test here
const newAccount = new BankAccount(initialFounds);
const transferredMoney = 20;

account.transfer(transferredMoney, newAccount);

expect(account.getBalance()).toEqual(initialFounds - transferredMoney);
expect(newAccount.getBalance()).toEqual(initialFounds + transferredMoney);
});

test('fetchBalance should return number in case if request did not failed', async () => {
// Write your tests here
jest.spyOn(lodash, 'random').mockReturnValueOnce(50).mockReturnValueOnce(1);

await account.fetchBalance().then((result) => {
expect(typeof result).toBe('number');
});
});

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

jest
.spyOn(lodash, 'random')
.mockReturnValueOnce(currentFounds)
.mockReturnValueOnce(1);

await account.synchronizeBalance();

expect(account.getBalance()).toEqual(currentFounds);
});

test('should throw SynchronizationFailedError if fetchBalance returned null', async () => {
// Write your tests here
jest
.spyOn(lodash, 'random')
.mockReturnValueOnce(initialFounds)
.mockReturnValueOnce(0);

await expect(() => account.synchronizeBalance()).rejects.toThrow(
new SynchronizationFailedError(),
);
});
});
17 changes: 11 additions & 6 deletions src/05-partial-mocking/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
// 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');
});
jest.mock('./index', () => ({
...jest.requireActual<typeof import('./index')>('./index'),
mockOne: jest.fn(),
mockTwo: jest.fn(),
mockThree: jest.fn(),
}));

describe('partial mocking', () => {
afterAll(() => {
jest.unmock('./index');
});

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

test('unmockedFunction should log into console', () => {
// Write your test here
unmockedFunction();
});
});
Loading