Skip to content

Commit ffa6a6c

Browse files
committed
pasando las pruebas correspondientes a servicios. con axios, mocks y siguiendo el laboratorio...
1 parent 74e6288 commit ffa6a6c

File tree

7 files changed

+122
-26
lines changed

7 files changed

+122
-26
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [["@babel/preset-env", { "targets": {"node": "current"} }]]
3+
}

package-lock.json

Lines changed: 34 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"@testing-library/react": "^12.1.2",
88
"@testing-library/user-event": "^13.5.0",
99
"apisauce": "^2.1.5",
10+
"axios": "^0.25.0",
11+
"axios-mock-adapter": "^1.20.0",
1012
"react": "^17.0.2",
1113
"react-dom": "^17.0.2",
1214
"react-scripts": "5.0.0",
@@ -39,6 +41,7 @@
3941
]
4042
},
4143
"devDependencies": {
44+
"@babel/preset-env": "^7.16.11",
4245
"autoprefixer": "^10.4.2",
4346
"dotenv-cli": "^5.0.0",
4447
"jest": "^27.5.1",

src/api/DictionaryService.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { BaseApi } from './BaseApi';
2+
import axios from 'axios';
23

3-
const defineWordByNameUrl = (word) => `${process.env.REACT_APP_URL}/entries/es/${word}`;
4+
const defineWordByNameUrl = `${process.env.REACT_APP_URL}/entries/es`;
45

5-
const defineWordByName = (request, headers) =>
6-
BaseApi.get(defineWordByNameUrl(request), null, headers).then(
7-
(response) => response.data
8-
);
6+
// const defineWordByName = (request, headers) =>
7+
// BaseApi.get(defineWordByNameUrl(request), null, headers).then(
8+
// (response) => response.data
9+
// );
910

10-
export default {
11-
defineWordByName
12-
};
11+
export const defineWordByName = (word) => {
12+
return axios.get(`${defineWordByNameUrl}/${word}`);
13+
};

src/api/utils/ResponseCode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const ResponseCode = {
55
},
66
CLIENT_ERROR : {
77
code: 800,
8-
message: "Please check the name of the pokemon"
8+
message: "Please check your inputs"
99
},
1010
UNDEFINED: {
1111
code: 404,
12-
message: "Por favor trata con otro Pokemon"
12+
message: "Por favor trata denuevo"
1313
},
1414
};
1515

src/pages/Home/App.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
1+
import { useState } from 'react'
2+
import { defineWordByName } from "../../api/DictionaryService";
13

24
const App = () => {
5+
6+
7+
const [definition, setDefinition] = useState("");
8+
const [isLoading, setIsLoading] = useState(false);
9+
const [errorMsg, setErrorMsg] = useState("");
10+
11+
const handleSubmit = async (event) => {
12+
try {
13+
event.preventDefault();
14+
setIsLoading(true);
15+
setDefinition('');
16+
setErrorMsg('');
17+
18+
const { word } = event.target.elements
19+
// console.log(word);
20+
const response = await defineWordByName(word.value);
21+
22+
setDefinition(response.data[0].meanings[0].definitions[0].definition);
23+
} catch (event) {
24+
console.log("Sorry hay un problema!");
25+
} finally {
26+
setIsLoading(false);
27+
}
28+
29+
};
30+
331
return (
432
<>
533

6-
<div>
34+
<div className="App">
735
<h1>This is react-testing-library</h1>
8-
<form >
9-
<label htmlFor="word" >Word: </label>
10-
<input id="word" name="word" type="text" />
36+
<form onSubmit={handleSubmit}>
37+
<label htmlFor="word" >word: </label>
38+
<input id="word" type="text" />
1139

12-
<button id="btnConsultar" type="submit" >Definicion</button>
40+
<button disabled={isLoading} type="submit" >definicion</button>
1341
</form>
14-
42+
{
43+
isLoading && <p>Loading...</p>
44+
}
45+
{
46+
!isLoading && definition && <p>{definition}</p>
47+
}
48+
{
49+
!isLoading && !definition && !errorMsg && <p>{`Type a word`}</p>
50+
}
51+
{
52+
!isLoading && errorMsg && <p>{errorMsg}</p>
53+
}
1554
</div>
1655

1756
</>

src/pages/Home/App.test.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { render, screen } from '@testing-library/react';
1+
import { fireEvent, render, screen } from '@testing-library/react';
22
import App from './App';
3+
import axios from 'axios';
4+
jest.mock('axios');
35

46
describe('App Test Suite', () => {
57

6-
beforeEach(() => {
7-
render(<App />)
8-
});
8+
9+
beforeEach(() => render(<App />));
910

1011
test('renders Free Disctionary title', () => {
1112
// screen.debug(); // para debugear y ver el componente a renderizar
@@ -28,7 +29,7 @@ describe('App Test Suite', () => {
2829

2930

3031
// Nota: dejar las pruebas los planas posibles, y single responsability
31-
test(`should render element's form`, () => {
32+
it(`should render element's form`, () => {
3233

3334
// buscando por el label correspondiente a un input
3435
const inputEl = screen.getByLabelText(/Word/i);
@@ -38,8 +39,27 @@ describe('App Test Suite', () => {
3839
expect(btnEl).toBeInTheDocument();
3940
});
4041

42+
it('should search a word', async () => {
43+
44+
axios.get.mockReturnValueOnce({
45+
data: [{
46+
meanings: [{
47+
definitions: [{
48+
definition: 'Construcción cubierta destinada a ser habitada'
49+
}]
50+
}]
51+
}]
52+
});
4153

54+
const inputEl = screen.getByLabelText(/word/i);
55+
const btnEl = screen.getByRole('button', { name: /definicion/i });
56+
57+
fireEvent.change(inputEl, { target: { value: 'casa' } });
58+
fireEvent.click(btnEl);
59+
60+
const wordMeaning = await screen.findAllByText(/Construcción cubierta destinada a ser habitada/i);
61+
expect(wordMeaning[0]).toBeInTheDocument();
4262

43-
63+
});
4464

4565
});

0 commit comments

Comments
 (0)