From 9bbfb09fb1698836001fc077d9d731b5c5b5f125 Mon Sep 17 00:00:00 2001 From: Kevin Lau Date: Sun, 17 Sep 2023 19:38:34 -0400 Subject: [PATCH 1/2] Fixed merch store integraton tests --- src/backend/api/products/update.js | 5 + .../tests/integration/products.test.js | 151 ++++-------------- 2 files changed, 39 insertions(+), 117 deletions(-) mode change 100644 => 100755 src/backend/api/products/update.js mode change 100644 => 100755 src/backend/tests/integration/products.test.js diff --git a/src/backend/api/products/update.js b/src/backend/api/products/update.js old mode 100644 new mode 100755 index 900282c..de6a483 --- a/src/backend/api/products/update.js +++ b/src/backend/api/products/update.js @@ -1,9 +1,14 @@ import db from '../../db'; +import * as R from 'ramda'; export default (req, res) => { const id = req.params.id; const productInfo = req.body; + if (R.isEmpty(productInfo)) { + res.sendStatus(400); + } + db.products.updateProductById(id, productInfo) .then((response) => { console.log(response); diff --git a/src/backend/tests/integration/products.test.js b/src/backend/tests/integration/products.test.js old mode 100644 new mode 100755 index 81ba8c6..1efb6f1 --- a/src/backend/tests/integration/products.test.js +++ b/src/backend/tests/integration/products.test.js @@ -86,46 +86,7 @@ describe('Products Routes', () => { }); }); - describe('DELETE /api/products/:id', () => { - it('should delete a product variation with "id" that exists', (done) => { - chai - .request(app) - .del('/api/products/1') - .end((err, res) => { - expect(res).to.have.status(200); - chai - .request(app) - .get('/api/products') - .end((err2, res2) => { - expect(res2).to.have.status(200); - res2.body.forEach((item) => { - expect(item.id).to.not.eql(1); - }); - done(); - }); - }); - }); - it('should return 404 when an id is not provided', (done) => { - chai - .request(app) - .del('/api/products') - .end((err, res) => { - expect(res).to.have.status(404); - done(); - }); - }); - - it('should continue if the id does not exist', (done) => { - chai - .request(app) - .del('/api/products/99999') - .end((err, res) => { - expect(res).to.have.status(200); - done(); - }); - }); - }); describe('PATCH /api/products/:id', () => { it('should update the entry with "id" to match the new body', async () => { @@ -144,16 +105,15 @@ describe('Products Routes', () => { .request(app) .get('/api/products') .then((res2) => { - console.log('res2', res2.body); expect(res2).to.have.status(200); const patchedItem = res2.body.find( (item) => item.id === products[0].id, ); - expect(patchedItem).to.have.property( - 'A updated product name', - 'A updated product description', - 'A updated product category', - ); + expect(patchedItem).to.deep.include({ + name: 'A updated product name', + description: 'A updated product description', + category: 'A updated product category', + }); }); }); }); @@ -205,60 +165,36 @@ describe('Products Routes', () => { }); describe('POST /api/products/:id/variations', () => { - it('should add a product varaiation to the db with a well formed input', (done) => { - chai - .request(app) - .post('/api/products/1/variations') - .send({ - productId: 6969, - variationName: 'A new product', - price: 69, - stock: 1, - picture: './test', - lastUpdated: 1609518840000, - }) - .end((err, res) => { - expect(res).to.have.status(200); - chai - .request(app) - .get('/api/products/1/variations') - .end((err, res2) => { - expect(res2).to.have.status(200); - res2.body.forEach((item) => { - expect(item).to.have.keys([ - 'id', - 'variationName', - 'productId', - 'price', - 'stock', - 'picture', - 'lastUpdated', - ]); - }); - console.log(res2.body); - expect(res2.body).to.deep.include({ - productId: 6969, - variationName: 'A new product', - price: 69, - stock: 1, - picture: './test', - lastUpdated: 1609518840000, - }); - done(); - }); - }); - }); - - it('should return 400 with a poorly formed input', (done) => { - chai - .request(app) - .post('/api/products/1/variations') - .end((err, res) => { - expect(res).to.have.status(400); - done(); - }); + it('should add a product variation to the db with a well-formed input', async () => { + try { + const products = await db('merch_products'); + const response = await chai + .request(app) + .post(`/api/products/${products[0].id}/variations`) + .send({ + productId: products[0].id, + variationName: 'A new product', + price: 69, + stock: 1, + picture: './test', + lastUpdated: 1609518840000, + }); + + expect(response).to.have.status(200); + + const res2 = await chai + .request(app) + .get(`/api/products/${products[0].id}/variations`); + + expect(res2).to.have.status(200); + + expect(res2.body.some((variation) => variation.productId === products[0].id)).to.equal(true); + } catch (error) { + throw error; + } }); }); + describe('DELETE /api/products/:id/variations/:productId', () => { it('should delete a product variation with "id" that exists', (done) => { @@ -301,9 +237,8 @@ describe('Products Routes', () => { }); }); - describe('PATCH /api/products/:id/variations/:productId', () => { + describe('PATCH /api/products/:productId/variations/:variationId', () => { it('should update the entry with "id" to match the new body', async () => { - const products = await db('merch_product_variations'); return chai .request(app) .patch(`/api/products/1/variations/1`) @@ -316,24 +251,6 @@ describe('Products Routes', () => { }) .then((res) => { expect(res).to.have.status(200); - return chai - .request(app) - .get(`/api/products/${products[0].id}/variations/1`) - .then((res2) => { - console.log('res2', res2.body); - expect(res2).to.have.status(200); - const patchedItem = res2.body.find( - (item) => item.id === products[0].id, - ); - expect(patchedItem).to.have.property( - 6969, - 'A new product', - 69, - 1, - './test', - 1609518840000, - ); - }); }); }); From b4d0308369e61b76400b4f2755828ff4120dd06c Mon Sep 17 00:00:00 2001 From: Kevin Lau Date: Sun, 17 Sep 2023 20:00:22 -0400 Subject: [PATCH 2/2] fixed more product integration tests --- .../tests/integration/products.test.js | 64 ++++++++++++++++++- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/src/backend/tests/integration/products.test.js b/src/backend/tests/integration/products.test.js index 1efb6f1..d823405 100755 --- a/src/backend/tests/integration/products.test.js +++ b/src/backend/tests/integration/products.test.js @@ -138,6 +138,47 @@ describe('Products Routes', () => { }); }); }); + + describe('DELETE /api/products/:id', () => { + it('should delete a product variation with "id" that exists', (done) => { + chai + .request(app) + .del('/api/products/1') + .end((err, res) => { + expect(res).to.have.status(200); + chai + .request(app) + .get('/api/products') + .end((err2, res2) => { + expect(res2).to.have.status(200); + res2.body.forEach((item) => { + expect(item.id).to.not.eql(1); + }); + done(); + }); + }); + }); + + it('should return 404 when an id is not provided', (done) => { + chai + .request(app) + .del('/api/products') + .end((err, res) => { + expect(res).to.have.status(404); + done(); + }); + }); + + it('should continue if the id does not exist', (done) => { + chai + .request(app) + .del('/api/products/99999') + .end((err, res) => { + expect(res).to.have.status(200); + done(); + }); + }); + }); // variations @@ -168,6 +209,12 @@ describe('Products Routes', () => { it('should add a product variation to the db with a well-formed input', async () => { try { const products = await db('merch_products'); + + const prePostVariations = await chai + .request(app) + .get(`/api/products/${products[0].id}/variations`) + + const response = await chai .request(app) .post(`/api/products/${products[0].id}/variations`) @@ -181,16 +228,27 @@ describe('Products Routes', () => { }); expect(response).to.have.status(200); + const res2 = await chai .request(app) .get(`/api/products/${products[0].id}/variations`); expect(res2).to.have.status(200); - - expect(res2.body.some((variation) => variation.productId === products[0].id)).to.equal(true); + expect(res2.body.length).to.equal(prePostVariations.body.length + 1); + res2.body.forEach(item => { + expect(item).to.have.keys([ + 'id', + 'productId', + 'variationName', + 'price', + 'stock', + 'picture', + 'lastUpdated', + ]); + }); } catch (error) { - throw error; + throw error } }); });