emberflow 1.4.3 → 1.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -57,6 +57,9 @@ const projectConfig = {
57
57
  admin.initializeApp({
58
58
  databaseURL: "https://test-project.firebaseio.com",
59
59
  });
60
+ const txnGet = {
61
+ get: jest.fn(),
62
+ };
60
63
  jest.mock("../utils/paths", () => {
61
64
  const originalModule = jest.requireActual("../utils/paths");
62
65
  return Object.assign(Object.assign({}, originalModule), { expandAndGroupDocPathsByEntity: jest.fn() });
@@ -415,7 +418,7 @@ describe("distribute", () => {
415
418
  }],
416
419
  ]]);
417
420
  (0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, []);
418
- await indexUtils.distribute(userDocsByDstPath);
421
+ await indexUtils.distributeFnNonTransactional(userDocsByDstPath);
419
422
  expect(admin.firestore().doc).toHaveBeenCalledTimes(1);
420
423
  expect(admin.firestore().doc).toHaveBeenCalledWith("/users/test-user-id/documents/test-doc-id");
421
424
  expect(queueInstructionsSpy).toHaveBeenCalledTimes(1);
@@ -442,7 +445,7 @@ describe("distribute", () => {
442
445
  dstPath: "/users/test-user-id/documents/test-doc-id",
443
446
  }],
444
447
  ]]);
445
- await indexUtils.distribute(userDocsByDstPath);
448
+ await indexUtils.distributeFnNonTransactional(userDocsByDstPath);
446
449
  expect(admin.firestore().doc).toHaveBeenCalledTimes(1);
447
450
  expect(admin.firestore().doc).toHaveBeenCalledWith("/users/test-user-id/documents/test-doc-id");
448
451
  expect(dbDoc.get).toHaveBeenCalledTimes(1);
@@ -483,6 +486,9 @@ describe("distributeLater", () => {
483
486
  });
484
487
  });
485
488
  describe("validateForm", () => {
489
+ beforeEach(() => {
490
+ (0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, []);
491
+ });
486
492
  it("returns an object with empty validationResult when document is valid", async () => {
487
493
  const entity = "user";
488
494
  const document = {
@@ -593,8 +599,6 @@ describe("runBusinessLogics", () => {
593
599
  let logicFn5;
594
600
  let logicFn6;
595
601
  let dbSpy;
596
- let simulateSubmitFormSpy;
597
- let createMetricExecutionSpy;
598
602
  let actionRef;
599
603
  beforeEach(() => {
600
604
  distributeFn = jest.fn();
@@ -604,8 +608,6 @@ describe("runBusinessLogics", () => {
604
608
  logicFn4 = jest.fn().mockResolvedValue({ status: "finished" });
605
609
  logicFn5 = jest.fn().mockResolvedValue({ status: "finished" });
606
610
  logicFn6 = jest.fn().mockResolvedValue({ status: "finished" });
607
- simulateSubmitFormSpy = jest.spyOn(indexUtils._mockable, "simulateSubmitForm").mockResolvedValue();
608
- createMetricExecutionSpy = jest.spyOn(indexUtils._mockable, "createMetricExecution").mockResolvedValue();
609
611
  const dbDoc = {
610
612
  get: jest.fn().mockResolvedValue({
611
613
  data: jest.fn().mockReturnValue({
@@ -633,8 +635,6 @@ describe("runBusinessLogics", () => {
633
635
  logicFn3.mockRestore();
634
636
  distributeFn.mockRestore();
635
637
  dbSpy.mockRestore();
636
- simulateSubmitFormSpy.mockRestore();
637
- createMetricExecutionSpy.mockRestore();
638
638
  });
639
639
  it("should call all matching logics and pass their results to distributeFn", async () => {
640
640
  const logics = [
@@ -691,71 +691,25 @@ describe("runBusinessLogics", () => {
691
691
  },
692
692
  ];
693
693
  (0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, logics);
694
- const runStatus = await indexUtils.runBusinessLogics(actionRef, action, distributeFn);
695
- expect(logicFn1).toHaveBeenCalledWith(action, new Map(), undefined);
696
- expect(logicFn2).toHaveBeenCalledWith(action, new Map(), undefined);
694
+ const runStatus = await indexUtils.runBusinessLogics(txnGet, action);
695
+ expect(logicFn1).toHaveBeenCalledWith(txnGet, action, new Map());
696
+ expect(logicFn2).toHaveBeenCalledWith(txnGet, action, new Map());
697
697
  expect(logicFn3).not.toHaveBeenCalled();
698
698
  expect(logicFn4).not.toHaveBeenCalled();
699
699
  expect(logicFn5).not.toHaveBeenCalled();
700
700
  expect(logicFn6).not.toHaveBeenCalled();
701
- expect(distributeFn).toHaveBeenCalledTimes(1);
702
- expect(distributeFn).toHaveBeenCalledWith(actionRef, [expect.objectContaining({
703
- status: "finished",
704
- execTime: expect.any(Number),
705
- timeFinished: expect.any(Timestamp),
706
- }),
707
- expect.objectContaining({
708
- status: "finished",
709
- execTime: expect.any(Number),
710
- timeFinished: expect.any(Timestamp),
711
- })], 0);
712
- expect(runStatus).toEqual("done");
713
- expect(createMetricExecutionSpy).toHaveBeenCalledTimes(1);
714
- expect(simulateSubmitFormSpy).toHaveBeenCalledTimes(1);
715
- });
716
- it("should recall logic when it returns \"partial-result\" status", async () => {
717
- logicFn2.mockResolvedValueOnce({ status: "partial-result", nextPage: {} });
718
- const logics = [
719
- {
720
- name: "Logic 1",
721
- actionTypes: ["create"],
722
- modifiedFields: ["field1"],
723
- entities: ["user"],
724
- logicFn: logicFn1,
725
- },
726
- {
727
- name: "Logic 2",
728
- actionTypes: "all",
729
- modifiedFields: ["field2"],
730
- entities: ["user"],
731
- logicFn: logicFn2,
732
- },
733
- ];
734
- (0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, logics);
735
- const runStatus = await indexUtils.runBusinessLogics(actionRef, action, distributeFn);
736
- expect(logicFn1).toHaveBeenCalledWith(action, new Map(), undefined);
737
- expect(logicFn2).toHaveBeenCalledWith(action, new Map(), undefined);
738
- expect(distributeFn).toHaveBeenCalledTimes(2);
739
- expect(distributeFn.mock.calls[0]).toEqual([actionRef,
740
- [expect.objectContaining({
701
+ expect(runStatus).toEqual({
702
+ status: "done",
703
+ logicResults: [{
741
704
  status: "finished",
742
705
  execTime: expect.any(Number),
743
706
  timeFinished: expect.any(Timestamp),
744
- }),
745
- expect.objectContaining({
746
- status: "partial-result",
747
- nextPage: {},
748
- execTime: expect.any(Number),
749
- timeFinished: expect.any(Timestamp),
750
- })], 0]);
751
- expect(logicFn2).toHaveBeenCalledWith(action, new Map(), {});
752
- expect(distributeFn.mock.calls[1]).toEqual([actionRef,
753
- [expect.objectContaining({
707
+ }, {
754
708
  status: "finished",
755
709
  execTime: expect.any(Number),
756
710
  timeFinished: expect.any(Timestamp),
757
- })], 1]);
758
- expect(runStatus).toEqual("done");
711
+ }],
712
+ });
759
713
  });
760
714
  it("should not call any logic when no matching logics are found but distributeFn should still be " +
761
715
  "called", async () => {
@@ -783,76 +737,14 @@ describe("runBusinessLogics", () => {
783
737
  },
784
738
  ];
785
739
  (0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, logics);
786
- const runStatus = await indexUtils.runBusinessLogics(actionRef, action, distributeFn);
740
+ const runStatus = await indexUtils.runBusinessLogics(txnGet, action);
787
741
  expect(logicFn1).not.toHaveBeenCalled();
788
742
  expect(logicFn2).not.toHaveBeenCalled();
789
743
  expect(logicFn3).not.toHaveBeenCalled();
790
- expect(distributeFn).toHaveBeenCalledTimes(1);
791
- expect(distributeFn).toHaveBeenCalledWith(actionRef, [], 0);
792
- expect(runStatus).toEqual("no-matching-logics");
793
- });
794
- it("should recall logic when it returns \"partial-result\" status indefinitely up to the config maxLogicResultPages", async () => {
795
- logicFn2.mockResolvedValue({ status: "partial-result", nextPage: {} });
796
- const logics = [
797
- {
798
- name: "Logic 1",
799
- actionTypes: ["create"],
800
- modifiedFields: ["field1"],
801
- entities: ["user"],
802
- logicFn: logicFn1,
803
- },
804
- {
805
- name: "Logic 2",
806
- actionTypes: "all",
807
- modifiedFields: ["field2"],
808
- entities: ["user"],
809
- logicFn: logicFn2,
810
- },
811
- ];
812
- (0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, logics);
813
- const runStatus = await indexUtils.runBusinessLogics(actionRef, action, distributeFn);
814
- expect(logicFn1).toHaveBeenCalledWith(action, new Map(), undefined);
815
- expect(logicFn2).toHaveBeenCalledWith(action, new Map(), undefined);
816
- expect(logicFn1).toHaveBeenCalledTimes(1);
817
- expect(logicFn2).toHaveBeenCalledTimes(10);
818
- expect(distributeFn).toHaveBeenCalledTimes(10);
819
- expect(runStatus).toEqual("done");
820
- });
821
- it("should return when a logic returns a \"cancel-then-retry\" status", async () => {
822
- logicFn2.mockResolvedValue({ status: "cancel-then-retry" });
823
- logicFn3.mockResolvedValue({ status: "finished" });
824
- const logics = [
825
- {
826
- name: "Logic 1",
827
- actionTypes: ["create"],
828
- modifiedFields: ["field1"],
829
- entities: ["user"],
830
- logicFn: logicFn1,
831
- },
832
- {
833
- name: "Logic 2",
834
- actionTypes: "all",
835
- modifiedFields: ["field2"],
836
- entities: ["user"],
837
- logicFn: logicFn2,
838
- },
839
- {
840
- name: "Logic 3",
841
- actionTypes: "all",
842
- modifiedFields: ["field1"],
843
- entities: ["user"],
844
- logicFn: logicFn3,
845
- },
846
- ];
847
- (0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, logics);
848
- const runStatus = await indexUtils.runBusinessLogics(actionRef, action, distributeFn);
849
- expect(logicFn1).toHaveBeenCalledTimes(1);
850
- expect(logicFn1).toHaveBeenCalledWith(action, new Map(), undefined);
851
- expect(logicFn2).toHaveBeenCalledTimes(1);
852
- expect(logicFn2).toHaveBeenCalledWith(action, new Map(), undefined);
853
- expect(logicFn3).not.toHaveBeenCalled();
854
- expect(distributeFn).not.toHaveBeenCalled();
855
- expect(runStatus).toEqual("cancel-then-retry");
744
+ expect(runStatus).toEqual({
745
+ status: "no-matching-logics",
746
+ logicResults: [],
747
+ });
856
748
  });
857
749
  it("should pass data from previous logic to the next logic via shared map", async () => {
858
750
  const expectedSharedMap = new Map();
@@ -864,11 +756,11 @@ describe("runBusinessLogics", () => {
864
756
  "@id": "test-document-id",
865
757
  "title": "Test Document Title",
866
758
  });
867
- logicFn2.mockImplementation((action, sharedMap) => {
759
+ logicFn2.mockImplementation((txnGet, action, sharedMap) => {
868
760
  sharedMap.set("another-document-id", expectedSharedMap.get("another-document-id"));
869
761
  return { status: "finished" };
870
762
  });
871
- logicFn3.mockImplementation((action, sharedMap) => {
763
+ logicFn3.mockImplementation((txnGet, action, sharedMap) => {
872
764
  sharedMap.set("test-document-id", expectedSharedMap.get("test-document-id"));
873
765
  return { status: "finished" };
874
766
  });
@@ -896,361 +788,16 @@ describe("runBusinessLogics", () => {
896
788
  },
897
789
  ];
898
790
  (0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, logics);
899
- const runStatus = await indexUtils.runBusinessLogics(actionRef, action, distributeFn);
900
- expect(logicFn1).toHaveBeenCalledWith(action, expectedSharedMap, undefined);
901
- expect(logicFn2).toHaveBeenCalledWith(action, expectedSharedMap, undefined);
902
- expect(logicFn3).toHaveBeenCalledWith(action, expectedSharedMap, undefined);
903
- expect(runStatus).toEqual("done");
904
- });
905
- });
906
- describe("simulateSubmitForm", () => {
907
- const eventContext = {
908
- id: "test-event-id",
909
- uid: "test-user-id",
910
- docPath: "servers/test-doc-id",
911
- docId: "test-doc-id",
912
- formId: "test-form-id",
913
- entity: "servers",
914
- };
915
- const user = {
916
- "@id": "test-user-id",
917
- "username": "Topic Creator",
918
- "avatarUrl": "Avatar URL",
919
- "firstName": "Topic",
920
- "lastName": "Creator",
921
- };
922
- const action = {
923
- actionType: "create",
924
- eventContext: eventContext,
925
- user: user,
926
- document: {},
927
- status: "new",
928
- timeCreated: admin.firestore.Timestamp.now(),
929
- modifiedFields: {},
930
- };
931
- const distributeFn = jest.fn();
932
- let runBusinessLogicsSpy;
933
- let dataMock;
934
- let docMock;
935
- let now;
936
- beforeEach(() => {
937
- jest.spyOn(console, "debug").mockImplementation();
938
- jest.spyOn(console, "warn").mockImplementation();
939
- dataMock = jest.fn().mockReturnValue({});
940
- const getMock = {
941
- data: dataMock,
942
- };
943
- docMock = {
944
- set: jest.fn(),
945
- get: jest.fn().mockResolvedValue(getMock),
946
- collection: jest.fn(() => collectionMock),
947
- };
948
- const collectionMock = {
949
- doc: jest.fn(() => docMock),
950
- };
951
- jest.spyOn(admin.firestore(), "doc").mockReturnValue(docMock);
952
- jest.spyOn(admin.firestore(), "collection").mockReturnValue(collectionMock);
953
- now = Timestamp.now();
954
- jest.spyOn(indexUtils._mockable, "createNowTimestamp").mockReturnValue(now);
955
- runBusinessLogicsSpy =
956
- jest.spyOn(indexUtils, "runBusinessLogics").mockResolvedValue("done");
957
- });
958
- afterEach(() => {
959
- jest.restoreAllMocks();
960
- });
961
- it("should skip when there is no logic result doc with 'simulate-submit-form' action", async () => {
962
- await indexUtils._mockable.simulateSubmitForm([], action, distributeFn);
963
- expect(console.debug).toHaveBeenCalledTimes(1);
964
- expect(console.debug).toHaveBeenCalledWith("Simulating submit form: ", 0);
965
- expect(runBusinessLogicsSpy).not.toHaveBeenCalled();
966
- });
967
- it("should skip when entity is undefined", async () => {
968
- const logicResults = [];
969
- const logicResultDoc = {
970
- action: "simulate-submit-form",
971
- dstPath: "no-entity/sample-doc-id",
972
- doc: {
973
- "@id": "no-entity/sample-doc-id",
974
- },
975
- };
976
- const logicResult = {
977
- name: "sampleLogicResult",
978
- status: "finished",
979
- documents: [logicResultDoc],
980
- };
981
- logicResults.push(logicResult);
982
- await indexUtils._mockable.simulateSubmitForm(logicResults, action, distributeFn);
983
- expect(console.warn).toHaveBeenCalledTimes(1);
984
- expect(console.warn).toHaveBeenCalledWith("No matching entity found for logic no-entity/sample-doc-id. Skipping");
985
- expect(runBusinessLogicsSpy).not.toHaveBeenCalled();
986
- });
987
- it("should skip when logic result doc is undefined", async () => {
988
- const logicResults = [];
989
- const logicResultDoc = {
990
- action: "simulate-submit-form",
991
- dstPath: "servers/sample-server-id",
992
- };
993
- const logicResult = {
994
- name: "sampleLogicResult",
995
- status: "finished",
996
- documents: [logicResultDoc],
997
- };
998
- logicResults.push(logicResult);
999
- await indexUtils._mockable.simulateSubmitForm(logicResults, action, distributeFn);
1000
- expect(console.warn).toHaveBeenCalledTimes(1);
1001
- expect(console.warn).toHaveBeenCalledWith("LogicResultDoc.doc should not be undefined. Skipping");
1002
- expect(runBusinessLogicsSpy).not.toHaveBeenCalled();
1003
- });
1004
- it("should skip when @actionType is undefined", async () => {
1005
- const logicResults = [];
1006
- const logicResultDoc = {
1007
- action: "simulate-submit-form",
1008
- dstPath: "servers/sample-server-id",
1009
- doc: { "@id": "sample-server-id" },
1010
- };
1011
- const logicResult = {
1012
- name: "sampleLogicResult",
1013
- status: "finished",
1014
- documents: [logicResultDoc],
1015
- };
1016
- logicResults.push(logicResult);
1017
- await indexUtils._mockable.simulateSubmitForm(logicResults, action, distributeFn);
1018
- expect(console.warn).toHaveBeenCalledTimes(1);
1019
- expect(console.warn).toHaveBeenCalledWith("No @actionType found. Skipping");
1020
- expect(runBusinessLogicsSpy).not.toHaveBeenCalled();
1021
- });
1022
- it("should skip when submitFormAs data is undefined", async () => {
1023
- dataMock.mockReturnValueOnce(undefined);
1024
- const logicResults = [];
1025
- const logicResultDoc = {
1026
- action: "simulate-submit-form",
1027
- dstPath: "servers/sample-server-id",
1028
- doc: {
1029
- "@actionType": "create",
1030
- "@submitFormAs": "test-user-id",
1031
- },
1032
- };
1033
- const logicResult = {
1034
- name: "sampleLogicResult",
1035
- status: "finished",
1036
- documents: [logicResultDoc],
1037
- };
1038
- logicResults.push(logicResult);
1039
- await indexUtils._mockable.simulateSubmitForm(logicResults, action, distributeFn);
1040
- expect(console.warn).toHaveBeenCalledTimes(1);
1041
- expect(console.warn).toHaveBeenCalledWith("User test-user-id not found. Skipping");
1042
- expect(runBusinessLogicsSpy).not.toHaveBeenCalled();
1043
- });
1044
- it("should simulate submit form correctly when submitFormAs is defined", async () => {
1045
- dataMock.mockReturnValueOnce({ "@id": "test-user-id" });
1046
- const logicResults = [];
1047
- const logicResultDoc = {
1048
- action: "simulate-submit-form",
1049
- dstPath: "servers/sample-server-id",
1050
- doc: {
1051
- "@actionType": "create",
1052
- "@submitFormAs": "test-user-id",
1053
- "name": "sample-server-name",
1054
- "createdAt": now,
1055
- },
1056
- };
1057
- const logicResult = {
1058
- name: "sampleLogicResult",
1059
- status: "finished",
1060
- documents: [logicResultDoc],
1061
- };
1062
- logicResults.push(logicResult);
1063
- const expectedEventContext = {
1064
- id: action.eventContext.id + "-1",
1065
- uid: action.eventContext.uid,
1066
- formId: action.eventContext.formId + "-1",
1067
- docId: "sample-server-id",
1068
- docPath: logicResultDoc.dstPath,
1069
- entity: "server",
1070
- };
1071
- const expectedAction = {
1072
- eventContext: expectedEventContext,
1073
- actionType: "create",
1074
- document: {},
1075
- modifiedFields: {
1076
- "name": "sample-server-name",
1077
- "createdAt": now,
1078
- },
1079
- user: { "@id": "test-user-id" },
1080
- status: "new",
1081
- timeCreated: now,
1082
- };
1083
- await indexUtils._mockable.simulateSubmitForm(logicResults, action, distributeFn);
1084
- expect(docMock.set).toHaveBeenCalledTimes(1);
1085
- expect(docMock.set).toHaveBeenCalledWith(expectedAction);
1086
- expect(runBusinessLogicsSpy).toHaveBeenCalledTimes(1);
1087
- expect(runBusinessLogicsSpy).toHaveBeenCalledWith(docMock, expectedAction, distributeFn);
1088
- });
1089
- it("should simulate submit form correctly when submitFormAs is undefined", async () => {
1090
- const logicResults = [];
1091
- const logicResultDoc = {
1092
- action: "simulate-submit-form",
1093
- dstPath: "servers/sample-server-id",
1094
- doc: {
1095
- "@actionType": "create",
1096
- "name": "sample-server-name",
1097
- "createdAt": now,
1098
- },
1099
- };
1100
- const logicResult = {
1101
- name: "sampleLogicResult",
1102
- status: "finished",
1103
- documents: [logicResultDoc],
1104
- };
1105
- logicResults.push(logicResult);
1106
- const expectedEventContext = {
1107
- id: action.eventContext.id + "-1",
1108
- uid: action.eventContext.uid,
1109
- formId: action.eventContext.formId + "-1",
1110
- docId: "sample-server-id",
1111
- docPath: logicResultDoc.dstPath,
1112
- entity: "server",
1113
- };
1114
- const expectedAction = {
1115
- eventContext: expectedEventContext,
1116
- actionType: "create",
1117
- document: {},
1118
- modifiedFields: {
1119
- "name": "sample-server-name",
1120
- "createdAt": now,
1121
- },
1122
- user: user,
1123
- status: "new",
1124
- timeCreated: now,
1125
- };
1126
- await indexUtils._mockable.simulateSubmitForm(logicResults, action, distributeFn);
1127
- expect(docMock.set).toHaveBeenCalledTimes(1);
1128
- expect(docMock.set).toHaveBeenCalledWith(expectedAction);
1129
- expect(runBusinessLogicsSpy).toHaveBeenCalledTimes(1);
1130
- expect(runBusinessLogicsSpy).toHaveBeenCalledWith(docMock, expectedAction, distributeFn);
1131
- });
1132
- it("should simulate submit form correctly with multiple logic result docs", async () => {
1133
- const logicResults = [];
1134
- const logicResultDoc1 = {
1135
- action: "simulate-submit-form",
1136
- dstPath: "servers/sample-server-id",
1137
- doc: {
1138
- "@actionType": "create",
1139
- "name": "sample-server-name",
1140
- "createdAt": now,
1141
- },
1142
- };
1143
- const logicResultDoc2 = {
1144
- action: "merge",
1145
- dstPath: "servers/merge-server-id",
1146
- doc: {
1147
- "@actionType": "update",
1148
- "name": "sample-server-name",
1149
- },
1150
- };
1151
- const logicResultDoc3 = {
1152
- action: "simulate-submit-form",
1153
- dstPath: "users/sample-user-id",
1154
- doc: {
1155
- "@actionType": "create",
1156
- "name": "sample-user-name",
1157
- },
1158
- };
1159
- const logicResult1 = {
1160
- name: "serverLogicResult",
1161
- status: "finished",
1162
- documents: [logicResultDoc1, logicResultDoc2],
1163
- };
1164
- const logicResult2 = {
1165
- name: "userLogicResult",
1166
- status: "finished",
1167
- documents: [logicResultDoc3],
1168
- };
1169
- logicResults.push(logicResult1);
1170
- logicResults.push(logicResult2);
1171
- const expectedServerEventContext = {
1172
- id: action.eventContext.id + "-1",
1173
- uid: action.eventContext.uid,
1174
- formId: action.eventContext.formId + "-1",
1175
- docId: "sample-server-id",
1176
- docPath: logicResultDoc1.dstPath,
1177
- entity: "server",
1178
- };
1179
- const expectedServerAction = {
1180
- eventContext: expectedServerEventContext,
1181
- actionType: "create",
1182
- document: {},
1183
- modifiedFields: {
1184
- "name": "sample-server-name",
1185
- "createdAt": now,
1186
- },
1187
- user: user,
1188
- status: "new",
1189
- timeCreated: now,
1190
- };
1191
- const expectedUserEventContext = {
1192
- id: action.eventContext.id + "-2",
1193
- uid: action.eventContext.uid,
1194
- formId: action.eventContext.formId + "-2",
1195
- docId: "sample-user-id",
1196
- docPath: logicResultDoc3.dstPath,
1197
- entity: "user",
1198
- };
1199
- const expectedUserAction = {
1200
- eventContext: expectedUserEventContext,
1201
- actionType: "create",
1202
- document: {},
1203
- modifiedFields: {
1204
- "name": "sample-user-name",
1205
- },
1206
- user: user,
1207
- status: "new",
1208
- timeCreated: now,
1209
- };
1210
- await indexUtils._mockable.simulateSubmitForm(logicResults, action, distributeFn);
1211
- expect(docMock.set).toHaveBeenCalledTimes(2);
1212
- expect(docMock.set).toHaveBeenNthCalledWith(1, expectedServerAction);
1213
- expect(docMock.set).toHaveBeenNthCalledWith(2, expectedUserAction);
1214
- expect(runBusinessLogicsSpy).toHaveBeenCalledTimes(2);
1215
- expect(runBusinessLogicsSpy).toHaveBeenNthCalledWith(1, docMock, expectedServerAction, distributeFn);
1216
- expect(runBusinessLogicsSpy).toHaveBeenNthCalledWith(2, docMock, expectedUserAction, distributeFn);
1217
- });
1218
- it("should skip when maximum retry count is reached", async () => {
1219
- runBusinessLogicsSpy.mockResolvedValueOnce("cancel-then-retry")
1220
- .mockResolvedValueOnce("cancel-then-retry")
1221
- .mockResolvedValueOnce("cancel-then-retry")
1222
- .mockResolvedValueOnce("cancel-then-retry")
1223
- .mockResolvedValueOnce("cancel-then-retry")
1224
- .mockResolvedValueOnce("cancel-then-retry");
1225
- const logicResults = [];
1226
- const doc = {
1227
- "@actionType": "create",
1228
- };
1229
- const logicResultDoc = {
1230
- action: "simulate-submit-form",
1231
- dstPath: "servers/sample-server-id",
1232
- doc: doc,
1233
- };
1234
- const logicResult = {
1235
- name: "sampleLogicResult",
1236
- status: "finished",
1237
- documents: [logicResultDoc],
1238
- };
1239
- logicResults.push(logicResult);
1240
- const dateSpy = jest.spyOn(Date, "now");
1241
- dateSpy.mockReturnValueOnce(now.toMillis())
1242
- .mockReturnValueOnce(now.toMillis() + 2000)
1243
- .mockReturnValueOnce(now.toMillis())
1244
- .mockReturnValueOnce(now.toMillis() + 4000)
1245
- .mockReturnValueOnce(now.toMillis())
1246
- .mockReturnValueOnce(now.toMillis() + 8000)
1247
- .mockReturnValueOnce(now.toMillis())
1248
- .mockReturnValueOnce(now.toMillis() + 16000)
1249
- .mockReturnValueOnce(now.toMillis())
1250
- .mockReturnValueOnce(now.toMillis() + 32000);
1251
- await indexUtils._mockable.simulateSubmitForm(logicResults, action, distributeFn);
1252
- expect(console.warn).toHaveBeenCalledTimes(1);
1253
- expect(console.warn).toHaveBeenCalledWith("Maximum retry count reached for logic servers/sample-server-id");
791
+ const runStatus = await indexUtils.runBusinessLogics(txnGet, action);
792
+ expect(logicFn1).toHaveBeenCalledWith(txnGet, action, expectedSharedMap);
793
+ expect(logicFn2).toHaveBeenCalledWith(txnGet, action, expectedSharedMap);
794
+ expect(logicFn3).toHaveBeenCalledWith(txnGet, action, expectedSharedMap);
795
+ expect(runStatus.status).toEqual("done");
796
+ const logicResults = runStatus.logicResults;
797
+ expect(logicResults.length).toEqual(3);
798
+ expect(logicResults[0].status).toEqual("finished");
799
+ expect(logicResults[1].status).toEqual("finished");
800
+ expect(logicResults[2].status).toEqual("finished");
1254
801
  });
1255
802
  });
1256
803
  describe("groupDocsByTargetDocPath", () => {