{"size": 2363, "ext": "js", "max_stars_repo_path": "__tests__/appium-service/resetApp.spec.js", "max_stars_repo_name": "lewie9021/appdriver", "max_stars_repo_stars_event_min_datetime": "2019-05-10T08:25:30.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-05T14:44:15.000Z", "max_issues_repo_path": "__tests__/appium-service/resetApp.spec.js", "max_issues_repo_name": "lewie9021/appdriver", "max_issues_repo_issues_event_min_datetime": "2019-11-13T18:15:45.000Z", "max_issues_repo_issues_event_max_datetime": "2020-03-19T07:18:02.000Z", "max_forks_repo_path": "__tests__/appium-service/resetApp.spec.js", "max_forks_repo_name": "lewie9021/appdriver", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 7, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 37.5079365079, "max_line_length": 103, "alphanum_fraction": 0.7342361405}
jest.mock("../../src/worker/services/request");
jest.mock("../../src/worker/stores/sessionStore");

const requestHelpers = require("../../src/worker/services/request");
const { sessionStore } = require("../../src/worker/stores/sessionStore");
const { appiumService } = require("../../src/worker/services/appiumService");

afterEach(() => {
  jest.resetAllMocks();
  jest.restoreAllMocks();
});

it("makes POST requests to the correct Appium endpoints", async () => {
  const sessionId = "sessionId";
  jest.spyOn(sessionStore, "getSessionId").mockReturnValue(sessionId);
  jest.spyOn(sessionStore, "getCapabilities").mockReturnValue(false);
  jest.spyOn(requestHelpers, "request").mockResolvedValue();

  await expect(appiumService.resetApp())
    .resolves.toEqual(undefined);

  expect(requestHelpers.request).toHaveBeenCalledTimes(1);
  expect(requestHelpers.request).toHaveBeenCalledWith({
    method: "POST",
    path: `/session/${sessionId}/appium/app/reset`
  });
  expect(sessionStore.getCapabilities).toHaveBeenCalledWith("noReset");
});

it("optionally accepts a sessionId", async () => {
  const sessionId = "newSessionId";
  jest.spyOn(sessionStore, "getSessionId").mockReturnValue("sessionId");
  jest.spyOn(sessionStore, "getCapabilities").mockReturnValue(false);
  jest.spyOn(requestHelpers, "request").mockResolvedValue();

  await expect(appiumService.resetApp({ sessionId }))
    .resolves.toEqual(undefined);

  expect(requestHelpers.request).toHaveBeenCalledTimes(1);
  expect(requestHelpers.request).toHaveBeenCalledWith({
    method: "POST",
    path: `/session/${sessionId}/appium/app/reset`
  });
  expect(sessionStore.getCapabilities).toHaveBeenCalledWith("noReset");
});

it("throws if 'noReset' capability is enabled", async () => {
  const sessionId = "sessionId";
  jest.spyOn(sessionStore, "getSessionId").mockReturnValue(sessionId);
  jest.spyOn(sessionStore, "getCapabilities").mockReturnValue(true);
  jest.spyOn(requestHelpers, "request").mockResolvedValue();
  expect.assertions(4);

  try {
    await appiumService.resetApp();
  } catch (err) {
    expect(err).toBeInstanceOf(Error);
    expect(err).toHaveProperty("message", "Unable to reset app when capabilities.noReset is enabled.");
  }

  expect(requestHelpers.request).toHaveBeenCalledTimes(0);
  expect(sessionStore.getCapabilities).toHaveBeenCalledWith("noReset");
});