LongJobs.spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { flushPromises } from "@vue/test-utils";
  2. import LongJobs from "@/components/LongJobs.vue";
  3. import FloatingBox from "@/components/FloatingBox.vue";
  4. import { getWrapper } from "@/tests/utils/utils";
  5. import { useLongJobsStore } from "@/stores/longJobs";
  6. import { useWebsocketsStore } from "@/stores/websockets";
  7. describe("LongJobs component", async () => {
  8. beforeEach(async context => {
  9. context.mockSocket = {
  10. data: {
  11. dispatch: {
  12. "users.getLongJobs": () => ({
  13. status: "success",
  14. data: {
  15. longJobs: [
  16. {
  17. id: "8704d336-660f-4d23-8c18-a7271c6656b5",
  18. name: "Bulk verifying songs",
  19. status: "success",
  20. message:
  21. "50 songs have been successfully verified"
  22. }
  23. ]
  24. }
  25. }),
  26. "users.getLongJob": id =>
  27. id === "bf3dc3aa-e7aa-4b69-bfd1-8e979fe7dfa5"
  28. ? {
  29. status: "success",
  30. data: {
  31. longJob: {
  32. id,
  33. name: "Bulk editing tags",
  34. status: "success",
  35. message: "Successfully edited tags."
  36. }
  37. }
  38. }
  39. : {
  40. status: "error",
  41. message: "Long job not found."
  42. },
  43. "users.removeLongJob": () => ({
  44. status: "success"
  45. })
  46. },
  47. progress: {
  48. "users.getLongJob": id =>
  49. id === "bf3dc3aa-e7aa-4b69-bfd1-8e979fe7dfa5"
  50. ? [
  51. {
  52. id,
  53. name: "Bulk editing tags",
  54. status: "started",
  55. message: "Updating tags."
  56. },
  57. {
  58. id,
  59. name: "Bulk editing tags",
  60. status: "update",
  61. message: "Updating tags in MongoDB."
  62. }
  63. ]
  64. : []
  65. },
  66. on: {
  67. "keep.event:longJob.added": {
  68. data: { jobId: "bf3dc3aa-e7aa-4b69-bfd1-8e979fe7dfa5" }
  69. },
  70. "keep.event:longJob.removed": {
  71. data: { jobId: "8704d336-660f-4d23-8c18-a7271c6656b5" }
  72. }
  73. }
  74. }
  75. };
  76. });
  77. test("component does not render if there are no jobs", async () => {
  78. const wrapper = await getWrapper(LongJobs, { mockSocket: true });
  79. expect(wrapper.findComponent(FloatingBox).exists()).toBeFalsy();
  80. });
  81. test("component and jobs render if jobs exists", async ({ mockSocket }) => {
  82. const wrapper = await getWrapper(LongJobs, {
  83. mockSocket,
  84. stubs: { FloatingBox },
  85. loginRequired: true
  86. });
  87. expect(wrapper.findComponent(FloatingBox).exists()).toBeTruthy();
  88. const activeJobs = wrapper.findAll(".active-jobs .active-job");
  89. const { longJobs } =
  90. mockSocket.data.dispatch["users.getLongJobs"]().data;
  91. expect(activeJobs.length).toBe(longJobs.length);
  92. });
  93. describe.each(["started", "update", "success", "error"])(
  94. "job with %s status",
  95. status => {
  96. const isRemoveable = status === "success" || status === "error";
  97. beforeEach(async context => {
  98. const getLongJobs =
  99. context.mockSocket.data.dispatch["users.getLongJobs"]();
  100. getLongJobs.data.longJobs[0].status = status;
  101. context.mockSocket.data.dispatch["users.getLongJobs"] = () =>
  102. getLongJobs;
  103. context.wrapper = await getWrapper(LongJobs, {
  104. mockSocket: context.mockSocket,
  105. stubs: { FloatingBox },
  106. loginRequired: true
  107. });
  108. });
  109. test("status icon, name and message render correctly", ({
  110. wrapper,
  111. mockSocket
  112. }) => {
  113. const activeJob = wrapper.find(".active-jobs .active-job");
  114. const job =
  115. mockSocket.data.dispatch["users.getLongJobs"]().data
  116. .longJobs[0];
  117. let icon;
  118. if (job.status === "success") icon = "Complete";
  119. else if (job.status === "error") icon = "Failed";
  120. else if (job.status === "started" || job.status === "update")
  121. icon = "In Progress";
  122. icon = `i[content="${icon}"]`;
  123. expect(activeJob.find(icon).exists()).toBeTruthy();
  124. expect(activeJob.find(".name").text()).toBe(job.name);
  125. (<any>(
  126. activeJob.find(".actions .message").element.parentElement
  127. ))._tippy.show();
  128. expect(
  129. document.body.querySelector(
  130. "body > [id^=tippy] .tippy-box .long-job-message"
  131. ).textContent
  132. ).toBe(`Latest Update:${job.message}`);
  133. });
  134. test(`job is ${
  135. isRemoveable ? "" : "not "
  136. }removed on click of clear icon`, async ({ wrapper }) => {
  137. await wrapper
  138. .find(".active-job .actions .clear")
  139. .trigger("click");
  140. await flushPromises();
  141. const longJobsStore = useLongJobsStore();
  142. expect(longJobsStore.removeJob).toBeCalledTimes(
  143. isRemoveable ? 1 : 0
  144. );
  145. expect(wrapper.findComponent(FloatingBox).exists()).not.toEqual(
  146. isRemoveable
  147. );
  148. });
  149. }
  150. );
  151. test("keep.event:longJob.added", async ({ mockSocket }) => {
  152. const wrapper = await getWrapper(LongJobs, {
  153. mockSocket,
  154. stubs: { FloatingBox },
  155. loginRequired: true
  156. });
  157. const websocketsStore = useWebsocketsStore();
  158. websocketsStore.socket.trigger("on", "keep.event:longJob.added");
  159. await flushPromises();
  160. const longJobsStore = useLongJobsStore();
  161. expect(longJobsStore.setJob).toBeCalledTimes(3);
  162. const activeJobs = wrapper.findAll(".active-jobs .active-job");
  163. const { longJobs } =
  164. mockSocket.data.dispatch["users.getLongJobs"]().data;
  165. expect(activeJobs.length).toBe(longJobs.length + 1);
  166. });
  167. test("keep.event:longJob.removed", async ({ mockSocket }) => {
  168. const wrapper = await getWrapper(LongJobs, {
  169. mockSocket,
  170. stubs: { FloatingBox },
  171. loginRequired: true
  172. });
  173. const websocketsStore = useWebsocketsStore();
  174. websocketsStore.socket.trigger("on", "keep.event:longJob.removed");
  175. await flushPromises();
  176. const longJobsStore = useLongJobsStore();
  177. expect(longJobsStore.removeJob).toBeCalledTimes(1);
  178. expect(wrapper.findComponent(FloatingBox).exists()).toBeFalsy();
  179. });
  180. });