Skip to content

Instantly share code, notes, and snippets.

@dineshigdd
Created August 4, 2023 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dineshigdd/ae2ec084d8c3ad23bd3cd7decece8edf to your computer and use it in GitHub Desktop.
Save dineshigdd/ae2ec084d8c3ad23bd3cd7decece8edf to your computer and use it in GitHub Desktop.
You can add this code in the App.test.js. It is not necessary to create another test file.
test('performs division', () => {
render(<App />);
const inputElement = screen.getByRole('textbox');
// Check the value of the textbox (initially it should be an empty string)
expect(inputElement.value).toBe('');
// Simulate clicking the buttons to enter the expression "2+3" into the calculator
fireEvent.click(screen.getByText('1'));
expect( inputElement.value ).toBe("1")
fireEvent.click(screen.getByText('0'));
expect( inputElement.value ).toBe("10")
// expect(inputElement.textContent).toBe(' 2');
fireEvent.click(screen.getByText('/'));
expect(inputElement.value).toBe('10/');
fireEvent.click(screen.getByText('2'));
expect(inputElement.value).toBe('10/2');
// Simulate clicking the "=" button
fireEvent.click(screen.getByText('='));
//select the 'result' div and check if it's value is 5
const resultElement = screen.getByTestId('result');
expect(resultElement.textContent).toBe('5');//or expect(resultElement).toHaveTextContent('5')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment