{"size": 3199, "ext": "js", "max_stars_repo_path": "test/unit/routes/pages/post/gatherTests.js", "max_stars_repo_name": "opensourcegeek/govuk-casa", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/unit/routes/pages/post/gatherTests.js", "max_issues_repo_name": "opensourcegeek/govuk-casa", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/unit/routes/pages/post/gatherTests.js", "max_forks_repo_name": "opensourcegeek/govuk-casa", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.1101694915, "max_line_length": 119, "alphanum_fraction": 0.5695529853}
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const proxyquire = require('proxyquire');
const JourneyData = require('../../../../../lib/JourneyData.js');
const logger = require('../../../../../lib/Logger.js')('test');

const { expect } = chai;
chai.use(sinonChai);

const doGather = require('../../../../../app/routes/pages/post/gather.js');

describe('Routes: post/gather.js', () => {
  it('should return a Promise', () => {
    const result = doGather();
    expect(result).to.be.an.instanceOf(Promise);
    result.catch(() => (true));
  });

  it('should execute the "pregather" hook', () => {
    const stubExecuteHook = sinon.stub().returns(Promise.resolve());

    const proxiedDoGather = proxyquire('../../../../../app/routes/pages/post/gather.js', {
      '../utils.js': {
        executeHook: stubExecuteHook,
      },
    });

    const argLogger = {};
    const argReq = {};
    const argRes = {};
    const argMeta = {};
    proxiedDoGather(argLogger, argReq, argRes, argMeta).catch(() => (true));

    return expect(stubExecuteHook).to.have.been.calledOnceWithExactly(argLogger, argReq, argRes, argMeta, 'pregather');
  });

  it('should merge modified data back to req.body', async () => {
    const postBodyData = {
      x: 1,
      y: {
        z: 2,
      },
    };
    const sessionData = {
      x: 1,
    };
    const modifiedData = {
      x: 2,
    };

    const proxiedDoGather = proxyquire('../../../../../app/routes/pages/post/gather.js', {
      '../utils.js': {
        executeHook: sinon.stub().returns(Promise.resolve()),
        extractSessionableData: sinon.stub().returns(sessionData),
      },
      './gather-modifiers.js': {
        doGatherDataModification: sinon.stub().returns(modifiedData),
      },
    });

    const mockReq = {
      body: postBodyData,
    };
    await proxiedDoGather(logger, mockReq, {}, {}).catch(() => (true));

    expect(mockReq.body).to.deep.equal({
      x: 2,
      y: {
        z: 2,
      },
    });
  });

  it('should store only sessionable data in the session', async () => {
    const postBodyData = {
      x: 1,
      y: {
        z: 2,
      },
    };
    const sessionData = {
      x: 1,
    };
    const modifiedData = {
      x: 2,
    };

    const proxiedDoGather = proxyquire('../../../../../app/routes/pages/post/gather.js', {
      '../utils.js': {
        executeHook: sinon.stub().returns(Promise.resolve()),
        extractSessionableData: sinon.stub().returns(sessionData),
      },
      './gather-modifiers.js': {
        doGatherDataModification: sinon.stub().returns(modifiedData),
      },
    });

    const testWaypointId = 'testwaypoint';
    const mockReq = {
      body: postBodyData,
      journeyWaypointId: testWaypointId,
      journeyData: new JourneyData({
        [testWaypointId]: {
          x: 'original',
        },
      }),
      session: {},
    };
    await proxiedDoGather(logger, mockReq, {}, {}).catch(() => (true));

    expect(mockReq.journeyData.getDataForPage(testWaypointId)).to.deep.equal(modifiedData);
    expect(mockReq.session.journeyData).to.deep.equal({
      [testWaypointId]: modifiedData,
    });
  });
});
