import express from 'express';
import request from 'supertest';
import createRouteMiddleware from '../../create-route-middleware';
import createFeatures from '../fixtures/createFeatures';
import { describe } from 'riteway';

describe('createRouteMiddleware()', async should => {

  const { assert } = should();
  
  {
    const app = express();
    const features = createFeatures();
    const handler = createRouteMiddleware(
      features,
      {
        requiredFeature: 'posts',
        get: (req, res) => {
          res.send();
        }
      }
    );
    const path = '/posts';
    app.use(path, handler);

    request(app)
      .get(path)
      .end(function(err, res) {
        assert({
          given: 'a feature that is enabled',
          should: 'set the correct response status code',
          actual: res.statusCode,
          expected: 200
        });
      });
  }

  {
    const app = express();
    const features = createFeatures();
    const handler = createRouteMiddleware(
      features,
      {
        requiredFeature : 'help',
        get: (req, res) => {
          res.send();
        }
      }
    );
    const path = '/posts';
    app.use(path, handler);

    request(app)
      .get(path)
      .end(function(err, res) {
        assert({
          given: 'a feature that is disabled',
          should: 'set the correct response status code',
          actual: res.statusCode,
          expected: 404
        });
      });
  }

  {
    const app = express();
    const features = createFeatures();
    const handler = createRouteMiddleware(
      features,
      {
        requiredFeature : 'help',
        get: (req, res) => {
          res.send();
        }
      }
    );
    const path = '/posts';
    app.use(path, handler);

    request(app)
      .get(`${path}?ft=posts`)
      .end(function(err, res) {
        assert({
          given: 'the path has param override that is not relevant to the given disabled feature',
          should: 'set the correct response status code',
          actual: res.statusCode,
          expected: 404
        });
      });
  }

  {
    const app = express();
    const features = createFeatures();
    const handler = createRouteMiddleware(
      features,
      {
        requiredFeature : 'help',
        get: (req, res) => {
          res.send();
        }
      }
    );
    const path = '/posts';
    app.use(path, handler);

    request(app)
      .get(`${path}?ft=help`)
      .end(function(err, res) {
        assert({
          given: 'the path has param override that enables a disabled feature',
          should: 'set the correct response status code',
          actual: res.statusCode,
          expected: 200
        });
      });
  }

  {
    const app = express();
    const features = createFeatures();
    const handler = createRouteMiddleware(
      features,
      {
        requiredFeature: 'posts',
        post: (req, res) => {
          res.send();
        }
      }
    );
    const path = '/posts';
    app.use(path, handler);

    request(app)
      .post(path)
      .end(function(err, res) {
        assert({
          given: 'a post method',
          should: 'should use the post method',
          actual: res.statusCode,
          expected: 200
        });
      });
  }

  {
    const app = express();
    const features = createFeatures();
    const handler = createRouteMiddleware(
      features,
      {
        requiredFeature: 'posts',
        put: (req, res) => {
          res.send();
        }
      }
    );
    const path = '/posts';
    app.use(path, handler);

    request(app)
      .put(path)
      .end(function(err, res) {
        assert({
          given: 'a put method',
          should: 'should use the put method',
          actual: res.statusCode,
          expected: 200
        });
      });
  }

});

@@ -2714,32 +2714,887 @@
 %0A      %7D);%0A  %7D%0A%0A
+  %7B%0A    const app = express();%0A    const features = createFeatures();%0A    const path = '/posts';%0A    const handler = createRouteMiddleware(%0A      features,%0A      %7B%0A        requiredFeature: 'posts',%0A        get: (req, res) =%3E %7B%0A          // Simulate template rendering time%0A          // This allows following express methods to be called, possibly causing errors or changing the status.%0A          setTimeout(() =%3E res.send(), 3000);%0A        %7D%0A      %7D%0A    );%0A    app.use(path, handler);%0A    app.get('*', (req, res) =%3E %7B res.status(404); res.send(); %7D);%0A%0A    request(app)%0A      .get(path)%0A      .end(function(err, res) %7B%0A        if (err) throw(err);%0A        assert(%7B%0A          given: 'given more than one handler',%0A          should: 'handler should not effect each other',%0A          actual: res.statusCode,%0A          expected: 200%0A        %7D);%0A      %7D);%0A  %7D%0A%0A
   %7B%0A    const ap

add test to very the correct application of the next function