identify.spec.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { act, fireEvent, render } from '@testing-library/react';
  2. import React from 'react';
  3. import { Identify, Props } from './identify';
  4. describe(Identify.name, () => {
  5. const props: Props = {
  6. connecting: false,
  7. onIdentify: jest.fn(),
  8. };
  9. it('should render an input', () => {
  10. expect.assertions(1);
  11. const { getByDisplayValue } = render(<Identify {...props} />);
  12. expect(getByDisplayValue('')).toBeInTheDocument();
  13. });
  14. it('should render a connect button', () => {
  15. expect.assertions(1);
  16. const { getByText } = render(<Identify {...props} />);
  17. expect(getByText('Connect')).toBeInTheDocument();
  18. });
  19. describe('when pressing the connect button', () => {
  20. it('should call the onIdentify prop', () => {
  21. expect.assertions(2);
  22. const { getByDisplayValue, getByText } = render(<Identify {...props} />);
  23. const input = getByDisplayValue('');
  24. const button = getByText('Connect');
  25. act(() => {
  26. fireEvent.change(input, { target: { value: 'my-computer' } });
  27. });
  28. act(() => {
  29. fireEvent.click(button);
  30. });
  31. expect(props.onIdentify).toHaveBeenCalledTimes(1);
  32. expect(props.onIdentify).toHaveBeenCalledWith('my-computer');
  33. });
  34. });
  35. describe('when connecting', () => {
  36. const propsConnecting: Props = { ...props, connecting: true };
  37. it('should not render the connect button', () => {
  38. expect.assertions(1);
  39. const { queryByText } = render(<Identify {...propsConnecting} />);
  40. expect(queryByText('Connect')).not.toBeInTheDocument();
  41. });
  42. });
  43. });