Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
15 kB
2
Indexable
Never
import { defineFeature, loadFeature } from "jest-cucumber";
import { shallow, ShallowWrapper } from "enzyme";

import * as helpers from "../../../../framework/src/Helpers";
import { runEngine } from "../../../../framework/src/RunEngine";
import { Message } from "../../../../framework/src/Message";

import MessageEnum, {
  getName,
} from "../../../../framework/src/Messages/MessageEnum";
import React from "react";
import LocationAndDistance from "../../src/LocationAndDistance";
import * as StorageUtils from "../../../../framework/src/Utilities";
import * as storageData from "../../../../framework/src/Utilities";

const navigation = require("react-navigation");

const screenProps = {
  navigation: {
    replace: jest.fn(),
    navigate: jest.fn(),
    addListener: (a: string, b: Function) => {
      b();
    },
  },
  id: "LocationAndDistance",
};

const mockgetStorageData = jest.spyOn(StorageUtils, "getStorageData");
mockgetStorageData.mockResolvedValue(Promise.resolve(true));

const feature = loadFeature(
  "./__tests__/features/locationAndDistance-scenario.feature"
);

defineFeature(feature, (test) => {
  beforeEach(() => {
    jest.resetModules();
    jest.doMock("react-native", () => ({ Platform: { OS: "web" } }));
    jest.spyOn(helpers, "getOS").mockImplementation(() => "web");
  });

  test("User navigates to LocationAndDistance", ({ given, when, then }) => {
    let exampleBlockA: ShallowWrapper;
    let instance: LocationAndDistance;
    let saveNewAddressFn: any;
    let saveMilesData: any;

    given("I am a User loading LocationAndDistance", () => {
      exampleBlockA = shallow(<LocationAndDistance {...screenProps} />);
    });

    when("I navigate to the LocationAndDistance", () => {
      instance = exampleBlockA.instance() as LocationAndDistance;
      instance.componentDidMount();
      saveNewAddressFn = jest.spyOn(instance, "saveNewAddress");
      saveMilesData = jest.spyOn(instance, "saveMilesData");
    });

    then("Locations details will load with out errors", async () => {
      mockgetStorageData.mockResolvedValue(Promise.resolve(false));

      let hidekeyboard = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "hideKeyboard"
      );
      hidekeyboard.simulate("press");
      //navigation message
      const navigationMessage = new Message(
        getName(MessageEnum.NavigationPayLoadMessage)
      );
      navigationMessage.addData(
        getName(MessageEnum.SessionResponseData),
        navigationMessage.messageId
      );
      runEngine.sendMessage("Unit Test", navigationMessage);
      let Token = await StorageUtils.getStorageData("Token");

      const data = {
        catalogue: {
          data: {
            id: "428",
            type: "catalogue",
            attributes: {
              addtional_miles_charge: 0,
              miles_per_day: 0,
              id: 428,
              name: "Car",
              account_id: 726,
              category_id: 8,
              sub_category_id: 74,
              earnings: null,
              taxes: null,
              total_nightly_price: null,
              status: null,
              car_number_plate: null,
              car_year: null,
              category: "Car",
              sub_category: "Tata",
              luxgo_service: 50.0,
              insurance_fee: 35.0,
              rate: 4.4,
              wishlist_liked: false,
              trips_completed: 0,
              address: {
                id: 480,
                country: "India",
                latitude: 237782373.0,
                longitude: 32323545.0,
                address: "32323545",
                addressble_id: 428,
                addressble_type: "BxBlockCatalogue::Catalogue",
                address_type: null,
                created_at: "2024-03-06T20:27:32.938Z",
                updated_at: "2024-03-06T20:27:32.938Z",
                pincode: 73467674,
                city: "goa",
              },
              delivery_address: [
                {
                  id: 482,
                  country: "India",
                  latitude: 28.5705399,
                  longitude: 77.3228931,
                  address: "C-1, Sector 18, Noida, Uttar Pradesh",
                  addressble_id: 428,
                  addressble_type: "BxBlockCatalogue::Catalogue",
                  address_type: "delivery_location",
                  created_at: "2024-03-07T17:32:07.896Z",
                  updated_at: "2024-03-07T17:32:07.896Z",
                  pincode: 201301,
                  city: "Noida",
                },
                {
                  id: 483,
                  country: "India",
                  latitude: 28.5705399,
                  longitude: 77.3228931,
                  address: "C-1, Sector 18, Noida, Uttar Pradesh",
                  addressble_id: 428,
                  addressble_type: "BxBlockCatalogue::Catalogue",
                  address_type: "delivery_location",
                  created_at: "2024-03-07T19:15:02.397Z",
                  updated_at: "2024-03-07T19:15:02.397Z",
                  pincode: 201301,
                  city: "Noida",
                },
              ],
            },
          },
        },
      };
      const getData = new Message(getName(MessageEnum.RestAPIResponceMessage));
      getData.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        getData.messageId
      );
      getData.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        JSON.parse(JSON.stringify(data))
      );
      instance.locationsApiCallId = getData.messageId;
      runEngine.sendMessage("Unit Test", getData);

      //error response
      const getData2 = new Message(getName(MessageEnum.RestAPIResponceMessage));
      getData2.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        getData2.messageId
      );
      getData2.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        JSON.parse(JSON.stringify({ error: "error" }))
      );
      instance.locationsApiCallId = getData2.messageId;
      runEngine.sendMessage("Unit Test", getData2);
      expect(exampleBlockA).toBeTruthy();
    });

    when("user changes the values for locations", () => {
      //dellivery location
      let locationDelivery = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "locationDelivery0"
      );
      locationDelivery.simulate("press");
      let inputLocationsInput = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "searchPlace"
      );
      inputLocationsInput.simulate("changeText", "location address");
      //api call for location input
      const data = {
        results: [
          {
            formatted_address: "address",
            geometry: {
              location: {
                lat: 22.7251204,
                lng: 71.63695419999999,
              },
            },
          },
          {
            formatted_address: "address",
            geometry: {
              location: {
                lat: 22.7251204,
                lng: 71.63695419999999,
              },
            },
          },
        ],
        status: "OK",
      };
      const addHomeDetails = new Message(
        getName(MessageEnum.RestAPIResponceMessage)
      );
      addHomeDetails.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        addHomeDetails
      );
      addHomeDetails.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        data
      );
      addHomeDetails.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        addHomeDetails.messageId
      );
      instance.getSearchedPlaceAPiCallId = addHomeDetails.messageId;
      const { receive: mockReceive } = instance;
      mockReceive("unit test", addHomeDetails);
      // rendering flast list data
      let locationFlatList = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "locationList"
      );
      const flatListRender = locationFlatList.renderProp("renderItem")({
        item: {
          formatted_address: "address",
          geometry: {
            location: {
              lat: 22.7251204,
              lng: 71.63695419999999,
            },
          },
        },
        index: 0,
      });
      let setSearchedLocation = flatListRender.findWhere(
        (node) => node.prop("testID") === "setSearchedLocation"
      );
      setSearchedLocation.simulate("press");

      //close modal
      let closeModal = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "closeMapMopdal"
      );
      closeModal.simulate("press");
      let mapModal = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "mapModal"
      );
      mapModal.props().onRequestClose();

      let priceInputContainer = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "safeAreaView"
      );
      let priceInput = priceInputContainer
        .dive()
        .findWhere((node) => node.prop("testID") === "priceInput");
      priceInput.simulate("changeText", 123);
      let saveBtn = priceInputContainer
        .dive()
        .findWhere((node) => node.prop("testID") === "saveLocationAddress");
      saveBtn.simulate("press");
      const saveAddressDetails = new Message(
        getName(MessageEnum.RestAPIResponceMessage)
      );
      saveAddressDetails.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        saveAddressDetails
      );
      saveAddressDetails.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        { data: { data: "some data" } }
      );
      saveAddressDetails.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        saveAddressDetails.messageId
      );
      instance.updateLocationCallId = saveAddressDetails.messageId;
      runEngine.sendMessage("Unit Test", saveAddressDetails);

      const errorResponse = new Message(
        getName(MessageEnum.RestAPIResponceMessage)
      );
      errorResponse.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        errorResponse
      );
      errorResponse.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        { error: "error occured" }
      );
      errorResponse.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        errorResponse.messageId
      );
      instance.updateLocationCallId = errorResponse.messageId;
      runEngine.sendMessage("Unit Test", errorResponse);

      //home location
      let openMap = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "openMap"
      );
      openMap.simulate("press");
      inputLocationsInput.simulate("changeText", "location address");
      //api call for location input
      addHomeDetails.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        data
      );
      addHomeDetails.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        addHomeDetails.messageId
      );
      instance.getSearchedPlaceAPiCallId = addHomeDetails.messageId;
      mockReceive("unit test", addHomeDetails);
      // rendering flast list data
      setSearchedLocation.simulate("press");

      //new Location adding
      let search = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "searchInput"
      );
      search.simulate("changeText", "address");
      search.props().onTouchStart();
      saveBtn.simulate("press");

      search.simulate("changeText", "address");
      search.props().onTouchStart();
      let discardBtn1 = priceInputContainer
        .dive()
        .findWhere((node) => node.prop("testID") === "closeEditableInput");
      discardBtn1.simulate("press");
      closeModal.simulate("press");
    });

    then("User can open the modal and update the details", () => {
      expect(saveNewAddressFn).toHaveBeenCalled();
    });

    when("User can changes the value for miles data", async () => {
      let editableMode = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "editableMode"
      );
      editableMode.simulate("press");

      let unlimitedMiles = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "unlimitedMiles"
      );
      unlimitedMiles.simulate("changeText", 1234);
      let additionalInput = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "additionalInput"
      );
      additionalInput.simulate("changeText", 1234);
      let milesPerDay = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "milesPerDay"
      );
      milesPerDay.simulate("changeText", 1234);
      let saveMilesData = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "saveMilesData"
      );
      saveMilesData.simulate("press");
      // saveMilesData.props().callApi();
      await storageData.getStorageData("usertoken");

      const saveAddressDetails = new Message(
        getName(MessageEnum.RestAPIResponceMessage)
      );
      saveAddressDetails.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        saveAddressDetails
      );
      saveAddressDetails.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        { data: { data: "some data" } }
      );
      saveAddressDetails.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        saveAddressDetails.messageId
      );
      instance.updateMilesdata = saveAddressDetails.messageId;
      runEngine.sendMessage("Unit Test", saveAddressDetails);

      saveAddressDetails.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        { message: "catalogue has been updated successfullly" }
      );
      saveAddressDetails.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        saveAddressDetails.messageId
      );
      instance.updateMilesdata = saveAddressDetails.messageId;
      runEngine.sendMessage("Unit Test", saveAddressDetails);

      const errorResponse = new Message(
        getName(MessageEnum.RestAPIResponceMessage)
      );
      errorResponse.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        errorResponse
      );
      errorResponse.addData(
        getName(MessageEnum.RestAPIResponceSuccessMessage),
        { errors: [{ token: "Invalid token" }] }
      );
      errorResponse.addData(
        getName(MessageEnum.RestAPIResponceDataMessage),
        errorResponse.messageId
      );
      instance.updateMilesdata = errorResponse.messageId;
      runEngine.sendMessage("Unit Test", errorResponse);

      editableMode.simulate("press");
      let discardMilesData = exampleBlockA.findWhere(
        (node) => node.prop("testID") === "discardMilesData"
      );
      discardMilesData.simulate("press");
    });

    then("User can update the data", () => {
      expect(saveMilesData).toHaveBeenCalled();
    });

    then("I can leave the screen with out errors", () => {
      instance.componentWillUnmount();
    });
  });
});
Leave a Comment