|
| 1 | +const Sinon = require("sinon"); |
| 2 | +const { expect } = require("chai"); |
| 3 | + |
| 4 | +const firestore = require("../../../utils/firestore"); |
| 5 | +const tasksModel = firestore.collection("tasks"); |
| 6 | +const cleanDb = require("../../utils/cleanDb"); |
| 7 | +const taskDataArray = require("../../fixtures/tasks/tasks")(); |
| 8 | +const { updateTaskStatusToDone } = require("../../../services/tasks"); |
| 9 | + |
| 10 | +describe("Tasks services", function () { |
| 11 | + describe("task status COMPLETED to DONE in bulk", function () { |
| 12 | + const tasks = []; |
| 13 | + const taskDetails = []; |
| 14 | + beforeEach(async function () { |
| 15 | + const addTasksPromises = []; |
| 16 | + taskDataArray.forEach((task) => { |
| 17 | + const taskData = { |
| 18 | + ...task, |
| 19 | + status: "COMPLETED", |
| 20 | + }; |
| 21 | + addTasksPromises.push(tasksModel.add(taskData)); |
| 22 | + }); |
| 23 | + |
| 24 | + await Promise.all(addTasksPromises); |
| 25 | + |
| 26 | + tasks.length = 0; |
| 27 | + taskDetails.length = 0; |
| 28 | + |
| 29 | + const snapshot = await tasksModel.where("status", "==", "COMPLETED").get(); |
| 30 | + |
| 31 | + snapshot.forEach((task) => { |
| 32 | + const id = task.id; |
| 33 | + const taskData = task.data(); |
| 34 | + tasks.push({ ...taskData, id }); |
| 35 | + taskDetails.push(id); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(async function () { |
| 40 | + await cleanDb(); |
| 41 | + Sinon.restore(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("successfully updated task status COMPLETED To DONE", async function () { |
| 45 | + const res = await updateTaskStatusToDone(tasks); |
| 46 | + |
| 47 | + expect(res).to.deep.equal({ |
| 48 | + totalUpdatedStatus: 8, |
| 49 | + totalOperationsFailed: 0, |
| 50 | + updatedTaskDetails: taskDetails, |
| 51 | + failedTaskDetails: [], |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should throw an error if firebase batch operation fails", async function () { |
| 56 | + const batchStub = Sinon.stub(firestore, "batch"); |
| 57 | + batchStub.returns({ |
| 58 | + update: function () {}, |
| 59 | + commit: function () { |
| 60 | + throw new Error("Firebase batch operation failed"); |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + const res = await updateTaskStatusToDone(tasks); |
| 65 | + |
| 66 | + expect(res).to.deep.equal({ |
| 67 | + totalUpdatedStatus: 0, |
| 68 | + totalOperationsFailed: 8, |
| 69 | + updatedTaskDetails: [], |
| 70 | + failedTaskDetails: taskDetails, |
| 71 | + }); |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments