Skip to content

Commit

Permalink
fix: Fix route change timing fetch counter decrement (#145)
Browse files Browse the repository at this point in the history
* fix: πŸ› fix issue that decrement fetch counter was called always

Basically the finally call of fetch was not only being called on
finally.

* test: πŸ’ add test for fetchCounter update
  • Loading branch information
maniator committed May 6, 2022
1 parent 59947fc commit c4414d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/sessions/VirtualPageLoadTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,12 @@ export class VirtualPageLoadTimer extends MonkeyPatched {
input: RequestInfo,
init?: RequestInit
): Promise<Response> => {
const self = this;
return original
.apply(thisArg, argsArray)
.catch((error) => {
throw error;
})
.finally(self.decrementFetchCounter());
.finally(this.decrementFetchCounter);
};

/**
Expand All @@ -190,12 +189,12 @@ export class VirtualPageLoadTimer extends MonkeyPatched {
};
};

private decrementFetchCounter() {
private decrementFetchCounter = () => {
if (!this.isPageLoaded) {
this.latestEndTime = Date.now();
}
this.fetchCounter -= 1;
}
};

/**
* Checks whether the virtual page is still being loaded.
Expand Down
22 changes: 22 additions & 0 deletions src/sessions/__tests__/VirtualPageLoadTimer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,28 @@ describe('VirtualPageLoadTimer tests', () => {
expect(virtualPageLoadTimer['ongoingRequests'].size).toEqual(0);
});

test('when fetch is fetch counter should be updated to 1 until finished', async () => {
// Init
const virtualPageLoadTimer = new VirtualPageLoadTimer(
pageManager,
config,
record
);

// Mocking Date.now to return 100 to simulate time passed
Date.now = jest.fn(() => 100);
virtualPageLoadTimer.startTiming();

// When fetch initially is sent, fetchCounter should be incremented to 1
const fetching = fetch('https://aws.amazon.com');
expect(virtualPageLoadTimer['fetchCounter']).toEqual(1);

await fetching;

// Upon completion, fetchCounter should be decremented to 0
expect(virtualPageLoadTimer['fetchCounter']).toEqual(0);
});

test('when fetch is detected during route change then latestEndTime is updated', async () => {
// Init
const virtualPageLoadTimer = new VirtualPageLoadTimer(
Expand Down

0 comments on commit c4414d9

Please sign in to comment.