import * as React from "react";
import { View, TouchableOpacity } from "react-native";
import { render } from "@testing-library/react-native";

import { Touchable } from "..";

const ChildrenComponent = () => <View name="childrenComponent" />;

const props = {
  testID: "some-test-id",
  onPress: jest.fn(),
  children: <ChildrenComponent />,
};

describe("<Touchable />", () => {
  describe("when running in automated tests environment", () => {
    beforeEach(() => {
      process.env.ZAPP_UI_TESTS_ENV = "true";
    });

    afterEach(() => {
      delete process.env.ZAPP_UI_TESTS_ENV;
    });

    it("has accessible flag set to false", () => {
      const { toJSON, UNSAFE_getByType } = render(<Touchable {...props} />);

      const touchableWrapper = UNSAFE_getByType(TouchableOpacity);
      expect(toJSON()).toMatchSnapshot();
      expect(touchableWrapper.props).toHaveProperty("accessible", false);
    });
  });

  describe("when not running in automated tests environment", () => {
    beforeEach(props.onPress.mockClear);

    it("renders correctly", () => {
      const { toJSON } = render(<Touchable {...props} />);
      expect(toJSON()).toMatchSnapshot();
    });

    it("has accessible flag set to true", () => {
      const { UNSAFE_getByType } = render(<Touchable {...props} />);
      const touchableWrapper = UNSAFE_getByType(TouchableOpacity);
      expect(touchableWrapper.props).toHaveProperty("accessible", true);
    });

    it("assigns testID and accessibilityLabel props correctly", () => {
      const { UNSAFE_getByType } = render(<Touchable {...props} />);
      const touchableWrapper = UNSAFE_getByType(TouchableOpacity);
      expect(touchableWrapper.props).toHaveProperty("testID", props.testID);

      expect(touchableWrapper.props).toHaveProperty(
        "accessibilityLabel",
        props.testID
      );
    });

    it("calls the onPress event when it is pressed", () => {
      const { UNSAFE_getByType } = render(<Touchable {...props} />);
      const touchableWrapper = UNSAFE_getByType(TouchableOpacity);
      touchableWrapper.props.onPress();
      expect(props.onPress).toHaveBeenCalledTimes(1);
    });
  });
});
