forked from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test for the calculator game. Review secureRandomNumber function of U…
…tils.js
- Loading branch information
Showing
3 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { ContextFun } from '../Context'; | ||
import axios from 'axios'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { BrowserRouter as Router } from 'react-router-dom'; | ||
import Calculator from './Calculator'; | ||
|
||
const mockAxios = new MockAdapter(axios); | ||
|
||
describe("Calculator game", () => { | ||
|
||
beforeEach(() => { | ||
mockAxios.reset(); | ||
}); | ||
|
||
test("renders Calculator",async () => { | ||
render( | ||
<ContextFun> | ||
<Router> | ||
<Calculator/> | ||
</Router> | ||
</ContextFun> | ||
); | ||
|
||
// Comprobamos que el número de elementos sea 3 | ||
let operation = document.getElementsByClassName('questionStructure')[0][0]; | ||
const separatedText = operation.split(' '); | ||
expect(separatedText.length).toBeGreaterThan(3); | ||
// Comprobamos que el número de respuestas posibles sea 4 | ||
let answers = document.getElementsByClassName('questionStructure')[1]; | ||
expect(answers).toHaveLength(4); | ||
// Tratamos de hacer la operación | ||
let number1 = separatedText[0]; | ||
let number2 = separatedText[2]; | ||
let op = separatedText[1]; | ||
let result; | ||
switch (op) { | ||
case '+': result = number1 + number2; break; | ||
case '-': result = number1 - number2; break; | ||
case 'x': result = number1 * number2; break; | ||
case '÷': result = Math.round(number1 / number2); break; | ||
} | ||
expect(screen.getByText(result)).toBeInTheDocument(); | ||
}); | ||
|
||
}); | ||
|