Skip to content

Commit

Permalink
doing refactor to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Softx0 committed Feb 16, 2022
1 parent ffa6a6c commit f53ae07
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions src/pages/Home/App.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@ jest.mock('axios');

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

beforeEach(() => {
render(<App />);
axios.get.mockClear();
});

const getFakeResponse = ({ expectedDefinition }) => ({
data: [{
meanings: [{
definitions: [{
definition: expectedDefinition
}]
}]
}]
})

beforeEach(() => render(<App />));
const fillFormAndSubmit = () => {
const inputEl = screen.getByLabelText(/word/i);
const btnEl = screen.getByRole('button', { name: /definicion/i });

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

test('renders Free Disctionary title', () => {
// screen.debug(); // para debugear y ver el componente a renderizar
Expand Down Expand Up @@ -40,26 +60,28 @@ describe('App Test Suite', () => {
});

it('should search a word', async () => {
const expectedDefinition = 'Construcción cubierta destinada a ser habitada';
axios.get.mockReturnValueOnce(getFakeResponse({ expectedDefinition }));

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

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);
const wordMeaning = await screen.findAllByText(expectedDefinition);
expect(wordMeaning[0]).toBeInTheDocument();
});

it('should dissapear loading message when search is finish', async () => {
const expectedDefinition = 'Construcción cubierta destinada a ser habitada';
axios.get.mockReturnValueOnce(getFakeResponse({ expectedDefinition }));

fillFormAndSubmit();

const loadingEl = screen.getByText(/loading/i);
expect(loadingEl).toBeInTheDocument();

await screen.findAllByText(expectedDefinition);

const loadingElExpetec = screen.queryByText(/loading/i);
expect(loadingElExpetec).not.toBeInTheDocument()
});

});

0 comments on commit f53ae07

Please sign in to comment.