Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
1
Indexable
Never
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const chaiHttp = require('chai-http');

const { expect } = chai;
const businessServices = require('../../../src/services/sales.services');
const businessController = require('../../../src/controllers/sales');
const { SOME_SALES_MOCK, SALES_SERVICES_MOCK, UNIQUE_SALE_MOCK } = require('../mocks/sales.mock');

chai.use(chaiHttp);
chai.use(sinonChai);

describe('Controller for sales', function () {
  afterEach(function () {
    sinon.restore();
  });

  it('should return all sales', async function () {
    const req = { params: { id: 1 } };
    const res = { status: sinon.stub().returnsThis(), json: sinon.stub().returnsThis() };
    const stub = sinon.stub(businessServices, 'getAllDataBase').resolves(SOME_SALES_MOCK);
    await businessController.getControllerByProducts(req, res);
    expect(res.status).to.have.been.calledWith(200);
    stub.restore();
  });

  it('should return "Sale not found" if sale does not exist', async function () {
    const res = { status: sinon.stub().returnsThis(), json: sinon.stub().returnsThis() };
    const req = { params: { id: 1 } };
    const stub = sinon.stub(businessServices, 'getAllById').returns(SALES_SERVICES_MOCK);
    await businessController.getControllerById(req, res);

    expect(res.status).to.have.been.calledWith(200);
    stub.restore();
  });

  it('Returns a single sale when the ID is found in the table SALES', async function () {
    const res = { status: sinon.stub().returnsThis(), json: sinon.stub().returnsThis() };
    const req = { params: { id: 1 } };
    const stub = sinon.stub(businessServices, 'getAllById').returns(UNIQUE_SALE_MOCK);
    await businessController.getControllerById(req, res);
    expect(res.status).to.have.been.calledWith(200);

    stub.restore();
  });
});
Leave a Comment