|
| 1 | +import TDSClient, { Task } from ".."; |
| 2 | + |
| 3 | +const myClient = TDSClient(process.env.TODOIST_TOKEN); |
| 4 | + |
| 5 | +const generalExpectedTask = Task({ |
| 6 | + content: "Hello world", |
| 7 | +}); |
| 8 | +let generalExpectedTaskID: number; |
| 9 | + |
| 10 | +describe("Client-Side Task Creation", () => { |
| 11 | + test("Create Empty Task", () => { |
| 12 | + const emptyTask = Task(); |
| 13 | + expect(emptyTask).toMatchObject<ClientCreatedTask>({ |
| 14 | + content: "_No_Task_Name_Provided_", |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + test("Create Normal Task", () => { |
| 19 | + expect(generalExpectedTask).toMatchObject<ClientCreatedTask>({ |
| 20 | + content: "Hello world", |
| 21 | + }); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe("API Tasks Functions", () => { |
| 26 | + test("Create A Task", async () => { |
| 27 | + // creates the task in the server |
| 28 | + |
| 29 | + let apiTask = await myClient.task |
| 30 | + .create(generalExpectedTask) |
| 31 | + // @ts-ignore: Task.create should return APITaskObject |
| 32 | + .then((res) => res.data as APITaskObject); |
| 33 | + |
| 34 | + expect(apiTask.id).toBeTruthy(); |
| 35 | + |
| 36 | + //fix needed --> task.create should return an APITaskObject --> issue #15 |
| 37 | + |
| 38 | + /* |
| 39 | + let apiTask = await myClient.task.create(generalExpectedTask); |
| 40 | + expect(apiTask).toMatchObject<APITaskObject>({ |
| 41 | + ...apiTask, |
| 42 | + content: "Hello world", |
| 43 | + }); |
| 44 | +
|
| 45 | + save the task id for next tests |
| 46 | + generalExpectedTaskID = apiTask.id;*/ |
| 47 | + |
| 48 | + //save the task id for next tests |
| 49 | + generalExpectedTaskID = apiTask.id; |
| 50 | + }); |
| 51 | + |
| 52 | + test("Get A Task", async () => { |
| 53 | + let apiTask = await myClient.task.get(generalExpectedTaskID); |
| 54 | + |
| 55 | + expect(apiTask.content).toBe(generalExpectedTask.content); |
| 56 | + }); |
| 57 | + |
| 58 | + test("Close A Task", async () => { |
| 59 | + let status = (await myClient.task.closeTask(generalExpectedTaskID)).status; |
| 60 | + |
| 61 | + expect(status).toBe(204); |
| 62 | + }); |
| 63 | + |
| 64 | + // no active tasks now |
| 65 | + test("Create Two Tasks", async () => { |
| 66 | + await myClient.task.create({ content: "First task" }); |
| 67 | + await myClient.task.create({ content: "Second task" }); |
| 68 | + }); |
| 69 | + |
| 70 | + test("Get All Active Tasks JSON", async () => { |
| 71 | + const responseTasks = await myClient.task.getAllJSON(); |
| 72 | + const firstTaskExists = responseTasks.some( |
| 73 | + (taskObj) => taskObj.content === "First task" |
| 74 | + ); |
| 75 | + const secondTaskExists = responseTasks.some( |
| 76 | + (taskObj) => taskObj.content === "Second task" |
| 77 | + ); |
| 78 | + |
| 79 | + expect(responseTasks.length).toBe(2); |
| 80 | + |
| 81 | + expect(firstTaskExists).toBe(true); |
| 82 | + expect(secondTaskExists).toBe(true); |
| 83 | + }); |
| 84 | + |
| 85 | + test("Get All Active Tasks Names", async () => { |
| 86 | + const responseTasks = await myClient.task.getAll(); |
| 87 | + const firstTaskExists = responseTasks.some((name) => name === "First task"); |
| 88 | + const secondTaskExists = responseTasks.some( |
| 89 | + (name) => name === "Second task" |
| 90 | + ); |
| 91 | + |
| 92 | + expect(responseTasks.length).toBe(2); |
| 93 | + |
| 94 | + expect(firstTaskExists).toBe(true); |
| 95 | + expect(secondTaskExists).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + test("Complete All Previous Tasks", async () => { |
| 99 | + let allTasksJSON = await myClient.task.getAllJSON(); |
| 100 | + |
| 101 | + for (let i = 0; i < allTasksJSON.length; ++i) { |
| 102 | + let status = (await myClient.task.closeTask(allTasksJSON[i].id)).status; |
| 103 | + expect(status).toBe(204); |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + // no active tasks now |
| 108 | + /* Not working now because of #19 |
| 109 | + test("Create Two Tasks For Today", async () => { |
| 110 | + // create tasks in today inbox |
| 111 | + const due_info = { |
| 112 | + due_lang: "en", |
| 113 | + due_string: "today", |
| 114 | + }; |
| 115 | + await myClient.task.create({ content: "First task", ...due_info }); |
| 116 | + await myClient.task.create({ content: "Second task", ...due_info }); |
| 117 | +
|
| 118 | + //get created tasks |
| 119 | + let allTodayJSON = await myClient.task.getTodayJSON(); |
| 120 | + const firstTaskExists = allTodayJSON.some( |
| 121 | + (taskObj) => taskObj.content === "First task" |
| 122 | + ); |
| 123 | + const secondTaskExists = allTodayJSON.some( |
| 124 | + (taskObj) => taskObj.content === "Second task" |
| 125 | + ); |
| 126 | +
|
| 127 | + console.log(allTodayJSON); |
| 128 | +
|
| 129 | + expect(allTodayJSON.length).toBe(2); |
| 130 | + expect(typeof allTodayJSON[0]).toBe("object"); |
| 131 | + expect(firstTaskExists).toBe(true); |
| 132 | + expect(secondTaskExists).toBe(true); |
| 133 | + }); |
| 134 | + */ |
| 135 | +}); |
0 commit comments