Skip to content

Commit 978dd10

Browse files
committed
chore(property-provider): update unit test
1 parent 4d51fe1 commit 978dd10

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

packages/property-provider/src/memoize.spec.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,26 @@ describe("memoize", () => {
130130
});
131131

132132
describe("when called with forceRefresh set to `true`", () => {
133-
const isExpiredFalseTest = async (requiresRefresh?: any) => {
133+
it("should reinvoke the underlying provider even if isExpired returns false", async () => {
134134
const memoized = memoize(provider, isExpired, requiresRefresh);
135-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
136-
for (const index in [...Array(repeatTimes).keys()]) {
137-
expect(await memoized()).toEqual(mockReturn);
138-
}
139-
140-
expect(isExpired).toHaveBeenCalledTimes(repeatTimes);
141-
if (requiresRefresh) {
142-
expect(requiresRefresh).toHaveBeenCalledTimes(repeatTimes);
143-
}
144-
expect(provider).toHaveBeenCalledTimes(repeatTimes + 1);
145-
};
146-
147-
it("should reinvoke the underlying provider even if isExpired returns false", () => {
148135
isExpired.mockReturnValue(false);
136+
for (const _ in [...Array(repeatTimes).keys()]) {
137+
expect(await memoized({ forceRefresh: true })).toEqual(mockReturn);
138+
}
139+
expect(provider).toHaveBeenCalledTimes(repeatTimes);
149140
});
150141

151-
it("when requiresRefresh returns true", () => {
152-
requiresRefresh.mockReturnValue(true);
153-
return isExpiredTrueTest(requiresRefresh);
142+
it("should reinvoke the underlying provider even if requiresRefresh returns false", async () => {
143+
const memoized = memoize(provider, isExpired, requiresRefresh);
144+
requiresRefresh.mockReturnValue(false);
145+
for (const _ in [...Array(repeatTimes).keys()]) {
146+
expect(await memoized({ forceRefresh: true })).toEqual(mockReturn);
147+
}
148+
expect(provider).toHaveBeenCalledTimes(repeatTimes);
154149
});
155150
});
156151

157-
describe("should return the same promise for invocations 2-infinity if `requiresRefresh` returns `false`", () => {
152+
describe("when `requiresRefresh` returns `false`", () => {
158153
const requiresRefreshFalseTest = async () => {
159154
const memoized = memoize(provider, isExpired, requiresRefresh);
160155
const result = memoized();
@@ -169,14 +164,22 @@ describe("memoize", () => {
169164
expect(isExpired).not.toHaveBeenCalled();
170165
};
171166

172-
it("when isExpired returns true", () => {
167+
it("should return the same promise for invocations 2-infinity if isExpired returns true", () => {
173168
return requiresRefreshFalseTest();
174169
});
175170

176-
it("when isExpired returns false", () => {
171+
it("should return the same promise for invocations 2-infinity if isExpired returns false", () => {
177172
isExpired.mockReturnValue(false);
178173
return requiresRefreshFalseTest();
179174
});
175+
176+
it("should re-evaluate `requiresRefresh` after force refresh", async () => {
177+
const memoized = memoize(provider, isExpired, requiresRefresh);
178+
for (const _ in [...Array(repeatTimes).keys()]) {
179+
expect(await memoized({ forceRefresh: true })).toStrictEqual(mockReturn);
180+
}
181+
expect(requiresRefresh).toBeCalledTimes(repeatTimes);
182+
});
180183
});
181184

182185
describe("should not make extra request for concurrent calls", () => {

packages/property-provider/src/memoize.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const memoize: MemoizeOverload = <T>(
5757
try {
5858
resolved = await pending;
5959
hasResult = true;
60-
isConstant = undefined;
60+
isConstant = false;
6161
} finally {
6262
pending = undefined;
6363
}
@@ -66,15 +66,15 @@ export const memoize: MemoizeOverload = <T>(
6666

6767
if (isExpired === undefined) {
6868
// This is a static memoization; no need to incorporate refreshing unless using forceRefresh;
69-
return async (options?: { forceRefresh?: boolean }) => {
69+
return async (options) => {
7070
if (!hasResult || options?.forceRefresh) {
7171
resolved = await coalesceProvider();
7272
}
7373
return resolved;
7474
};
7575
}
7676

77-
return async (options?: { forceRefresh?: boolean }) => {
77+
return async (options) => {
7878
if (!hasResult || options?.forceRefresh) {
7979
resolved = await coalesceProvider();
8080
}

0 commit comments

Comments
 (0)