Skip to content

Commit a8c4abe

Browse files
committed
fixed test cases and used db emulator
1 parent 42bf5d9 commit a8c4abe

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

models/stocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const userStocksModel = firestore.collection("user-stocks");
66
* Adds Stocks
77
*
88
* @param stockData { Object }: stock data object to be stored in DB
9-
* @return {Promise<{stockId: string}>}
9+
* @return {Promise<{id: string, stockData: Object}>}
1010
*/
1111
const addStock = async (stockData) => {
1212
try {

test/integration/stocks.test.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import chai from "chai";
22
import chaiHttp from "chai-http";
3-
43
import app from "../../server";
54
import authService from "../../services/authService";
65
import addUser from "../utils/addUser";
@@ -16,44 +15,34 @@ const { expect } = chai;
1615
describe("GET /stocks/:userId", function () {
1716
let jwt: string;
1817
let userId: string;
18+
let userStock;
19+
const stockData = { name: "EURO", quantity: 2, price: 10 };
1920

2021
beforeEach(async function () {
2122
userId = await addUser();
2223
jwt = authService.generateAuthToken({ userId });
24+
const { id } = await stocks.addStock(stockData);
25+
userStock = { stockId: id, stockName: "EURO", quantity: 1, orderValue: 10, initialStockValue: 2 };
2326
});
2427

2528
afterEach(async function () {
2629
await cleanDb();
2730
sinon.restore();
2831
});
2932

30-
it("Should return user stocks when stocks are available", function (done) {
31-
const userStocks = [{id: "5YGjUSW1SinwCNfuLXOO", userId: "DHLG3gYMTtMenj6lciWz", stockId: "s2eYDswDUAoQxwAhh07f", stockName: "EURO", quantity: 1, orderValue: 150, initialStockValue: 150}];
32-
33-
sinon.stub(stocks, "fetchUserStocks").resolves(userStocks);
34-
35-
chai
36-
.request(app)
37-
.get(`/stocks/${userId}?dev=true`)
38-
.set("cookie", `${cookieName}=${jwt}`)
39-
.end((err, res) => {
40-
if (err) return done(err);
33+
it("Should return user stocks when stocks are available", async function () {
34+
await stocks.updateUserStocks(userId, userStock);
4135

42-
expect(res).to.have.status(200);
43-
expect(res.body).to.be.an("object");
44-
expect(res.body.message).to.equal("User stocks returned successfully!");
45-
expect(res.body.userStocks).to.be.an("array");
46-
expect(res.body.userStocks).to.deep.equal(userStocks);
36+
const res = await chai.request(app).get(`/stocks/${userId}?dev=true`).set("cookie", `${cookieName}=${jwt}`);
4737

48-
return done();
49-
});
38+
expect(res).to.have.status(200);
39+
expect(res.body).to.be.an("object");
40+
expect(res.body.message).to.equal("User stocks returned successfully!");
41+
expect(res.body.userStocks).to.be.an("array");
42+
expect(res.body.userStocks.map(({ id, ...rest }) => rest)).to.deep.equal([{ ...userStock, userId }]);
5043
});
5144

5245
it("Should return empty object when no stocks are found", function (done) {
53-
const userStocks=[];
54-
55-
sinon.stub(stocks, "fetchUserStocks").resolves(userStocks);
56-
5746
chai
5847
.request(app)
5948
.get(`/stocks/${userId}?dev=true`)
@@ -89,7 +78,6 @@ describe("GET /stocks/:userId", function () {
8978
});
9079

9180
it("Should return 500 when an internal server error occurs", function (done) {
92-
// Stub fetchUserStocks to throw an error
9381
sinon.stub(stocks, "fetchUserStocks").throws(new Error("Database error"));
9482

9583
chai
@@ -106,4 +94,4 @@ describe("GET /stocks/:userId", function () {
10694
return done();
10795
});
10896
});
109-
});
97+
});

0 commit comments

Comments
 (0)