{"size": 1369, "ext": "js", "max_stars_repo_path": "test/integration/context_spec.js", "max_stars_repo_name": "n1ru4l/apollo-resolvers", "max_stars_repo_stars_event_min_datetime": "2019-07-29T03:42:31.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-19T16:18:37.000Z", "max_issues_repo_path": "test/integration/context_spec.js", "max_issues_repo_name": "Jobstart/apollo-resolvers", "max_issues_repo_issues_event_min_datetime": "2019-10-01T15:09:54.000Z", "max_issues_repo_issues_event_max_datetime": "2020-03-04T20:12:27.000Z", "max_forks_repo_path": "test/integration/context_spec.js", "max_forks_repo_name": "Jobstart/apollo-resolvers", "max_forks_repo_forks_event_min_datetime": "2021-01-19T04:29:30.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-01T05:14:56.000Z"}
{"max_stars_count": 64, "max_issues_count": 4, "max_forks_count": 7, "avg_line_length": 24.8909090909, "max_line_length": 86, "alphanum_fraction": 0.504017531}
import { expect } from 'chai';
import { spy } from 'sinon';
import supertest from 'supertest';
import express from 'express';
import Promise from 'bluebird';

import { createExpressContext } from '../../dist/context';

const createServer = () => {
  const app = express();

  return new Promise((resolve, reject) => {
    const server = app.listen(8187, err => err ? reject(err) : resolve({
      app,
      close: () => server.close()
    }));
  });
};

describe('(integration) src/context', () => {
  describe('createExpressContext', () => {
    let app = null
      , close = null
      , context = null;
    beforeEach(() => createServer().then(({ app: a, close: c}) => {
      app = a;
      close = c;
    }));
    afterEach(() => close());
    it('calls dispose on the returned context when the response finishes', (next) => {
      let context = null;
      app.post('/', (req, res, next) => {
        context = createExpressContext({
          models: {},
          user: {}
        }, res);
        spy(context, 'dispose');
        return res.status(200).send({
          message: 'ok'
        });
      });
      supertest(app)
        .post('/')
        .end((err, res) => {
          try {
            expect(context.dispose.calledOnce).to.be.true;
            next();
          } catch (e) {
            next(e);
          }
        });
    });
  });
});
