-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@quilted/preact-async": patch | ||
"@quilted/async": patch | ||
--- | ||
|
||
More async API type fixes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import {describe, it, expect, vi} from 'vitest'; | ||
|
||
import {AsyncFetchCache} from '../AsyncFetchCache.ts'; | ||
|
||
describe('AsyncFetchCache', () => { | ||
describe('get()', () => { | ||
it('returns a wrapper around an async function', async () => { | ||
const cache = new AsyncFetchCache(); | ||
const greet = cache.get(async (name: string) => `Hello ${name}!`, { | ||
key: ['greet'], | ||
}); | ||
|
||
expect(greet).toHaveProperty('key', ['greet']); | ||
expect(greet).toHaveProperty('id', expect.any(String)); | ||
|
||
const result = await greet.call('Winston'); | ||
expect(result).toBe('Hello Winston!'); | ||
}); | ||
|
||
it('returns the same promise for a given key', async () => { | ||
const cache = new AsyncFetchCache(); | ||
const entry1 = cache.get(async () => '', {key: 'key'}); | ||
const entry2 = cache.get(async () => '', {key: 'key'}); | ||
|
||
expect(entry1).toBe(entry2); | ||
}); | ||
|
||
it('passes through an abort signal to the fetch function', async () => { | ||
const cache = new AsyncFetchCache(); | ||
const abortController = new AbortController(); | ||
const spy = vi.fn(); | ||
const reason = new Error('Abort'); | ||
|
||
const waitUntilAbort = cache.get(async (_, {signal}) => { | ||
signal.addEventListener('abort', () => { | ||
spy(signal.reason); | ||
}); | ||
}); | ||
|
||
const promise = waitUntilAbort.call(undefined, { | ||
signal: abortController.signal, | ||
}); | ||
|
||
abortController.abort(reason); | ||
|
||
await expect(promise).rejects.toThrow(reason); | ||
expect(spy).toHaveBeenCalledWith(reason); | ||
}); | ||
|
||
it('provides an abort() method to manually abort the fetch function', async () => { | ||
const cache = new AsyncFetchCache(); | ||
const spy = vi.fn(); | ||
const reason = new Error('Abort'); | ||
|
||
const waitUntilAbort = cache.get(async (_, {signal}) => { | ||
signal.addEventListener('abort', () => { | ||
spy(signal.reason); | ||
}); | ||
}); | ||
|
||
const promise = waitUntilAbort.call(); | ||
|
||
promise.source.abort(reason); | ||
|
||
await expect(promise).rejects.toThrow(reason); | ||
expect(spy).toHaveBeenCalledWith(reason); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters