-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[holee] typescript-calculator #2
base: main
Are you sure you want to change the base?
Changes from all commits
2b4c4cc
a7106a8
22e4a32
13f5f8d
e4cd2a7
3c7d913
62f2112
5269a22
2e706bd
517bbe2
8f301d1
aaf5cbb
e0c8df6
9207148
185e02f
85bf0ed
1ab968c
2a78429
33ea372
d00ae27
6b7a4c5
b07c74b
91944e8
041dfb5
04b7bbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
}, | ||
"extends": ["airbnb-base"], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": 12, | ||
"sourceType": "module" | ||
}, | ||
"plugins": ["@typescript-eslint"], | ||
"ignorePatterns": ["dist/", "node_modules/"], | ||
"rules": {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"parser": "typescript", | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"baseUrl": "http://127.0.0.1:5500" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "Using fixtures to represent data", | ||
"email": "[email protected]", | ||
"body": "Fixtures are a great way to mock data for responses to routes" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
const testInputClickEvent = (first, result) => { | ||
for (let item of first) { | ||
cy.get('.digit') | ||
.contains(item) | ||
.click(); | ||
} | ||
cy.get('#total').should('have.text', result); | ||
}; | ||
|
||
const testTwoInputCalculateEvent = (first, oper, second, result) => { | ||
for (let item of first) { | ||
cy.get('.digit') | ||
.contains(item) | ||
.click(); | ||
} | ||
for (let item of oper) { | ||
cy.get('.operations') | ||
.contains(item) | ||
.click(); | ||
} | ||
for (let item of second) { | ||
cy.get('.digit') | ||
.contains(item) | ||
.click(); | ||
} | ||
cy.get('.operations') | ||
.contains('=') | ||
.click(); | ||
cy.get('#total').should('have.text', result); | ||
}; | ||
|
||
describe('initial value', () => { | ||
beforeEach(() => { | ||
cy.visit('/javascript-calculator/'); | ||
}); | ||
it('total value', () => { | ||
cy.get('#total').should('have.text', '0'); | ||
}); | ||
}); | ||
|
||
describe('Render digit when button clicked', () => { | ||
beforeEach(() => { | ||
cy.visit('/javascript-calculator/'); | ||
}); | ||
it('button 0', () => { | ||
testInputClickEvent([0], '0'); | ||
}); | ||
it('button 1', () => { | ||
testInputClickEvent([1], '1'); | ||
}); | ||
it('button 2', () => { | ||
testInputClickEvent([2], '2'); | ||
}); | ||
it('button 3', () => { | ||
testInputClickEvent([3], '3'); | ||
}); | ||
it('button 4', () => { | ||
testInputClickEvent([4], '4'); | ||
}); | ||
it('button 5', () => { | ||
testInputClickEvent([5], '5'); | ||
}); | ||
it('button 6', () => { | ||
testInputClickEvent([6], '6'); | ||
}); | ||
it('button 7', () => { | ||
testInputClickEvent([7], '7'); | ||
}); | ||
it('button 8', () => { | ||
testInputClickEvent([8], '8'); | ||
}); | ||
it('button 9', () => { | ||
testInputClickEvent([9], '9'); | ||
}); | ||
}); | ||
|
||
describe('Render max input length 3 when button click', () => { | ||
beforeEach(() => { | ||
cy.visit('/javascript-calculator/'); | ||
}); | ||
it('button 1 2', () => { | ||
testInputClickEvent([1, 2], '12'); | ||
}); | ||
it('button 4 2', () => { | ||
testInputClickEvent([4, 2], '42'); | ||
}); | ||
it('button 0 2', () => { | ||
testInputClickEvent([0, 2], '2'); | ||
}); | ||
it('button 1 2 3', () => { | ||
testInputClickEvent([1, 2, 3], '123'); | ||
}); | ||
it('button 1 2 3 4', () => { | ||
testInputClickEvent([1, 2, 3, 4], '123'); | ||
}); | ||
}); | ||
|
||
describe('Calculate two input when button click', () => { | ||
beforeEach(() => { | ||
cy.visit('/javascript-calculator/'); | ||
}); | ||
it('button 1 + 2', () => { | ||
testTwoInputCalculateEvent([1], ['+'], [2], '3'); | ||
}); | ||
it('button 1234567 + 1234567', () => { | ||
testTwoInputCalculateEvent( | ||
[1, 2, 3, 4, 5, 6, 7], | ||
['+'], | ||
[1, 2, 3, 4, 5, 6, 7], | ||
'246' | ||
); | ||
}); | ||
it('button 42424242 + 42424242', () => { | ||
testTwoInputCalculateEvent( | ||
[4, 2, 4, 2, 4, 2, 4, 2], | ||
['+'], | ||
[4, 2, 4, 2, 4, 2, 4, 2], | ||
'848' | ||
); | ||
}); | ||
}); | ||
|
||
describe('계산 결과를 표현할 때 소수점 이하는 버림한다.', () => { | ||
beforeEach(() => { | ||
cy.visit('/javascript-calculator/'); | ||
}); | ||
it('button 1 / 3', () => { | ||
testTwoInputCalculateEvent([1], ['/'], [3], '0'); | ||
}); | ||
it('button 3 / 2', () => { | ||
testTwoInputCalculateEvent([3], ['/'], [2], '1'); | ||
}); | ||
it('button 5 / 2', () => { | ||
testTwoInputCalculateEvent([5], ['/'], [2], '2'); | ||
}); | ||
}); | ||
|
||
describe('AC(All Clear) 버튼을 누를때 total을 0으로 변경한다.', () => { | ||
beforeEach(() => { | ||
cy.visit('/javascript-calculator/'); | ||
}); | ||
it('button 1 / 3', () => { | ||
testTwoInputCalculateEvent([5], ['/'], [2], '2'); | ||
cy.get('.modifier').click(); | ||
cy.get('#total').should('have.text', '0'); | ||
}); | ||
it('button 1234567 + 1234567', () => { | ||
testTwoInputCalculateEvent([1, 2, 3, 4], ['+'], [1, 2, 3, 4], '246'); | ||
cy.get('.modifier').click(); | ||
cy.get('#total').should('have.text', '0'); | ||
}); | ||
}); | ||
|
||
describe('operator가 연속으로 나왔을때 한개만 적용되도록 처리', () => { | ||
beforeEach(() => { | ||
cy.visit('/javascript-calculator/'); | ||
}); | ||
it('button 1 /// 3', () => { | ||
testTwoInputCalculateEvent([5], ['/', '/', '/'], [2], '2'); | ||
cy.get('.modifier').click(); | ||
cy.get('#total').should('have.text', '0'); | ||
}); | ||
it('button 4 +++++ 2', () => { | ||
testTwoInputCalculateEvent([4], ['+', '+', '+', '+', '+'], [2], '6'); | ||
cy.get('.modifier').click(); | ||
cy.get('#total').should('have.text', '0'); | ||
}); | ||
}); | ||
|
||
describe('operator가 2번이상 나왔을때 한개만 적용되도록 처리', () => { | ||
beforeEach(() => { | ||
cy.visit('/javascript-calculator/'); | ||
}); | ||
it('button 1 + 3 + =', () => { | ||
cy.get('.digit') | ||
.contains('4') | ||
.click(); | ||
cy.get('.operations') | ||
.contains('+') | ||
.click(); | ||
cy.get('.digit') | ||
.contains('2') | ||
.click(); | ||
cy.get('.operations') | ||
.contains('+') | ||
.click(); | ||
cy.get('.operations') | ||
.contains('=') | ||
.click(); | ||
cy.get('#total').should('have.text', '6'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// <reference types="cypress" /> | ||
// *********************************************************** | ||
// This example plugins/index.js can be used to load plugins | ||
// | ||
// You can change the location of this file or turn off loading | ||
// the plugins file with the 'pluginsFile' configuration option. | ||
// | ||
// You can read more here: | ||
// https://on.cypress.io/plugins-guide | ||
// *********************************************************** | ||
|
||
// This function is called when a project is opened or re-opened (e.g. due to | ||
// the project's config changing) | ||
|
||
/** | ||
* @type {Cypress.PluginConfig} | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
module.exports = (on, config) => { | ||
// `on` is used to hook into various events Cypress emits | ||
// `config` is the resolved Cypress config | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// *********************************************** | ||
// This example commands.js shows you how to | ||
// create various custom commands and overwrite | ||
// existing commands. | ||
// | ||
// For more comprehensive examples of custom | ||
// commands please read more here: | ||
// https://on.cypress.io/custom-commands | ||
// *********************************************** | ||
// | ||
// | ||
// -- This is a parent command -- | ||
// Cypress.Commands.add('login', (email, password) => { ... }) | ||
// | ||
// | ||
// -- This is a child command -- | ||
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... }) | ||
// | ||
// | ||
// -- This is a dual command -- | ||
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... }) | ||
// | ||
// | ||
// -- This will overwrite an existing command -- | ||
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// *********************************************************** | ||
// This example support/index.js is processed and | ||
// loaded automatically before your test files. | ||
// | ||
// This is a great place to put global configuration and | ||
// behavior that modifies Cypress. | ||
// | ||
// You can change the location of this file or turn off | ||
// automatically serving support files with the | ||
// 'supportFile' configuration option. | ||
// | ||
// You can read more here: | ||
// https://on.cypress.io/configuration | ||
// *********************************************************** | ||
|
||
// Import commands.js using ES2015 syntax: | ||
import './commands' | ||
|
||
// Alternatively you can use CommonJS syntax: | ||
// require('./commands') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const calculateOperator = (total, operator, num) => { | ||
let result = 0; | ||
if (operator === '+') | ||
result = total + num; | ||
if (operator === '/') | ||
result = total / num; | ||
if (operator === '%') | ||
result = total % num; | ||
Comment on lines
+7
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 프로그램에서 % 연산이 쓰이는 것 같진 않네요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗앗 맞아요 ㅎㅎ 예전에 쓰던 함수를 그대로 긁어와서 레거시가 생겼네요..! |
||
if (operator === '-') | ||
result = total - num; | ||
if (operator === 'X') | ||
result = total * num; | ||
return result; | ||
}; | ||
const calculateResult = (total) => { | ||
const result = total.match(new RegExp('(\\-?[\\d]{1,3})(X|\\-|\\+|\\/|\\=)(\\-?[\\d]{1,3})')); | ||
if (result) { | ||
return calculateOperator(Number(result[1]), result[2], Number(result[3])); | ||
} | ||
return 0; | ||
}; | ||
export { calculateResult }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const checkValidInput = (total) => { | ||
const result = total.match(new RegExp('(\\-?[\\d]{1,3})(X|\\-|\\+|\\/|\\=)?(\\-?[\\d]{1,3})?')); | ||
if (!result) { | ||
return ''; | ||
} | ||
if (result[2] === undefined) { | ||
if (result[1] === undefined) { | ||
return ''; | ||
} | ||
return result[1]; | ||
} | ||
if (result[3] === undefined) { | ||
return result[1] + result[2]; | ||
} | ||
return result[1] + result[2] + result[3]; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getCountOperationdataSet, setCountOperationdataSet } from './operation-data-set.js'; | ||
import { checkValidInput } from './check.js'; | ||
import { calculateResult } from './calculate-util.js'; | ||
const digitClickEvent = (e) => { | ||
const eventTarget = e.target; | ||
const totalTarget = document.getElementById('total'); | ||
if (Number(totalTarget.innerText) === 0) { | ||
document.getElementById('total').innerText = eventTarget.innerText; | ||
} | ||
else { | ||
const result = checkValidInput(document.getElementById('total').innerText + eventTarget.innerText); | ||
if (result === '') { | ||
return; | ||
} | ||
document.getElementById('total').innerText = result; | ||
} | ||
}; | ||
const operatorEvent = (e) => { | ||
const eventTarget = e.target; | ||
const totalTarget = document.getElementById('total'); | ||
if (eventTarget.innerText === '=') { | ||
setCountOperationdataSet('0'); | ||
document.getElementById('total').innerText = String(Math.floor(calculateResult(document.getElementById('total').innerText))); | ||
return; | ||
} | ||
if (Number(totalTarget.innerText) === 0) { | ||
return; | ||
} | ||
else if (!['/', '-', 'X', '+'].includes(totalTarget.innerText[totalTarget.innerText.length - 1])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분도 정규표현식을 이용할 수 있지 않을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오.. 맞아요 test 문법을 사용하면 간단하게 표현가능할 수 있을것 같아요! |
||
if (getCountOperationdataSet() === '1') { | ||
return; | ||
} | ||
setCountOperationdataSet('1'); | ||
document.getElementById('total').innerText += eventTarget.innerText; | ||
} | ||
}; | ||
const ACEvent = () => { | ||
document.getElementById('total').innerText = '0'; | ||
}; | ||
export { digitClickEvent, operatorEvent, ACEvent }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
함수로 분리하니까 훨씬 깔끔하고 좋네요