Skip to content

Commit

Permalink
fix test naming
Browse files Browse the repository at this point in the history
  • Loading branch information
AnujChhikara committed Jan 8, 2025
1 parent 13fe9fc commit 402ddd5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 95 deletions.
4 changes: 2 additions & 2 deletions test/unit/models/progresses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("progressModel", function () {
sinon.restore();
});

describe("buildQueryToFetchPaginatedDocs", function () {
describe("getPaginatedProgressDocument", function () {
let userId;
let userId2;
let userId3;
Expand Down Expand Up @@ -102,7 +102,7 @@ describe("progressModel", function () {
});
});

describe("getProgressDocument", function () {
describe("addUserDetailsToProgressDocs", function () {
afterEach(function () {
cleanDb();
sinon.restore();
Expand Down
213 changes: 120 additions & 93 deletions test/unit/utils/progresses.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const chai = require("chai");
const { expect } = chai;
const sinon = require("sinon");
const cleanDb = require("../../utils/cleanDb");
const { buildQueryToFetchPaginatedDocs, getPaginatedProgressDocs } = require("../../../utils/progresses");
const fireStore = require("../../../utils/firestore");
Expand All @@ -8,122 +9,148 @@ const { stubbedModelTaskProgressData, stubbedModelProgressData } = require("../.
const {
PROGRESSES_RESPONSE_MESSAGES: { PROGRESS_DOCUMENT_NOT_FOUND },
} = require("../../../constants/progresses");
describe("buildQueryToFetchPaginatedDocs", function () {
beforeEach(async function () {
const progressData = stubbedModelTaskProgressData("userId", "task1", 1683072000000, 1682985600000);
const progressData2 = stubbedModelTaskProgressData("userId2", "task2", 1683072000000, 1682985600000);
const progressData3 = stubbedModelProgressData("userId", 1683072000000, 1682985600000);
const progressData4 = stubbedModelProgressData("userId2", 1683072000000, 1682985600000);
await progressesCollection.add(progressData);
await progressesCollection.add(progressData2);
await progressesCollection.add(progressData3);
await progressesCollection.add(progressData4);
});

describe("Utils | Progresses", function () {
afterEach(async function () {
await cleanDb();
sinon.restore();
});

it("should build a query with type filter", async function () {
const queryParams = {
type: "task",
size: 100,
page: 0,
};
describe("buildQueryToFetchPaginatedDocs", function () {
beforeEach(async function () {
const progressData = stubbedModelTaskProgressData("userId", "task1", 1683072000000, 1682985600000);
const progressData2 = stubbedModelTaskProgressData("userId2", "task2", 1683072000000, 1682985600000);
const progressData3 = stubbedModelProgressData("userId", 1683072000000, 1682985600000);
const progressData4 = stubbedModelProgressData("userId2", 1683072000000, 1682985600000);
await progressesCollection.add(progressData);
await progressesCollection.add(progressData2);
await progressesCollection.add(progressData3);
await progressesCollection.add(progressData4);
});

const { totalProgressCount } = await buildQueryToFetchPaginatedDocs(queryParams);
expect(totalProgressCount).to.equal(2);
});
afterEach(async function () {
await cleanDb();
});

it("should build a query with userId filter", async function () {
const queryParams = {
userId: "userId",
size: 100,
page: 0,
};

const { baseQuery, totalProgressCount } = await buildQueryToFetchPaginatedDocs(queryParams);
const results = await baseQuery.get();
const docs = results.docs.map((doc) => doc.data());
expect(docs[0].type).to.equal("user");
expect(totalProgressCount).to.equal(1);
});
it("should build a query with type filter", async function () {
const queryParams = {
type: "task",
size: 100,
page: 0,
};

it("should build a query with taskId filter", async function () {
const queryParams = {
taskId: "task1",
size: 100,
page: 0,
};
const { totalProgressCount } = await buildQueryToFetchPaginatedDocs(queryParams);
expect(totalProgressCount).to.equal(2);
});

const { totalProgressCount } = await buildQueryToFetchPaginatedDocs(queryParams);
expect(totalProgressCount).to.equal(1);
});
it("should build a query with userId filter", async function () {
const queryParams = {
userId: "userId",
size: 100,
page: 0,
};

const { baseQuery, totalProgressCount } = await buildQueryToFetchPaginatedDocs(queryParams);
const results = await baseQuery.get();
const docs = results.docs.map((doc) => doc.data());
expect(docs[0].type).to.equal("user");
expect(totalProgressCount).to.equal(1);
});

it("should apply default sorting when orderBy is not provided", async function () {
const queryParams = {
type: "task",
size: 100,
page: 0,
};
it("should build a query with taskId filter", async function () {
const queryParams = {
taskId: "task1",
size: 100,
page: 0,
};

const { baseQuery } = await buildQueryToFetchPaginatedDocs(queryParams);
const results = await baseQuery.get();
const docs = results.docs.map((doc) => doc.data());
const { totalProgressCount } = await buildQueryToFetchPaginatedDocs(queryParams);
expect(totalProgressCount).to.equal(1);
});

expect(docs[0].type).to.equal("task");
});
it("should apply default sorting when orderBy is not provided", async function () {
const queryParams = {
type: "task",
size: 100,
page: 0,
};

it("should handle pagination correctly", async function () {
const queryParams = {
type: "task",
size: 1,
page: 1,
};
const { baseQuery } = await buildQueryToFetchPaginatedDocs(queryParams);
const results = await baseQuery.get();
const docs = results.docs.map((doc) => doc.data());

const { baseQuery } = await buildQueryToFetchPaginatedDocs(queryParams);
const results = await baseQuery.get();
expect(results.size).to.equal(1);
});
expect(docs[0].type).to.equal("task");
});

it("should return empty results for a large page number", async function () {
const queryParams = {
type: "task",
size: 100,
page: 10,
};
it("should handle pagination correctly", async function () {
const queryParams = {
type: "task",
size: 1,
page: 1,
};

const { baseQuery } = await buildQueryToFetchPaginatedDocs(queryParams);
const results = await baseQuery.get();
expect(results.size).to.equal(0);
});
const { baseQuery } = await buildQueryToFetchPaginatedDocs(queryParams);
const results = await baseQuery.get();
expect(results.size).to.equal(1);
});

it("should throw a NotFound error if no documents are found and no page is specified", async function () {
const query = progressesCollection.where("userId", "==", "nonExistentUser");
it("should return empty results for a large page number", async function () {
const queryParams = {
type: "task",
size: 100,
page: 10,
};

try {
await getPaginatedProgressDocs(query);
throw new Error("Test failed: expected a NotFound error to be thrown.");
} catch (err) {
expect(err.message).to.equal(PROGRESS_DOCUMENT_NOT_FOUND);
}
const { baseQuery } = await buildQueryToFetchPaginatedDocs(queryParams);
const results = await baseQuery.get();
expect(results.size).to.equal(0);
});
});

it("should return an empty array if no documents are found and a page is specified", async function () {
const query = progressesCollection.where("userId", "==", "nonExistentUser");
const results = await getPaginatedProgressDocs(query, 1);
describe("getPaginatedProgressDocs", function () {
beforeEach(async function () {
const progressData = stubbedModelTaskProgressData("userId", "task1", 1683072000000, 1682985600000);
const progressData2 = stubbedModelTaskProgressData("userId2", "task2", 1683072000000, 1682985600000);
const progressData3 = stubbedModelProgressData("userId", 1683072000000, 1682985600000);
const progressData4 = stubbedModelProgressData("userId2", 1683072000000, 1682985600000);
await progressesCollection.add(progressData);
await progressesCollection.add(progressData2);
await progressesCollection.add(progressData3);
await progressesCollection.add(progressData4);
});

// eslint-disable-next-line no-unused-expressions
expect(results).to.be.an("array").that.is.empty;
});
afterEach(async function () {
await cleanDb();
sinon.restore();
});

it("should throw a NotFound error if no documents are found and no page is specified", async function () {
const query = progressesCollection.where("userId", "==", "nonExistentUser");

try {
await getPaginatedProgressDocs(query);
throw new Error("Test failed: expected a NotFound error to be thrown.");
} catch (err) {
expect(err.message).to.equal(PROGRESS_DOCUMENT_NOT_FOUND);
}
});

it("should return an empty array if no documents are found and a page is specified", async function () {
const query = progressesCollection.where("userId", "==", "nonExistentUser");
const results = await getPaginatedProgressDocs(query, 1);

// eslint-disable-next-line no-unused-expressions
expect(results).to.be.an("array").that.is.empty;
});

it("should handle queries returning multiple documents", async function () {
const query = progressesCollection.where("type", "==", "task");
const results = await getPaginatedProgressDocs(query);
it("should handle queries returning multiple documents", async function () {
const query = progressesCollection.where("type", "==", "task");
const results = await getPaginatedProgressDocs(query);

expect(results).to.be.an("array").that.has.lengthOf(2);
results.forEach((doc) => {
expect(doc).to.have.property("id").that.is.a("string");
expect(results).to.be.an("array").that.has.lengthOf(2);
results.forEach((doc) => {
expect(doc).to.have.property("id").that.is.a("string");
});
});
});
});

0 comments on commit 402ddd5

Please sign in to comment.