Expresso MenuItem POST route testing

Hello, finishing up the Expresso capstone (LINK). For the most part it’s been good, but when i test the MenuItem POST route it doesn’t seem to hit the endpoint. I put some logging in at the beginning of the routes for extra feedback during testing. i receive a message during the GET test, but nothing for POST. Code, testing output and test code below:

POST

The server is listening on port: 8081
  POST /api/menus/:menuId/menu-items
POST /api/menus/2/menu-items 404 3.540 ms - 9
    √ should create a valid menuItem (48ms)
    1) should create a valid menuItem
POST /api/menus/2/menu-items 404 2.165 ms - 9
    2) should return a 201 status code after menu item creation


  1 passing (351ms)
  2 failing

  1) POST /api/menus/:menuId/menu-items
       should create a valid menuItem:
     Uncaught AssertionError: expected undefined to exist
      at Statement.<anonymous> (test\test.js:1032:32)

  2) POST /api/menus/:menuId/menu-items
       should return a 201 status code after menu item creation:
     Error: expected 201 "Created", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1659:8)
      at processTicksAndRejections (internal/process/task_queues.js:79:21)
menuitemRouter.post('/', (req, res, next) => {
    console.log(req.body);
    const menuItem = req.body.menuItem;
    const sql = `INSERT INTO MenuItem(name, description, inventory, price, menu_id)
        VALUES($name, $description, $inventory, $price, $menuId)`;
    const values = {
        $name: menuItem.name,
        $description: menuItem.description,
        $inventory: menuItem.inventory,
        $price: menuItem.price,
        $menuId: req.params.menuId
    };
    const menuSql = `SELECT * FROM Menu WHERE id = ${req.params.menuId}`;

    db.get(menuSql, (error, menu) => {
        if(error) {
            next(error);
        } else {
            if(!menu) {
                return res.sendStatus(400);
            } else {
                db.run(sql, values, function(error) {
                    if(error) {
                        next(error);
                    } else {
                        const itemSql = `SELECT * FROM MenuItem WHERE id = ${this.lastID}`;
                        db.get(itemSql, (error, item) => {
                            if(error) {
                                next(error);
                            } else {
                                res.status(201).send({ menuItem: item });
                            }
                        });
                    }
                });
            }
        }
    });
});

GET TEST

The server is listening on port: 8081
  GET /api/menus/:menuId/menu-items
***is this thing on?***
GET /api/menus/1/menu-items 200 5.152 ms - 249
    √ should return all menu items of an existing menu
**is this thing on?**
GET /api/menus/3/menu-items 200 7.112 ms - 16
    √ should return an empty array for existing menus with no menu items
GET /api/menus/2/menu-items 404 1.044 ms - 9
    1) should return a status code of 200 for valid menus
GET /api/menus/999/menu-items 404 0.949 ms - 9
    √ should return a status code of 404 for invalid menus


  3 passing (213ms)
  1 failing

  1) GET /api/menus/:menuId/menu-items
       should return a status code of 200 for valid menus:
     Error: expected 200 "OK", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1659:8)
      at processTicksAndRejections (internal/process/task_queues.js:79:21)
menuitemRouter.get('/', (req, res, next) => {
    **console.log('is this thing on?');**
    const sql = `SELECT * FROM MenuItem WHERE menu_id = ${req.params.menuId}`;
   
    db.all(sql, (error, items) => {
        if(error) {
            next(error);
        } else {
            res.status(200).json({ menuItems: items});
        }
    });
});

TEST.JS

describe('POST /api/menus/:menuId/menu-items', function() {
  let newMenuItem;

  beforeEach(function(done) {
    newMenuItem = {
      name: 'New Menu Item',
      description: 'New Description',
      inventory: 20,
      price: 1.5
    };

    seed.seedMenuItemDatabase(done);
  });

  it('should create a valid menuItem', function() {
    return request(app)
        .post('/api/menus/2/menu-items')
        .send({menuItem: newMenuItem})
        .then(function() {
          testDb.all('SELECT * FROM menuItem', function(error, result) {
            const menuItem = result.find(menuItem => menuItem.name === newMenuItem.name);
            expect(menuItem).to.exist;
            expect(menuItem.id).to.exist;
            expect(menuItem.name).to.equal(newMenuItem.name);
            expect(menuItem.description).to.equal(newMenuItem.description);
            expect(menuItem.inventory).to.equal(newMenuItem.inventory);
            expect(menuItem.price).to.equal(newMenuItem.price);
            expect(menuItem.menu_id).to.equal(2);
          });
        });
  });

  it('should return a 201 status code after menu item creation', function() {
    return request(app)
        .post('/api/menus/2/menu-items')
        .send({menuItem: newMenuItem})
        .expect(201);
  });

  it('should return the newly-created menu item after menu item creation', function() {
    return request(app)
        .post('/api/menus/2/menu-items')
        .send({menuItem: newMenuItem})
        .then(function(response) {
          const menuItem = response.body.menuItem;
          expect(menuItem).to.exist;
          expect(menuItem.id).to.exist;
          expect(menuItem.name).to.equal(newMenuItem.name);
          expect(menuItem.description).to.equal(newMenuItem.description);
          expect(menuItem.inventory).to.equal(newMenuItem.inventory);
          expect(menuItem.price).to.equal(newMenuItem.price);
          expect(menuItem.menu_id).to.equal(2);
        });
  });

  it('should return a 400 status code for invalid menu items', function() {
    newMenuItem = {
      description: 'New Description',
      inventory: 20,
      price: 1.5
    };

    return request(app)
        .post('/api/menus/2/menu-items')
        .send({menuItem: newMenuItem})
        .expect(400);
  });
});

In the GET test “is this thing on?” shows up fine, but there’s no request body for POST. I’ve compared it to the other files in this and other projects and, all else aside, i should be able to se the request body, no? It’s something obvious, right? Let met know. I’ll live it down. Thanks in advance.

I built out the remainder of the API and, somehow, completing the other CRUD operations cleared up the issues with the POST and GET (also inexplicable) tests listed above. Moral of the story, code through it?

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.