---
# System prepended metadata

title: JS筆記 jest (下)

---

# JS筆記 jest (下)

接下來把剩下的規格寫成測試

不可以存 0 元或是小於 0 元的金額（越存錢越少！

*bank.test.js*
```javascript
test("不可以存 0 元或是小於 0 元的金額（越存錢越少！",()=>{
    const account = new BankAccount(5)
    account.deposit(-10)
    expect(account.balance).toBe(5);
}) 
```
存的錢不該是負數，帳戶的金額應該不變

*bank.js*
```javascript
class BankAccount {
    constructor(amount){  
        this.amount = amount   
    }
    deposit(money){
        if (money > 0 ){
            this.amount += money
        }       
    }
    get balance(){
        return this.amount
    }
}
module.exports = BankAccount
```
![](https://i.imgur.com/IVHogFD.png)

---
可以領錢

*bank.test.js*
```javascript
test("可以領錢",()=>{
    const account = new BankAccount(25)
    account.withDraw(10)
    expect(account.balance).toBe(15);
}) 
```

*bank.js*
```javascript
class BankAccount {
    constructor(amount){  
        this.amount = amount   
    }
    deposit(money){
        if (money > 0 ){
            this.amount += money
        }       
    }
    withDraw(money){
        this.amount -= money
    }
    get balance(){
        return this.amount
    }
}
module.exports = BankAccount
```



---
不能領 0 元或是小於 0 元的金額

*bank.test.js*
```javascript
test("不能領 0 元或是小於 0 元的金額",()=>{
    const account = new BankAccount(25)
    account.withDraw(-10)
    expect(account.balance).toBe(25);
}) 
```


*bank.js*
```javascript
class BankAccount {
  constructor(amount) {
    this.amount = amount;
  }
  deposit(money) {
    if (money > 0) {
      this.amount += money;
    }
  }
  withDraw(money) {
    if (money > 0) {
      this.amount -= money;
    }
  }
  get balance() {
    return this.amount;
  }
}
module.exports = BankAccount;

```


---
不能領超過本身餘額

*bank.test.js*
```javascript
test("不能領超過本身餘額",()=>{
    const account = new BankAccount(25)
    account.withDraw(30)
    expect(account.balance).toBe(25);
}) 
```

*bank.js*
```javascript
class BankAccount {
  constructor(amount) {
    this.amount = amount;
  }
  deposit(money) {
    if (money > 0) {
      this.amount += money;
    }
  }
  withDraw(money) {
    if (money > 0 && this.amount > money) {
      this.amount -= money;
    }
  }
  get balance() {
    return this.amount;
  }
}
module.exports = BankAccount;

```

![](https://i.imgur.com/4tuxfVl.png)
