Untitled

 avatar
unknown
plain_text
a year ago
2.0 kB
4
Indexable
const chai = require('chai');
const sinon = require('sinon');
const productsServices = require('../../../src/services/products.services');
const productsModels = require('../../../src/models/products.models');

const { expect } = chai;

const PRODUCTS_MOCK = [      
  { id: 1, name: 'Martelo de Thor' },
  { id: 2, name: 'Traje de encolhimento' },
  { id: 3, name: 'Escudo do Capitão América' },
];

describe('Tests /services/products.services', function () {
  afterEach(function () {
    sinon.restore();
  });

  describe('Tests getProducts from /services/products.services', function () {
    it('Returns all the products from the table PRODUCTS as an array', async function () {
      const stub = sinon.stub(productsModels, 'getAllProducts').returns(PRODUCTS_MOCK);
      const products = (await productsServices.getProducts()).data;
      expect(products).to.be.an('array');
      expect(products).to.deep.equal(PRODUCTS_MOCK);
      stub.restore();
    });
  });

  describe('Tests getProductId from /services/products.services', function () {
    it('Returns a single product when the ID is found in the table PRODUCTS', async function () {
      const SINGLE_PRODUCT_MOCK = { 
        id: 1,
        name: 'Martelo de Thor',
      };
      const stub = sinon.stub(productsModels, 'getProductById').returns(SINGLE_PRODUCT_MOCK);
      const { data } = await productsServices.getProductId(1);
      // const product = (await productsServices.getProductId(1)).data;
      expect(data).to.deep.equal(SINGLE_PRODUCT_MOCK);
      stub.restore();
    });

    it('Returns "Product not found" if an product does not exist in the table PRODUCTS', async function () {
      const MOCK_NOTFOUND = { 
        message: 'Product not found', 
      };

      const stub = sinon.stub(productsModels, 'getProductById').returns(null);
      const { data } = await productsServices.getProductId(999);

      expect(data).to.deep.equal(MOCK_NOTFOUND);

      stub.restore();
    });
  });
});
Editor is loading...
Leave a Comment