submit.cy.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Choices, LabelStudio, ToolBar } from "@humansignal/frontend-test/helpers/LSF";
  2. import { FF_CUSTOM_SCRIPT, FF_DEV_3873, FF_REVIEWER_FLOW } from "../../../../src/utils/feature-flags";
  3. enum RESPONSE_TYPE {
  4. DRAFT = "draft",
  5. SUBMIT = "submit",
  6. }
  7. const RESPONSE_DELAY = 100;
  8. const WAITING_FOR_EVENTS_DELAY = 200;
  9. describe("Annotation submitting process", () => {
  10. it("should not create a duplicate annotation if the annotation and draft are saved concurrently", () => {
  11. LabelStudio.addFeatureFlagsOnPageLoad({
  12. [FF_DEV_3873]: true,
  13. [FF_REVIEWER_FLOW]: true,
  14. [FF_CUSTOM_SCRIPT]: true,
  15. });
  16. // here will be stored an order of completed saving action
  17. // it consists of strings: "draft" and "submit"
  18. const callApi = cy.spy().as("callApi");
  19. LabelStudio.params()
  20. .data({
  21. text: "Some words",
  22. })
  23. .config(`<View>
  24. <Text name="text" value="$text" />
  25. <Choices toName="text" name="choice">
  26. <Choice value="ClickMe"/>
  27. </Choices>
  28. </View>`)
  29. // To have "new" annotation we need to use userGenerate
  30. .withAnnotation({ userGenerate: true, result: [] })
  31. .withEventListener("submitAnnotation", () => {
  32. callApi(RESPONSE_TYPE.SUBMIT);
  33. })
  34. .withEventListener("submitDraft", () => {
  35. return new Promise<void>((resolve) => {
  36. // initialize saving annotation exactly when request of saving draft should be sent to the server
  37. ToolBar.submitBtn.click();
  38. // this emulates server response delay
  39. setTimeout(() => {
  40. callApi(RESPONSE_TYPE.DRAFT);
  41. resolve();
  42. }, RESPONSE_DELAY);
  43. });
  44. })
  45. .init();
  46. // just to have something to save
  47. Choices.findChoice("ClickMe").click();
  48. // Preventing waiting for autosave. It will save time.
  49. cy.window().then(async (win) => {
  50. win.Htx.annotationStore.selected.saveDraftImmediately();
  51. });
  52. // Wait for all submit events to be invoked, to have `callOrder` ready to check
  53. cy.wait(WAITING_FOR_EVENTS_DELAY);
  54. // Check order
  55. cy.window().then((win) => {
  56. expect(callApi).to.be.calledTwice;
  57. expect(callApi.firstCall).to.be.calledWith(RESPONSE_TYPE.DRAFT);
  58. expect(callApi.secondCall).to.be.calledWith(RESPONSE_TYPE.SUBMIT);
  59. });
  60. });
  61. });