Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: done only gets called one in each #861

Merged
merged 10 commits into from
Jan 4, 2023
4 changes: 3 additions & 1 deletion lib/base/Version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export default class Version {
throw new Error("Callback function must be provided");
}
let done = false;
let doneCalled = false;
let currentPage = 1;
let currentResource = 0;
let limits = {} as PageLimit;
Expand All @@ -253,9 +254,10 @@ export default class Version {
}
function onComplete(error?) {
done = true;
if (typeof params.done === "function") {
if (typeof params.done === "function" && !doneCalled) {
params.done(error);
}
doneCalled = true;
}
function fetchNextPage(fn) {
let promise = fn();
Expand Down
64 changes: 64 additions & 0 deletions spec/unit/base/Version.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,67 @@ describe("streaming results", function () {
done();
});
});

describe("done should only be called once in each", () => {
let holodeck;
let client;
let body;
beforeEach(function () {
holodeck = new Holodeck();
client = new Twilio("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "AUTHTOKEN", {
httpClient: holodeck,
});
body = {
end: 0,
first_page_uri:
"/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/IncomingPhoneNumbers.json?FriendlyName=friendly_name&Beta=true&PhoneNumber=%2B19876543210&PageSize=1&Page=0",
incoming_phone_numbers: [{}],
next_page_uri:
"/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/IncomingPhoneNumbers.json?FriendlyName=friendly_name&Beta=true&PhoneNumber=%2B19876543210&PageSize=1&Page=1",
page: 0,
page_size: 1,
previous_page_uri: null,
start: 0,
uri: "/2010-04-01/Accounts/ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/IncomingPhoneNumbers.json?FriendlyName=friendly_name&Beta=true&PhoneNumber=%2B19876543210&PageSize=1&Page=0",
};
});
it("done is explicitly called", async () => {
holodeck.mock(new Response(200, body));
holodeck.mock(new Response(200, body));
const mockDone = jest.fn(console.debug.bind(null, "done!"));
client.api.v2010
.accounts("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.incomingPhoneNumbers.each({
limit: 1,
callback: (account, done) => {
done();
},
done: mockDone,
});

// Sleep to allow async work to complete
await new Promise((r) => setTimeout(r, 2000));

expect(mockDone.mock.calls.length).toBe(1);
});

it("done is not explicitly called", async () => {
holodeck.mock(new Response(200, body));
holodeck.mock(new Response(200, body));
const mockDone = jest.fn(console.debug.bind(null, "done!"));
client.api.v2010
.accounts("ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
.incomingPhoneNumbers.each({
limit: 1,
callback: (account, done) => {
console.log();
},
done: mockDone,
});

// Sleep to allow async work to complete
await new Promise((r) => setTimeout(r, 2000));

expect(mockDone.mock.calls.length).toBe(1);
});
});