{"size": 1671, "ext": "js", "max_stars_repo_path": "src/api/test/unit/api/cart.spec.js", "max_stars_repo_name": "naikvenu/oci-cloudnative", "max_stars_repo_stars_event_min_datetime": "2019-11-04T11:13:46.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-21T15:38:01.000Z", "max_issues_repo_path": "src/api/test/unit/api/cart.spec.js", "max_issues_repo_name": "naikvenu/oci-cloudnative", "max_issues_repo_issues_event_min_datetime": "2019-10-31T18:41:40.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-26T00:18:52.000Z", "max_forks_repo_path": "src/api/test/unit/api/cart.spec.js", "max_forks_repo_name": "naikvenu/oci-cloudnative", "max_forks_repo_forks_event_min_datetime": "2019-10-31T18:30:32.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-10T10:03:46.000Z"}
{"max_stars_count": 133, "max_issues_count": 127, "max_forks_count": 121, "avg_line_length": 23.5352112676, "max_line_length": 71, "alphanum_fraction": 0.5481747457}
/**
 * Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
 * The Universal Permissive License (UPL), Version 1.0
 */
const mock = require('../../helpers/mock');
const helpers = require('../../../helpers/index');

describe('Carts', () => {
  it('should return cart items', done => {
    const id = 1;
    jest.spyOn(helpers, 'getCartId').mockReturnValue(id);

    mock.service()
      .get(`/carts/${id}/items`)
      .reply(200, [{foo: 'bar'}]);
    
    mock.app()
      .get('/api/cart')
      .expect(200)
      .expect(res => expect(res.body.length).toBeGreaterThan(0))
      .end(done);
  });

  it('should delete cart', done => {
    const id = 1;
    jest.spyOn(helpers, 'getCartId').mockReturnValue(id);

    mock.service()
      .delete(`/carts/${id}`)
      .reply(204);

    mock.app()
      .delete('/api/cart')
      .expect(204)
      .end(done);

  });

  it('should add item to cart', done => {
    const cart = 1;
    const item = { id: 12345, quantity: 2 };
    jest.spyOn(helpers, 'getCartId').mockReturnValue(cart);

    mock.service()
      .get(`/catalogue/${item.id}`)
      .reply(200, {id: item.id, price: 100})
      .post(`/carts/${cart}`)
      .reply(201);

    mock.app()
      .post('/api/cart')
      .send(item)
      .expect(201)
      .end(done);
  });

  it('should delete item from cart', done => {
    const cart = 1;
    const item = { id: 12345, quantity: 2 };
    jest.spyOn(helpers, 'getCartId').mockReturnValue(cart);

    mock.service()
      .delete(`/carts/${cart}/items/${item.id}`)
      .reply(204);
    
    mock.app()
      .delete(`/api/cart/${item.id}`)
      .expect(204)
      .end(done);
  });
});