-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.int.test.js
74 lines (59 loc) · 2.35 KB
/
Account.int.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
const Account = require('./Account')
const fsExtra = require('fs-extra')
const fs = require('fs')
describe('Account Integration Tests', () => {
afterEach(() => {
fsExtra.emptyDirSync('accounts')
})
test('should successfully create a new account with balance 0', async () => {
const name = 'Clark_Create'
const account = await Account.create(name)
expect(account.name).toBe(name)
expect(account.balance).toBe(0)
expect(fs.readFileSync(account.filePath).toString()).toBe('0')
})
test('should successfully deposit into an existing account', async () => {
const name = 'Davis_Deposit'
const account = await Account.create(name)
await account.deposit(100)
expect(account.balance).toBe(100)
expect(fs.readFileSync(account.filePath).toString()).toBe('100')
await account.deposit(50)
expect(account.balance).toBe(150)
expect(fs.readFileSync(account.filePath).toString()).toBe('150')
await account.deposit(25)
expect(account.balance).toBe(175)
expect(fs.readFileSync(account.filePath).toString()).toBe('175')
})
test('should successfully withdraw from an existing account', async () => {
const name = 'White_Withdraw'
const account = await Account.create(name)
await account.deposit(100)
await account.withdraw(25)
expect(account.balance).toBe(75)
expect(fs.readFileSync(account.filePath).toString()).toBe('75')
})
test('should prevent withdraw when insufficient funds', async () => {
const name = 'White_Withdraw_Insufficient'
const account = await Account.create(name)
const startingBalance = 25
const withdrawAmount = 100
await account.deposit(startingBalance)
expect(account.withdraw(withdrawAmount)).rejects.toThrow()
expect(account.balance).toBe(startingBalance)
})
test('should find an existing account', async () => {
const name = 'Flores_Find'
const balance = 25
fs.writeFileSync(`accounts/${name}.txt`, balance.toString())
const foundAccount = await Account.find(name)
expect(foundAccount.name).toBe(name)
expect(foundAccount.balance).toBe(balance)
expect(fs.readFileSync(foundAccount.filePath).toString()).toBe('25')
})
test('should return undefined if account not found', async () => {
const name = 'Flores_Undefined'
const account = await Account.find(name)
expect(account).toBeUndefined()
})
})