"use strict";

var _importJobPoller = require("../importJobPoller");
var _types = require("@wix/bex-utils/@wix/ambassador-hub-v1-job/types");
const noWait = () => Promise.resolve();
describe('isTerminalJobStatus', () => {
  it('is true for completed / partially successful / failed', () => {
    expect((0, _importJobPoller.isTerminalJobStatus)(_types.JobStatus.COMPLETED)).toBe(true);
    expect((0, _importJobPoller.isTerminalJobStatus)(_types.JobStatus.PARTIALLY_SUCCESSFUL)).toBe(true);
    expect((0, _importJobPoller.isTerminalJobStatus)(_types.JobStatus.FAILED)).toBe(true);
  });
  it('is false for in-progress / initializing / undefined', () => {
    expect((0, _importJobPoller.isTerminalJobStatus)(_types.JobStatus.IN_PROGRESS)).toBe(false);
    expect((0, _importJobPoller.isTerminalJobStatus)(_types.JobStatus.INITIALIZING)).toBe(false);
    expect((0, _importJobPoller.isTerminalJobStatus)(undefined)).toBe(false);
  });
});
describe('pollImportJob', () => {
  it('polls until terminal and returns the terminal job', async () => {
    const jobs = [{
      id: 'j',
      status: _types.JobStatus.INITIALIZING
    }, {
      id: 'j',
      status: _types.JobStatus.IN_PROGRESS
    }, {
      id: 'j',
      status: _types.JobStatus.COMPLETED
    }];
    const getJob = jest.fn(async () => jobs.shift());
    const result = await (0, _importJobPoller.pollImportJob)(getJob, 'j', {
      wait: noWait
    });
    expect(getJob).toHaveBeenCalledTimes(3);
    expect(result == null ? void 0 : result.status).toBe(_types.JobStatus.COMPLETED);
  });
  it('reports progress for every fetched job', async () => {
    const jobs = [{
      id: 'j',
      status: _types.JobStatus.IN_PROGRESS,
      progress: {
        current: 1
      }
    }, {
      id: 'j',
      status: _types.JobStatus.COMPLETED,
      progress: {
        current: 2
      }
    }];
    const getJob = jest.fn(async () => jobs.shift());
    const onProgress = jest.fn();
    await (0, _importJobPoller.pollImportJob)(getJob, 'j', {
      wait: noWait,
      onProgress
    });
    expect(onProgress).toHaveBeenCalledTimes(2);
    expect(onProgress.mock.calls[1][0].progress.current).toBe(2);
  });
  it('stops and returns null when cancelled before the first poll', async () => {
    const getJob = jest.fn();
    const result = await (0, _importJobPoller.pollImportJob)(getJob, 'j', {
      wait: noWait,
      isCancelled: () => true
    });
    expect(result).toBeNull();
    expect(getJob).not.toHaveBeenCalled();
  });
  it('stops between polls when cancelled mid-run', async () => {
    let cancelled = false;
    const getJob = jest.fn(async () => {
      cancelled = true; // cancel after the first fetch
      return {
        id: 'j',
        status: _types.JobStatus.IN_PROGRESS
      };
    });
    const result = await (0, _importJobPoller.pollImportJob)(getJob, 'j', {
      wait: noWait,
      isCancelled: () => cancelled
    });
    expect(result).toBeNull();
    expect(getJob).toHaveBeenCalledTimes(1);
  });
});
//# sourceMappingURL=importJobPoller.spec.js.map