Skip to content

Commit

Permalink
Merge pull request #45 from intuit/timeout
Browse files Browse the repository at this point in the history
Api call duration customizable now
  • Loading branch information
vamshisuram committed Apr 28, 2024
2 parents b01ca6a + 2a7d7a2 commit 065085b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/mocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ export class MockLink extends ApolloLink {
super();
const dataStringFromLocalStorage = localStorage.getItem("AMH_mockdata");
const mockDataFromLocalStorage = JSON.parse(dataStringFromLocalStorage);
const { enableMock, mockData, targetOperations } = mockLinkConfig;
const { enableMock, mockData, targetOperations, defaultAPICallDuration } =
mockLinkConfig;
this.config = {
enableMock,
mockData: mockData ? mockData : mockDataFromLocalStorage,
targetOperations,
defaultAPICallDuration,
};
}

request(operation, forward) {
const { enableMock, targetOperations } = this.config;
const { enableMock, targetOperations, defaultAPICallDuration } =
this.config;
const isOperationMocked = targetOperations.includes(
operation.operationName
);
Expand All @@ -25,7 +28,7 @@ export class MockLink extends ApolloLink {
const relatedMockdata = this.config.mockData[operation.operationName];
observer.next(relatedMockdata);
observer.complete();
}, 2000);
}, defaultAPICallDuration);
});
}
return forward(operation);
Expand All @@ -45,11 +48,13 @@ export const injectMock = ({
targetOperations,
mockData,
createCustomLinkObj,
defaultAPICallDuration = 2000,
}) => {
const mockLink = new MockLink({
enableMock,
mockData,
targetOperations,
defaultAPICallDuration,
});

const mockLinkObj = createCustomLinkObj
Expand Down
9 changes: 8 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("MockLink Overall", () => {
},
},
createCustomLinkObj: null,
defaultAPICallDuration: 2000,
});
expect(links.length).toBe(2);
expect(links[0].constructor).toBe(mocker.MockLink);
Expand All @@ -49,6 +50,7 @@ describe("MockLink Overall", () => {
},
},
createCustomLinkObj: null,
defaultAPICallDuration: 2000,
});
expect(links.length).toBe(2);
expect(links[0].constructor).toBe(mocker.MockLink);
Expand All @@ -75,6 +77,7 @@ describe("MockLink Overall", () => {
linkName: "customMockLink",
linkObj: mockLink,
}),
defaultAPICallDuration: 2000,
});
expect(links.length).toBe(2);
expect(links[0].linkName).toBe("customMockLink");
Expand All @@ -97,6 +100,7 @@ describe("MockLink Overall", () => {
},
},
createCustomLinkObj: null,
defaultAPICallDuration: 2000,
};
mocker.injectMock(mockDataConfig);
expect(links.length).toBe(2);
Expand Down Expand Up @@ -135,6 +139,7 @@ describe("MockLink Overall", () => {
},
},
createCustomLinkObj: null,
defaultAPICallDuration: 2000,
});
expect(links.length).toBe(2);
expect(links[0].constructor).toBe(mocker.MockLink);
Expand All @@ -159,6 +164,7 @@ describe("MockLink Overall", () => {
},
},
createCustomLinkObj: null,
defaultAPICallDuration: 2000,
});
expect(links.length).toBe(2);
expect(links[0].constructor).toBe(mocker.MockLink);
Expand Down Expand Up @@ -188,6 +194,7 @@ describe("MockLink Overall", () => {
enableMock: true,
targetOperations: ["getCompanyName"],
createCustomLinkObj: null,
defaultAPICallDuration: 3000,
};
mocker.injectMock(mockDataConfig);
expect(links.length).toBe(2);
Expand All @@ -208,7 +215,7 @@ describe("MockLink Overall", () => {
expect(forwardSpy).not.toHaveBeenCalledWith(op);

expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 2000);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 3000);
expect(localStorageSpy).toHaveBeenCalledWith("AMH_mockdata");
});
});

0 comments on commit 065085b

Please sign in to comment.