const { expect } = require('chai');
const { spy } = require('sinon');
const middlewareFn = require('./middleware');

describe('app/resources/dirs/rename/middleware', () => {
  const trimSlashes = spy();
  const middleware = middlewareFn(trimSlashes);

  it('should add getDir to the request', () => {
    const req = { query: { dir: 'test' } };
    const next = spy();
    middleware[0](req, {}, next);
    expect(req.getDir).to.be.a('function');
    expect(next.calledOnce).to.equal(true);
  });

  it('should add getNewName to the request', () => {
    const req = { body: { newname: 'test' } };
    const next = spy();
    middleware[1](req, {}, next);
    expect(req.getNewName).to.be.a('function');
    expect(next.calledOnce).to.equal(true);
  });

  it('should respond with 400 error if dir is missing', () => {
    const req = { query: {} };
    const status = spy();
    const json = spy();
    const res = {
      status: (code) => {
        status(code);
        return { json };
      }
    };
    middleware[0](req, res);
    expect(status.getCall(0).args[0]).to.equal(400);
    expect(json.getCall(0).args[0]).to.deep.equal({
      error: '?dir query path is empty'
    });
  });

  it('should respond with 400 error if newname is missing', () => {
    const req = { body: {} };
    const status = spy();
    const json = spy();
    const res = {
      status: (code) => {
        status(code);
        return { json };
      }
    };
    middleware[1](req, res);
    expect(status.getCall(0).args[0]).to.equal(400);
    expect(json.getCall(0).args[0]).to.deep.equal({
      error: 'newname body param is empty'
    });
  });
});
