2

I've built a simple eCommerce application on Adonis JS. I'm trying to test the cart functionality where if the user adds an item to the cart and if it's already there in the cart, then it should simply increase the quantity. For this, I'm hitting the cart API twice to check the functionality. However, my session isn't persisted between requests because of which each time I receive only one quantity.

test('should increase the quantity if user tries to add the same item to cart', async ({ assert, client }) => {
    const menu = await createMenu(client);
    // Simply adds to cart
    await client.post(`/cart/${menu.id}`).send({
        quantity: 1,
    }).end();

    // It should increase the quantity to 2 now.
    const response = await client.post(`/cart/${menu.id}`).send({
        quantity: 1,
    }).end();

    response.assertStatus(200);
    // There should be only one item in cart
    assert.equal(response.body.data.items.length, 1);
    // And quantity of that item should be 2.
    assert.equal(response.body.data.items[0].quantity, 2);
});
Saravanan Sampathkumar
  • 3,201
  • 1
  • 20
  • 46

1 Answers1

0

The way to do it was to use supertest

const supertest = require('supertest');

test('should increase the quantity if user tries to add the same item to cart', async ({ assert, client }) => {

    const BASE_URL = 'http://localhost:4000';
    const agent = supertest.agent(BASE_URL);

    const menu = await createMenu(client);

    // Simply adds to cart
    const result1 = await agent.post(`/cart/${menu.id}`).send({
            quantity: 1,
    }).withCredentials();

    assert.equal(result1.status, 200);

    // It should increase the quantity to 2 now.
    const result2 = await agent.post(`/cart/${menu.id}`).send({
            quantity: 1,
    }).withCredentials();

    assert.equal(result2.status, 200);
    // There should be only one item in cart
    assert.equal(result2.body.data.items.length, 1);
    // And quantity of that item should be 2.
    assert.equal(result2.body.data.items[0].quantity, 2);
});
Saravanan Sampathkumar
  • 3,201
  • 1
  • 20
  • 46