{"size": 2286, "ext": "js", "max_stars_repo_path": "web-client/src/presenter/actions/FileDocument/uploadOrderFileAction.test.js", "max_stars_repo_name": "ericsorenson/ef-cms", "max_stars_repo_stars_event_min_datetime": "2018-10-11T15:41:58.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-12T20:09:18.000Z", "max_issues_repo_path": "web-client/src/presenter/actions/FileDocument/uploadOrderFileAction.test.js", "max_issues_repo_name": "ericsorenson/ef-cms", "max_issues_repo_issues_event_min_datetime": "2018-10-10T18:39:26.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T16:30:12.000Z", "max_forks_repo_path": "web-client/src/presenter/actions/FileDocument/uploadOrderFileAction.test.js", "max_forks_repo_name": "ericsorenson/ef-cms", "max_forks_repo_forks_event_min_datetime": "2019-05-23T16:25:28.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-18T02:29:43.000Z"}
{"max_stars_count": 23, "max_issues_count": 2108, "max_forks_count": 13, "avg_line_length": 28.2222222222, "max_line_length": 137, "alphanum_fraction": 0.6557305337}
import { applicationContextForClient as applicationContext } from '../../../../../shared/src/business/test/createTestApplicationContext';
import { presenter } from '../../presenter-mock';
import { runAction } from 'cerebral/test';
import { uploadOrderFileAction } from './uploadOrderFileAction';

describe('uploadOrderFileAction', () => {
  const errorStub = jest.fn();
  const successStub = jest.fn();
  const fakeFile = { data: 'something' };
  const mockPrimaryDocumentFileId = '2729c8a9-45a5-47ff-9210-01bf0fc3a133';

  presenter.providers.applicationContext = applicationContext;

  presenter.providers.path = {
    error: errorStub,
    success: successStub,
  };

  it('should call path.error when an error occurs while uploading the order document', async () => {
    await applicationContext
      .getUseCases()
      .uploadOrderDocumentInteractor.mockRejectedValue(new Error());

    await runAction(uploadOrderFileAction, {
      modules: {
        presenter,
      },
      state: {
        form: {
          primaryDocumentFile: fakeFile,
        },
      },
    });

    expect(errorStub).toHaveBeenCalled();
  });

  it('should call uploadOrderDocumentInteractor', async () => {
    await runAction(uploadOrderFileAction, {
      modules: {
        presenter,
      },
      state: {
        form: {
          primaryDocumentFile: fakeFile,
        },
      },
    });

    expect(
      applicationContext.getUseCases().uploadOrderDocumentInteractor,
    ).toHaveBeenCalled();
    expect(
      applicationContext.getUseCases().uploadOrderDocumentInteractor.mock
        .calls[0][1],
    ).toMatchObject({ documentFile: fakeFile });
  });

  it('should call path.success with the returned primaryDocumentFileId after successfully uploading the order document', async () => {
    applicationContext
      .getUseCases()
      .uploadOrderDocumentInteractor.mockReturnValue(mockPrimaryDocumentFileId);

    await runAction(uploadOrderFileAction, {
      modules: {
        presenter,
      },
      state: {
        form: {
          primaryDocumentFile: fakeFile,
        },
      },
    });

    expect(successStub).toHaveBeenCalled();
    expect(successStub.mock.calls[0][0]).toMatchObject({
      primaryDocumentFileId: mockPrimaryDocumentFileId,
    });
  });
});
