-
Notifications
You must be signed in to change notification settings - Fork 0
/
Account.unit.test.js
83 lines (76 loc) · 2.64 KB
/
Account.unit.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const Account = require('./Account')
const FileSystem = require('./FileSystem')
//create a dummy account
async function createAccount(name, balance) {
const spy = jest.spyOn(FileSystem, 'read').mockReturnValueOnce(Promise.resolve(balance))
const account = await Account.find(name)
spy.mockRestore()
return account
}
describe('Account Unit Tests', () => {
beforeEach(() => {
jest.restoreAllMocks()
})
test('should successfully create a new account with balance 0', async () => {
//given
const name = 'Clark_Create'
const spy = jest.spyOn(FileSystem, 'write')
//when
const account = await Account.create(name)
//then
expect(account.name).toBe(name)
expect(account.balance).toBe(0)
expect(account.filePath).toBe(`accounts/${name}.txt`)
expect(spy).toBeCalledWith(account.filePath, 0)
// spy.mockRestore()
})
test('should successfully deposit into an existing account', async () => {
//given
const name = 'Davis_Deposit'
const depositAmount = 100
const spy = jest.spyOn(FileSystem, 'write')
const account = await Account.create(name)
//when
await account.deposit(depositAmount)
//then
expect(account.balance).toBe(depositAmount)
expect(account.filePath).toBe(`accounts/${name}.txt`)
expect(spy).toBeCalledWith(account.filePath, depositAmount)
})
test('should successfully withdraw from an existing account', async () => {
//given
const name = 'White_Withdraw'
const startingBalance = 100
const withdrawAmount = 25
const spy = jest.spyOn(FileSystem, 'write')
const account = await createAccount(name, startingBalance)
//when
await account.withdraw(withdrawAmount)
//then
expect(account.balance).toBe(startingBalance - withdrawAmount)
expect(account.filePath).toBe(`accounts/${name}.txt`)
expect(spy).toBeCalledWith(account.filePath, startingBalance - withdrawAmount)
})
test('should prevent withdraw when insufficient funds', async () => {
//given
const name = 'White_Withdraw'
const startingBalance = 25
const withdrawAmount = 100
const spy = jest.spyOn(FileSystem, 'write')
const account = await createAccount(name, startingBalance)
//then
await expect(account.withdraw(withdrawAmount)).rejects.toThrow()
expect(account.balance).toBe(startingBalance)
expect(spy).not.toBeCalled
})
test('should return undefined if account not found', async () => {
//given
const name = 'Flores_Undefined'
const spy = jest.spyOn(FileSystem, 'read')
//when
const account = await Account.find(name)
//then
expect(spy).toHaveBeenCalled
expect(account).toBeUndefined()
})
})