Skip to content

Commit

Permalink
fix(helpers): Update cachedPromises tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed Oct 14, 2022
1 parent 2b90efb commit 335409e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/utils/helpers/src/cachedPromises.test.js
Expand Up @@ -18,8 +18,9 @@ import createCachedPromises from './cachedPromises.js';
import wait from './wait.js';

test('cachedPromises calls getter with key', async () => {
const cache = new Map();
const getter = jest.fn(() => Promise.resolve('value'));
const cachedGetter = createCachedPromises(getter);
const cachedGetter = createCachedPromises({ cache, getter });
const promise = cachedGetter('key');
expect(`${promise}`).toEqual('[object Promise]');
const result = await promise;
Expand All @@ -28,8 +29,10 @@ test('cachedPromises calls getter with key', async () => {
});

test('cachedPromises only calls getter once', async () => {
const cache = new Map();

const getter = jest.fn(() => Promise.resolve('value'));
const cachedGetter = createCachedPromises(getter);
const cachedGetter = createCachedPromises({ cache, getter });
const result1 = await cachedGetter('key');
expect(result1).toEqual('value');
const result2 = await cachedGetter('key');
Expand All @@ -38,11 +41,13 @@ test('cachedPromises only calls getter once', async () => {
});

test('getter is called once if first call has not yet resolved', async () => {
const cache = new Map();

const getter = jest.fn(async () => {
await wait(10);
return 'value';
});
const cachedGetter = createCachedPromises(getter);
const cachedGetter = createCachedPromises({ cache, getter });
const promise1 = cachedGetter('key');
const promise2 = cachedGetter('key');
expect(getter.mock.calls).toEqual([['key']]);
Expand Down

0 comments on commit 335409e

Please sign in to comment.