Skip to content

Commit

Permalink
fix: done only gets called one in each (#861)
Browse files Browse the repository at this point in the history
* [DI-2414] test: add test case to ensure done only gets called once

* [DI-2414] fix: only call done once in each

* [DI-2414] test: fix test cases

* [DI-2414] nit: update logic for version

* [DI-2414] nit: remove use strict from test cases

* [DI-2414] chore: make code pretty
  • Loading branch information
mattcole19 authored Jan 4, 2023
1 parent b65a0af commit e8330cb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
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);
});
});

0 comments on commit e8330cb

Please sign in to comment.