Skip to content

Commit

Permalink
[2단계 - 자판기] 병민(윤병인) 미션 제출합니다. (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
airman5573 authored Apr 14, 2022
1 parent 960db37 commit a7ee26f
Show file tree
Hide file tree
Showing 72 changed files with 5,252 additions and 575 deletions.
10 changes: 10 additions & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"testFiles": "**/*.spec.js",
"screenshotOnRunFailure": false,
"video": false,
"pluginsFile": false,
"supportFile": "./cypress/support",
"fixturesFolder": "./cypress/fixtures",
"defaultCommandTimeout": 30000,
"baseUrl": "http://localhost:9000"
}
6 changes: 6 additions & 0 deletions cypress/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['plugin:cypress/recommended'],
rules: {
'no-unused-expressions': 0, // chai assertions trigger this rule
},
};
46 changes: 46 additions & 0 deletions cypress/integration/charge-money.fail.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ERROR_MESSAGE, COIN, MONEY } from '../../src/constants';

describe('잔돈을 충전한다 - 실패케이스', () => {
const email = `${Date.now()}@gmail.com`;
const name = '윤병인';
const password = 'Abcde123!';

before(() => {
cy.register(email, name, password);
cy.login(email, password);
});

beforeEach(() => {
cy.restoreLocalStorage();
});

it('빈 값을 허용하지 않는다', () => {
const money = '';
cy.chargeMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.EMPTY_CHARGE_MONEY);
});

it('숫자만 입력 가능하다.', () => {
const money = '1,000';
cy.chargeMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.NOT_NUMBER_CHARGE_MONEY);
});

it('양수만 입력 가능하다', () => {
const money = '-1000';
cy.chargeMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.NEGATIVE_CHARGE_MONEY);
});

it(`${COIN.MIN_UNIT.toLocaleString()}원으로 나누어 떨어지는 금액만 투입할 수 있다`, () => {
const money = '1001';
cy.chargeMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.NOT_DIVIDED_BY_TEN_CHARGE_MONEY);
});

it(`잔돈으로 보유할 수 있는 최대 금액은 ${MONEY.MAX.toLocaleString()}원이다`, () => {
const money = `${MONEY.MAX + 1000}`;
cy.chargeMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.OVER_MAX_CHARGE_MONEY);
});
});
30 changes: 30 additions & 0 deletions cypress/integration/charge-money.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { testid } from '../support/utils';
import { COIN } from '../../src/constants';
import { krLocaleStringToInt } from '../../src/utils';

describe('잔돈을 충전한다 - 성공케이스', () => {
const email = `${Date.now()}@gmail.com`;
const name = '윤병인';
const password = 'Abcde123!';
const chargedMoney = 3000;

before(() => {
cy.register(email, name, password);
cy.login(email, password);
});

it('잔돈을 충전한다', () => {
cy.restoreLocalStorage();
cy.chargeMoney(chargedMoney);
cy.get(testid`charged-money`).should('have.text', chargedMoney.toLocaleString());

cy.get(testid`changes-inventory`).then(($el) => {
console.log($el[0]);
const total = COIN.UNITS.reduce((acc, unit) => {
const count = krLocaleStringToInt($el.find(testid(`coin-unit-${unit}-quantity`)).text());
return acc + count * unit;
}, 0);
cy.get(testid`charged-money`).should('have.text', total.toLocaleString());
});
});
});
101 changes: 101 additions & 0 deletions cypress/integration/product-manage.fail.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { ERROR_MESSAGE } from '../../src/constants';

describe('상품을 관리 한다 - 실패케이스', () => {
const email = `${Date.now()}@gmail.com`;
const name = '윤병인';
const password = 'Abcde123!';
const _product = {
name: '콜라',
price: 1000,
quantity: 10,
};

before(() => {
cy.register(email, name, password);
cy.login(email, password);
});

beforeEach(() => {
cy.restoreLocalStorage();
cy.removeProducts();
});

it('상품명이 빈 경우', () => {
const product = { ..._product, name: '' };
cy.addProduct(product);
cy.checkToastErrorMessage(ERROR_MESSAGE.EMPTY_PRODUCT_NAME);
});

it('상품명이 너무 긴 경우', () => {
const product = { ..._product, name: '가나다라마바사123456' };
cy.addProduct(product);
cy.checkToastErrorMessage(ERROR_MESSAGE.OVER_MAX_LENGTH_PRODUCT_NAME);
});

it('상품명이 중복된 경우', () => {
cy.addProduct(_product);
cy.addProduct(_product);
cy.checkToastErrorMessage(ERROR_MESSAGE.DUPLICATE_PRDUCT_NAME);
});

it('상품가격이 빈 경우', () => {
const product = { ..._product, name: '환타', price: '' };
cy.addProduct(product);
cy.checkToastErrorMessage(ERROR_MESSAGE.EMPTY_PRODUCT_PRICE);
});

it('상품수량이 빈 경우', () => {
const product = { ..._product, name: '밀키스', quantity: '' };
cy.addProduct(product);
cy.checkToastErrorMessage(ERROR_MESSAGE.EMPTY_PRODUCT_QUANTITY);
});

// 상품 수정

it('상품 수정 - 상품명이 빈 경우', () => {
cy.addProduct(_product);
cy.findProduct(_product.name);
cy.get('@productIndex').then((index) => {
cy.editProduct(index, true, { ..._product, name: '' });
cy.checkToastErrorMessage(ERROR_MESSAGE.EMPTY_PRODUCT_NAME);
});
});

it('상품 수정 - 상품명이 너무 긴 경우', () => {
cy.addProduct(_product);
cy.findProduct(_product.name);
cy.get('@productIndex').then((index) => {
cy.editProduct(index, true, { ..._product, name: '가나다라마바사123456' });
cy.checkToastErrorMessage(ERROR_MESSAGE.OVER_MAX_LENGTH_PRODUCT_NAME);
});
});

it('상품 수정 - 상품명이 중복된 경우', () => {
cy.addProduct(_product);
const newProduct = { ..._product, name: '사이다' };
cy.addProduct(newProduct);
cy.findProduct(_product.name);
cy.get('@productIndex').then((index) => {
cy.editProduct(index, true, newProduct);
cy.checkToastErrorMessage(ERROR_MESSAGE.DUPLICATE_PRDUCT_NAME);
});
});

it('상품 수정 - 상품가격이 빈 경우', () => {
cy.addProduct(_product);
cy.findProduct(_product.name);
cy.get('@productIndex').then((index) => {
cy.editProduct(index, true, { ..._product, price: '' });
cy.checkToastErrorMessage(ERROR_MESSAGE.EMPTY_PRODUCT_PRICE);
});
});

it('상품 수정 - 상품수량이 빈 경우', () => {
cy.addProduct(_product);
cy.findProduct(_product.name);
cy.get('@productIndex').then((index) => {
cy.editProduct(index, true, { ..._product, quantity: '' });
cy.checkToastErrorMessage(ERROR_MESSAGE.EMPTY_PRODUCT_QUANTITY);
});
});
});
54 changes: 54 additions & 0 deletions cypress/integration/product-manage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { testid } from '../support/utils';

describe('상품을 관리 한다 - 성공케이스', () => {
const email = `${Date.now()}@gmail.com`;
const name = '윤병인';
const password = 'Abcde123!';
const product = {
name: '콜라',
price: 1000,
quantity: 10,
};

before(() => {
cy.register(email, name, password);
cy.login(email, password);
});

beforeEach(() => {
cy.restoreLocalStorage();
cy.removeProducts();
});

it('상품을 추가한다', () => {
cy.addProduct(product);
cy.get(testid`product-inventory-table`)
.find('tbody tr td:first-of-type span')
.contains(product.name);
});

it('상품을 수정한다', () => {
const product = {
name: '사이다',
price: 1150,
quantity: 8,
};
cy.addProduct(product);
cy.editProduct(0, false, product);
cy.get(`product-inventory ${testid`product-name`}`).should('have.text', product.name);
cy.get(`product-inventory ${testid`product-price`}`).should(
'have.text',
product.price.toLocaleString()
);
cy.get(`product-inventory ${testid`product-quantity`}`).should(
'have.text',
product.quantity.toLocaleString()
);
});

it('상품을 삭제한다', () => {
cy.addProduct(product);
cy.get(testid`delete-btn`).click();
cy.get('product-inventory table').should('not.exist');
});
});
46 changes: 46 additions & 0 deletions cypress/integration/purchase-product.fail.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ERROR_MESSAGE, COIN, INSERT_MONEY } from '../../src/constants';

describe('상품을 구매한다 - 실패케이스', () => {
const email = `${Date.now()}@gmail.com`;
const name = '윤병인';
const password = 'Abcde123!';

before(() => {
cy.register(email, name, password);
cy.login(email, password);
});

beforeEach(() => {
cy.restoreLocalStorage();
});

it('돈 투입시 빈 값을 허용하지 않는다', () => {
const money = '';
cy.insertMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.EMPTY_INSERT_MONEY);
});

it('숫자만 입력 가능하다.', () => {
const money = '1,000';
cy.insertMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.NOT_NUMBER_INSERT_MONEY);
});

it('양수만 입력 가능하다', () => {
const money = '-1000';
cy.insertMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.NEGATIVE_INSERT_MONEY);
});

it(`${COIN.MIN_UNIT.toLocaleString()}원으로 나누어 떨어지는 금액만 투입할 수 있다`, () => {
const money = '1001';
cy.insertMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.NOT_DIVIDED_BY_TEN_INSERT_MONEY);
});

it(`충전할 수 있는 최대 금액은 ${INSERT_MONEY.MAX.toLocaleString()}원이다`, () => {
const money = `${INSERT_MONEY.MAX + 1000}`;
cy.insertMoney(money);
cy.checkToastErrorMessage(ERROR_MESSAGE.OVER_MAX_INSERT_MONEY);
});
});
72 changes: 72 additions & 0 deletions cypress/integration/purchase-product.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { coinToMoney, krLocaleStringToInt } from '../../src/utils';
import { testid } from '../support/utils';

describe('상품을 구매한다 - 성공케이스', () => {
const email = `${Date.now()}@gmail.com`;
const name = '윤병인';
const password = 'Abcde123!';
const product = {
name: '콜라',
price: 1000,
quantity: 10,
};
const chargedMoney = 3000;
const insertedMoney = 3000;

before(() => {
cy.register(email, name, password);
cy.login(email, password);
});

beforeEach(() => {
cy.restoreLocalStorage();
});

it('돈을 투입한다', () => {
cy.insertMoney(insertedMoney);
cy.get(testid`inserted-money`)
.invoke('text')
.should('eq', insertedMoney.toLocaleString());
});

it('상품을 구매한다', () => {
cy.addProduct(product); // 상품 추가
cy.chargeMoney(chargedMoney); // 잔돈 충전
cy.purchaseProduct(product);
cy.get(testid`purchase-btn`).click();
cy.get(`${testid`product-price`} span`)
.invoke('text')
.then((_price) => {
const price = krLocaleStringToInt(_price);
cy.get(testid`inserted-money`)
.invoke('text')
.then((_insertedMoney) => {
const isPurchased = insertedMoney - price === krLocaleStringToInt(_insertedMoney);
expect(isPurchased).to.be.true;
});
});
});

it('잔돈을 반환한다', () => {
cy.releaseCoin();
cy.get(testid`release-coin-table`).then(($table) => {
const $trs = $table.find('tbody tr');
const coins = {};
$trs.each((_, $tr) => {
const $tds = $tr.querySelectorAll('td');
const [$unit, $count] = $tds;
const unit = krLocaleStringToInt($unit.querySelector('span').textContent);
const count = krLocaleStringToInt($count.querySelector('span').textContent);
coins[unit] = count;
});
const changes = coinToMoney(coins);
cy.get(testid`inserted-money`)
.invoke('text')
.then((_insertedMoney) => {
const isChangesCorrect =
insertedMoney === krLocaleStringToInt(_insertedMoney) + product.price + changes;
expect(isChangesCorrect).to.be.true;
});
});
});
});
Loading

0 comments on commit a7ee26f

Please sign in to comment.