import * as React from "react";
import { View, TouchableOpacity } from "react-native";
import { create, act } from "react-test-renderer";

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", () => {
      let wrapper;

      act(() => {
        wrapper = create(<Touchable {...props} />);
      });

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

  describe("when not running in automated tests environment", () => {
    let wrapper;

    act(() => {
      wrapper = create(<Touchable {...props} />);
    });

    const touchableWrapper = wrapper.root.findByType(TouchableOpacity);

    beforeEach(props.onPress.mockClear);

    it("renders correctly", () => {
      expect(wrapper.toJSON()).toMatchSnapshot();
    });

    it("has accessible flag set to true", () => {
      expect(touchableWrapper.props).toHaveProperty("accessible", true);
    });

    it("assigns testID and accessibilityLabel props correctly", () => {
      expect(touchableWrapper.props).toHaveProperty("testID", props.testID);

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

    it("calls the onPress event when it is pressed", () => {
      touchableWrapper.props.onPress();
      expect(props.onPress).toHaveBeenCalledTimes(1);
    });
  });
});
