Skip to content

Commit

Permalink
pasando las pruebas correspondientes a servicios. con axios, mocks y …
Browse files Browse the repository at this point in the history
…siguiendo el laboratorio...
  • Loading branch information
Softx0 committed Feb 16, 2022
1 parent 74e6288 commit ffa6a6c
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 26 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@babel/preset-env", { "targets": {"node": "current"} }]]
}
38 changes: 34 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"apisauce": "^2.1.5",
"axios": "^0.25.0",
"axios-mock-adapter": "^1.20.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0",
Expand Down Expand Up @@ -39,6 +41,7 @@
]
},
"devDependencies": {
"@babel/preset-env": "^7.16.11",
"autoprefixer": "^10.4.2",
"dotenv-cli": "^5.0.0",
"jest": "^27.5.1",
Expand Down
17 changes: 9 additions & 8 deletions src/api/DictionaryService.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { BaseApi } from './BaseApi';
import axios from 'axios';

const defineWordByNameUrl = (word) => `${process.env.REACT_APP_URL}/entries/es/${word}`;
const defineWordByNameUrl = `${process.env.REACT_APP_URL}/entries/es`;

const defineWordByName = (request, headers) =>
BaseApi.get(defineWordByNameUrl(request), null, headers).then(
(response) => response.data
);
// const defineWordByName = (request, headers) =>
// BaseApi.get(defineWordByNameUrl(request), null, headers).then(
// (response) => response.data
// );

export default {
defineWordByName
};
export const defineWordByName = (word) => {
return axios.get(`${defineWordByNameUrl}/${word}`);
};
4 changes: 2 additions & 2 deletions src/api/utils/ResponseCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const ResponseCode = {
},
CLIENT_ERROR : {
code: 800,
message: "Please check the name of the pokemon"
message: "Please check your inputs"
},
UNDEFINED: {
code: 404,
message: "Por favor trata con otro Pokemon"
message: "Por favor trata denuevo"
},
};

Expand Down
51 changes: 45 additions & 6 deletions src/pages/Home/App.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
import { useState } from 'react'
import { defineWordByName } from "../../api/DictionaryService";

const App = () => {


const [definition, setDefinition] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [errorMsg, setErrorMsg] = useState("");

const handleSubmit = async (event) => {
try {
event.preventDefault();
setIsLoading(true);
setDefinition('');
setErrorMsg('');

const { word } = event.target.elements
// console.log(word);
const response = await defineWordByName(word.value);

setDefinition(response.data[0].meanings[0].definitions[0].definition);
} catch (event) {
console.log("Sorry hay un problema!");
} finally {
setIsLoading(false);
}

};

return (
<>

<div>
<div className="App">
<h1>This is react-testing-library</h1>
<form >
<label htmlFor="word" >Word: </label>
<input id="word" name="word" type="text" />
<form onSubmit={handleSubmit}>
<label htmlFor="word" >word: </label>
<input id="word" type="text" />

<button id="btnConsultar" type="submit" >Definicion</button>
<button disabled={isLoading} type="submit" >definicion</button>
</form>

{
isLoading && <p>Loading...</p>
}
{
!isLoading && definition && <p>{definition}</p>
}
{
!isLoading && !definition && !errorMsg && <p>{`Type a word`}</p>
}
{
!isLoading && errorMsg && <p>{errorMsg}</p>
}
</div>

</>
Expand Down
32 changes: 26 additions & 6 deletions src/pages/Home/App.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { render, screen } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import App from './App';
import axios from 'axios';
jest.mock('axios');

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

beforeEach(() => {
render(<App />)
});

beforeEach(() => render(<App />));

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


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

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

it('should search a word', async () => {

axios.get.mockReturnValueOnce({
data: [{
meanings: [{
definitions: [{
definition: 'Construcción cubierta destinada a ser habitada'
}]
}]
}]
});

const inputEl = screen.getByLabelText(/word/i);
const btnEl = screen.getByRole('button', { name: /definicion/i });

fireEvent.change(inputEl, { target: { value: 'casa' } });
fireEvent.click(btnEl);

const wordMeaning = await screen.findAllByText(/Construcción cubierta destinada a ser habitada/i);
expect(wordMeaning[0]).toBeInTheDocument();


});

});

0 comments on commit ffa6a6c

Please sign in to comment.