{"size": 2158, "ext": "js", "max_stars_repo_path": "api/test/infra/setUpReadModelTests.js", "max_stars_repo_name": "the-kid89/nota", "max_stars_repo_stars_event_min_datetime": "2017-01-27T02:13:16.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-23T06:41:58.000Z", "max_issues_repo_path": "api/test/infra/setUpReadModelTests.js", "max_issues_repo_name": "the-kid89/nota", "max_issues_repo_issues_event_min_datetime": "2017-02-04T21:51:38.000Z", "max_issues_repo_issues_event_max_datetime": "2017-05-16T13:50:08.000Z", "max_forks_repo_path": "api/test/infra/setUpReadModelTests.js", "max_forks_repo_name": "the-kid89/nota", "max_forks_repo_forks_event_min_datetime": "2017-02-03T02:28:56.000Z", "max_forks_repo_forks_event_max_datetime": "2019-05-13T08:19:55.000Z"}
{"max_stars_count": 19, "max_issues_count": 6, "max_forks_count": 20, "avg_line_length": 39.2363636364, "max_line_length": 114, "alphanum_fraction": 0.6858202039}
const {getTypeName} = require("../../src/infra/utils");
const ReadRepository = require("../../src/infra/ReadRepository");
const ReadRepositorySyncWrapper = require("../../src/infra/ReadRepositorySyncWrapper");
const TransactionalRepository = require("../../src/infra/TransactionalRepository");
const InMemoryMapper = require("../../src/infra/inmemory/Mapper");
const NoOpLogger = require("../../src/infra/NoOpLogger");
const async = require("../../src/infra/async");

function changeProxyFactory(obj) {
  const handler = {
    getChanges: function() {
      return obj;
    }
  };
  const proxy = new Proxy(obj, handler);
  return {proxy, handler};
}

function setUpReadModelTest({readModels, events, initialState, resultsSetter}) {
  if (!readModels) throw new Error('Missing readModels.');
  if (!events || !events.length) throw new Error('Missing event(s).');
  if (!resultsSetter) throw new Error('Missing resultsSetter.');
  const logger = new NoOpLogger();
  initialState = initialState || {};
  const eventsData = events.map(event => {
    return {
      event,
      typeId: getTypeName(event)
    }
  });
  const readModelList = Object.keys(readModels).map(name => {
    const {config, handler} = readModels[name];
    return {name, config, handler}
  });
  const readModelUnderTest = readModelList[0];

  beforeEach(done => {
    const mapper = new InMemoryMapper(readModelList, initialState, logger);
    const readRepository = new ReadRepositorySyncWrapper(new ReadRepository(mapper, logger));
    async
      .forEach(eventsData, eventData => {
        return async.forEach(readModelList, readModel => {
          const modelRepository = new TransactionalRepository(mapper, readModel.name, changeProxyFactory, logger);
          return Promise.resolve(readModel.handler(modelRepository, eventData, readRepository))
            .then(() => modelRepository.applyChanges())
        });
      })
      .then(() => readRepository.findAll(readModelUnderTest.name))
      .then(data => resultsSetter(data))
      .then(() => done())
      .catch(err => done(err))
  })
}
module.exports = setUpReadModelTest;
module.exports.default = setUpReadModelTest;