emberflow 1.4.3 → 1.4.4
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.
- package/lib/index-utils.d.ts +3 -6
- package/lib/index-utils.js +17 -160
- package/lib/index-utils.js.map +1 -1
- package/lib/index.js +311 -297
- package/lib/index.js.map +1 -1
- package/lib/logics/view-logics.js +1 -1
- package/lib/logics/view-logics.js.map +1 -1
- package/lib/sample-custom/business-logics.js +1 -1
- package/lib/sample-custom/business-logics.js.map +1 -1
- package/lib/tests/index-utils.test.js +32 -487
- package/lib/tests/index-utils.test.js.map +1 -1
- package/lib/tests/index.test.js +90 -80
- package/lib/tests/index.test.js.map +1 -1
- package/lib/tests/logics/view-logics.test.js +1 -1
- package/lib/tests/logics/view-logics.test.js.map +1 -1
- package/lib/tests/utils/distribution.test.js +13 -13
- package/lib/tests/utils/distribution.test.js.map +1 -1
- package/lib/tests/utils/forms.test.js +0 -23
- package/lib/tests/utils/forms.test.js.map +1 -1
- package/lib/types.d.ts +13 -3
- package/lib/utils/distribution.d.ts +2 -1
- package/lib/utils/distribution.js +53 -53
- package/lib/utils/distribution.js.map +1 -1
- package/lib/utils/forms.js +0 -4
- package/lib/utils/forms.js.map +1 -1
- package/package.json +1 -1
- package/src/sample-custom/business-logics.ts +1 -1
|
@@ -57,6 +57,7 @@ const projectConfig = {
|
|
|
57
57
|
admin.initializeApp({
|
|
58
58
|
databaseURL: "https://test-project.firebaseio.com",
|
|
59
59
|
});
|
|
60
|
+
const txnGet = jest.fn();
|
|
60
61
|
jest.mock("../utils/paths", () => {
|
|
61
62
|
const originalModule = jest.requireActual("../utils/paths");
|
|
62
63
|
return Object.assign(Object.assign({}, originalModule), { expandAndGroupDocPathsByEntity: jest.fn() });
|
|
@@ -415,7 +416,7 @@ describe("distribute", () => {
|
|
|
415
416
|
}],
|
|
416
417
|
]]);
|
|
417
418
|
(0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, []);
|
|
418
|
-
await indexUtils.
|
|
419
|
+
await indexUtils.distributeFnNonTransactional(userDocsByDstPath);
|
|
419
420
|
expect(admin.firestore().doc).toHaveBeenCalledTimes(1);
|
|
420
421
|
expect(admin.firestore().doc).toHaveBeenCalledWith("/users/test-user-id/documents/test-doc-id");
|
|
421
422
|
expect(queueInstructionsSpy).toHaveBeenCalledTimes(1);
|
|
@@ -442,7 +443,7 @@ describe("distribute", () => {
|
|
|
442
443
|
dstPath: "/users/test-user-id/documents/test-doc-id",
|
|
443
444
|
}],
|
|
444
445
|
]]);
|
|
445
|
-
await indexUtils.
|
|
446
|
+
await indexUtils.distributeFnNonTransactional(userDocsByDstPath);
|
|
446
447
|
expect(admin.firestore().doc).toHaveBeenCalledTimes(1);
|
|
447
448
|
expect(admin.firestore().doc).toHaveBeenCalledWith("/users/test-user-id/documents/test-doc-id");
|
|
448
449
|
expect(dbDoc.get).toHaveBeenCalledTimes(1);
|
|
@@ -483,6 +484,9 @@ describe("distributeLater", () => {
|
|
|
483
484
|
});
|
|
484
485
|
});
|
|
485
486
|
describe("validateForm", () => {
|
|
487
|
+
beforeEach(() => {
|
|
488
|
+
(0, index_1.initializeEmberFlow)(projectConfig, admin, db_structure_1.dbStructure, db_structure_1.Entity, security_1.securityConfig, validators_1.validatorConfig, []);
|
|
489
|
+
});
|
|
486
490
|
it("returns an object with empty validationResult when document is valid", async () => {
|
|
487
491
|
const entity = "user";
|
|
488
492
|
const document = {
|
|
@@ -593,8 +597,6 @@ describe("runBusinessLogics", () => {
|
|
|
593
597
|
let logicFn5;
|
|
594
598
|
let logicFn6;
|
|
595
599
|
let dbSpy;
|
|
596
|
-
let simulateSubmitFormSpy;
|
|
597
|
-
let createMetricExecutionSpy;
|
|
598
600
|
let actionRef;
|
|
599
601
|
beforeEach(() => {
|
|
600
602
|
distributeFn = jest.fn();
|
|
@@ -604,8 +606,6 @@ describe("runBusinessLogics", () => {
|
|
|
604
606
|
logicFn4 = jest.fn().mockResolvedValue({ status: "finished" });
|
|
605
607
|
logicFn5 = jest.fn().mockResolvedValue({ status: "finished" });
|
|
606
608
|
logicFn6 = jest.fn().mockResolvedValue({ status: "finished" });
|
|
607
|
-
simulateSubmitFormSpy = jest.spyOn(indexUtils._mockable, "simulateSubmitForm").mockResolvedValue();
|
|
608
|
-
createMetricExecutionSpy = jest.spyOn(indexUtils._mockable, "createMetricExecution").mockResolvedValue();
|
|
609
609
|
const dbDoc = {
|
|
610
610
|
get: jest.fn().mockResolvedValue({
|
|
611
611
|
data: jest.fn().mockReturnValue({
|
|
@@ -633,8 +633,6 @@ describe("runBusinessLogics", () => {
|
|
|
633
633
|
logicFn3.mockRestore();
|
|
634
634
|
distributeFn.mockRestore();
|
|
635
635
|
dbSpy.mockRestore();
|
|
636
|
-
simulateSubmitFormSpy.mockRestore();
|
|
637
|
-
createMetricExecutionSpy.mockRestore();
|
|
638
636
|
});
|
|
639
637
|
it("should call all matching logics and pass their results to distributeFn", async () => {
|
|
640
638
|
const logics = [
|
|
@@ -691,71 +689,25 @@ describe("runBusinessLogics", () => {
|
|
|
691
689
|
},
|
|
692
690
|
];
|
|
693
691
|
(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(
|
|
695
|
-
expect(logicFn1).toHaveBeenCalledWith(action, new Map()
|
|
696
|
-
expect(logicFn2).toHaveBeenCalledWith(action, new Map()
|
|
692
|
+
const runStatus = await indexUtils.runBusinessLogics(txnGet, action);
|
|
693
|
+
expect(logicFn1).toHaveBeenCalledWith(txnGet, action, new Map());
|
|
694
|
+
expect(logicFn2).toHaveBeenCalledWith(txnGet, action, new Map());
|
|
697
695
|
expect(logicFn3).not.toHaveBeenCalled();
|
|
698
696
|
expect(logicFn4).not.toHaveBeenCalled();
|
|
699
697
|
expect(logicFn5).not.toHaveBeenCalled();
|
|
700
698
|
expect(logicFn6).not.toHaveBeenCalled();
|
|
701
|
-
expect(
|
|
702
|
-
|
|
703
|
-
|
|
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({
|
|
699
|
+
expect(runStatus).toEqual({
|
|
700
|
+
status: "done",
|
|
701
|
+
logicResults: [{
|
|
741
702
|
status: "finished",
|
|
742
703
|
execTime: expect.any(Number),
|
|
743
704
|
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({
|
|
705
|
+
}, {
|
|
754
706
|
status: "finished",
|
|
755
707
|
execTime: expect.any(Number),
|
|
756
708
|
timeFinished: expect.any(Timestamp),
|
|
757
|
-
}
|
|
758
|
-
|
|
709
|
+
}],
|
|
710
|
+
});
|
|
759
711
|
});
|
|
760
712
|
it("should not call any logic when no matching logics are found but distributeFn should still be " +
|
|
761
713
|
"called", async () => {
|
|
@@ -783,76 +735,14 @@ describe("runBusinessLogics", () => {
|
|
|
783
735
|
},
|
|
784
736
|
];
|
|
785
737
|
(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(
|
|
738
|
+
const runStatus = await indexUtils.runBusinessLogics(txnGet, action);
|
|
787
739
|
expect(logicFn1).not.toHaveBeenCalled();
|
|
788
740
|
expect(logicFn2).not.toHaveBeenCalled();
|
|
789
741
|
expect(logicFn3).not.toHaveBeenCalled();
|
|
790
|
-
expect(
|
|
791
|
-
|
|
792
|
-
|
|
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");
|
|
742
|
+
expect(runStatus).toEqual({
|
|
743
|
+
status: "no-matching-logics",
|
|
744
|
+
logicResults: [],
|
|
745
|
+
});
|
|
856
746
|
});
|
|
857
747
|
it("should pass data from previous logic to the next logic via shared map", async () => {
|
|
858
748
|
const expectedSharedMap = new Map();
|
|
@@ -864,11 +754,11 @@ describe("runBusinessLogics", () => {
|
|
|
864
754
|
"@id": "test-document-id",
|
|
865
755
|
"title": "Test Document Title",
|
|
866
756
|
});
|
|
867
|
-
logicFn2.mockImplementation((action, sharedMap) => {
|
|
757
|
+
logicFn2.mockImplementation((txnGet, action, sharedMap) => {
|
|
868
758
|
sharedMap.set("another-document-id", expectedSharedMap.get("another-document-id"));
|
|
869
759
|
return { status: "finished" };
|
|
870
760
|
});
|
|
871
|
-
logicFn3.mockImplementation((action, sharedMap) => {
|
|
761
|
+
logicFn3.mockImplementation((txnGet, action, sharedMap) => {
|
|
872
762
|
sharedMap.set("test-document-id", expectedSharedMap.get("test-document-id"));
|
|
873
763
|
return { status: "finished" };
|
|
874
764
|
});
|
|
@@ -896,361 +786,16 @@ describe("runBusinessLogics", () => {
|
|
|
896
786
|
},
|
|
897
787
|
];
|
|
898
788
|
(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(
|
|
900
|
-
expect(logicFn1).toHaveBeenCalledWith(action, expectedSharedMap
|
|
901
|
-
expect(logicFn2).toHaveBeenCalledWith(action, expectedSharedMap
|
|
902
|
-
expect(logicFn3).toHaveBeenCalledWith(action, expectedSharedMap
|
|
903
|
-
expect(runStatus).toEqual("done");
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
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");
|
|
789
|
+
const runStatus = await indexUtils.runBusinessLogics(txnGet, action);
|
|
790
|
+
expect(logicFn1).toHaveBeenCalledWith(txnGet, action, expectedSharedMap);
|
|
791
|
+
expect(logicFn2).toHaveBeenCalledWith(txnGet, action, expectedSharedMap);
|
|
792
|
+
expect(logicFn3).toHaveBeenCalledWith(txnGet, action, expectedSharedMap);
|
|
793
|
+
expect(runStatus.status).toEqual("done");
|
|
794
|
+
const logicResults = runStatus.logicResults;
|
|
795
|
+
expect(logicResults.length).toEqual(3);
|
|
796
|
+
expect(logicResults[0].status).toEqual("finished");
|
|
797
|
+
expect(logicResults[1].status).toEqual("finished");
|
|
798
|
+
expect(logicResults[2].status).toEqual("finished");
|
|
1254
799
|
});
|
|
1255
800
|
});
|
|
1256
801
|
describe("groupDocsByTargetDocPath", () => {
|