Last active
April 11, 2022 04:56
-
-
Save Bruno125/b852de8d0e585e406a4e1d8c8444e806 to your computer and use it in GitHub Desktop.
Test cases para el ejercicio de Calculadora del curso de RN de Fernando Herrera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from 'react'; | |
import { CalculadoraScreen } from "../CalculadoraScreen"; | |
import {render, fireEvent} from '@testing-library/react-native' | |
describe('<CalculadoraScreen />', () => { | |
describe('results', () => { | |
it('inits main result with zero', () => { | |
const {assertResult} = renderCalculadora() | |
assertResult('0') | |
}) | |
it('hides previous result by default', () => { | |
const {assertPreviousDoesNotExist} = renderCalculadora() | |
assertPreviousDoesNotExist() | |
}) | |
it('updates both results when + is pressed', () => { | |
const {press, assertResult, assertPreviousResult} = renderCalculadora() | |
press('1') | |
press('+') | |
assertPreviousResult('1') | |
assertResult('0') | |
}) | |
it('updates both results when - is pressed', () => { | |
const {press, assertResult, assertPreviousResult} = renderCalculadora() | |
press('1') | |
press('-') | |
assertPreviousResult('1') | |
assertResult('0') | |
}) | |
it('updates both results when X is pressed', () => { | |
const {press, assertResult, assertPreviousResult} = renderCalculadora() | |
press('1') | |
press('X') | |
assertPreviousResult('1') | |
assertResult('0') | |
}) | |
it('updates both results when / is pressed', () => { | |
const {press, assertResult, assertPreviousResult} = renderCalculadora() | |
press('1') | |
press('/') | |
assertPreviousResult('1') | |
assertResult('0') | |
}) | |
}) | |
describe('number interaction', () => { | |
it('concatenates multiple numbers', () => { | |
const {press, assertResult} = renderCalculadora(); | |
press('1') | |
press('2') | |
press('3') | |
assertResult('123') | |
}) | |
describe('starts with zero', () => { | |
it('replaces zero with a number', () => { | |
const {press, assertResult} = renderCalculadora(); | |
press('1') | |
assertResult('1') | |
}) | |
it('ignores multiple zeros if there is no dot', () => { | |
const {press, assertResult} = renderCalculadora(); | |
press('0') | |
press('0') | |
press('0') | |
press('0') | |
assertResult('0') | |
}) | |
it('only allows a single dot', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('.') | |
press('.') | |
press('.') | |
assertResult('0.') | |
}) | |
it('adds multiple zeros if there is a dot', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('.') | |
press('0') | |
press('0') | |
press('0') | |
press('0') | |
press('0') | |
assertResult('0.00000') | |
}) | |
it('supports decimals', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('.') | |
press('1') | |
assertResult('0.1') | |
}) | |
it('avoids 0000.0, should be 0.0', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('0') | |
press('0') | |
press('0') | |
press('0') | |
press('.') | |
press('0') | |
assertResult('0.0') | |
}) | |
it('keeps negative sign for first digit', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('+/-') | |
press('1') | |
assertResult('-1') | |
}) | |
}) | |
}); | |
describe('positive negative switch', () => { | |
it('toogles between positive and negative', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('+/-') | |
assertResult('-0') | |
press('+/-') | |
assertResult('0') | |
}) | |
}) | |
describe('clear', () => { | |
it('resets to zero', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('C') | |
assertResult('0') | |
}) | |
it('resets to zero regardless of number of digits', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('1') | |
press('1') | |
press('1') | |
press('C') | |
assertResult('0') | |
}) | |
it('hides previous number when clear is triggered', () => { | |
const {press, assertPreviousDoesNotExist} = renderCalculadora() | |
press('1') | |
press('+') | |
press('C') | |
assertPreviousDoesNotExist() // because it's back to zero | |
}) | |
}) | |
describe('plus', () => { | |
it('sums single digit numbers', () =>{ | |
const {press, assertResult} = renderCalculadora() | |
press('1') | |
press('+') | |
press('2') | |
press('=') | |
assertResult('3') | |
}) | |
it('sums positive and negative numbers', () =>{ | |
const {press, assertResult} = renderCalculadora() | |
press('+/-') | |
press('1') | |
press('+') | |
press('1') | |
press('=') | |
assertResult('0') | |
}) | |
}) | |
describe('minus', () => { | |
it('substracts single digit numbers', () =>{ | |
const {press, assertResult} = renderCalculadora() | |
press('5') | |
press('-') | |
press('3') | |
press('=') | |
assertResult('2') | |
}) | |
it('substracts below zero', () =>{ | |
const {press, assertResult} = renderCalculadora() | |
press('1') | |
press('-') | |
press('5') | |
press('=') | |
assertResult('-4') | |
}) | |
it('substracts negative and positive numbers', () =>{ | |
const {press, assertResult} = renderCalculadora() | |
press('+/-') | |
press('1') | |
press('-') | |
press('5') | |
press('=') | |
assertResult('-6') | |
}) | |
}) | |
describe('multiply', () => { | |
it('multiples two positive numbers', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('2') | |
press('X') | |
press('3') | |
press('=') | |
assertResult('6') | |
}) | |
it('multiples two negative numbers', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('+/-') | |
press('2') | |
press('X') | |
press('3') | |
press('+/-') | |
press('=') | |
assertResult('6') | |
}) | |
it('multiples a positive and a negative number', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('+/-') | |
press('2') | |
press('X') | |
press('3') | |
press('=') | |
assertResult('-6') | |
}) | |
}) | |
describe('divide', () => { | |
it('divides divisible numbers', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('6') | |
press('/') | |
press('2') | |
press('=') | |
assertResult('3') | |
}) | |
it('divides non divisible numbers', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('5') | |
press('/') | |
press('2') | |
press('=') | |
assertResult('2.5') | |
}) | |
it('divides a positive and a negative number', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('6') | |
press('/') | |
press('+/-') | |
press('2') | |
press('=') | |
assertResult('-3') | |
}) | |
it('divides with periodic decimals', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('1') | |
press('/') | |
press('3') | |
press('=') | |
assertResult('0.3333333333333333') | |
}) | |
}) | |
describe('delete', () => { | |
it('does nothing when the result is already zero', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('del') | |
press('del') | |
press('del') | |
assertResult('0') | |
}) | |
it('deletes last digit of positive number', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('1') | |
press('2') | |
press('3') | |
press('del') | |
assertResult('12') | |
}) | |
it('resets to zero when the result is negative single digit', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('+/-') | |
press('1') | |
press('del') | |
assertResult('0') | |
}) | |
it('resets to zero when the result is zero + dot', () => { | |
const {press, assertResult} = renderCalculadora() | |
press('.') | |
press('del') | |
assertResult('0') | |
}) | |
}); | |
}); | |
type AllowedDigits = '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '0' | '.' | |
type AllowedOperations = '+' | '-' | 'X' | '/' | '=' | 'del' | 'C' | '+/-' | |
interface TestActions { | |
press: (value: AllowedDigits | AllowedOperations) => void | |
assertResult: (expected: string) => void | |
assertPreviousResult: (expected: string) => void | |
assertPreviousDoesNotExist: () => void | |
} | |
const renderCalculadora = () : TestActions => { | |
const wrapper = render(<CalculadoraScreen/>) | |
return { | |
press: (number: AllowedDigits | AllowedOperations) => { | |
fireEvent.press(wrapper.getByTestId(number)) | |
}, | |
assertResult: (expected: string) => { | |
expect(wrapper.getByTestId('resultado').props.children).toBe(expected) | |
}, | |
assertPreviousResult: (expected: string) => { | |
expect(wrapper.getByTestId('resultado-anterior').props.children).toBe(expected) | |
}, | |
assertPreviousDoesNotExist: () => { | |
expect(wrapper.queryByTestId('resultado-anterior')).toBeNull() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment