-
Notifications
You must be signed in to change notification settings - Fork 60
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
1 parent
ae3e737
commit 3c59afd
Showing
3 changed files
with
83 additions
and
10 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,5 @@ | ||
--- | ||
'@remote-dom/core': minor | ||
--- | ||
|
||
add flush method to BatchingRemoteConnection |
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,64 @@ | ||
import '../../polyfill.ts'; | ||
|
||
import {describe, expect, it, vi, type MockedObject} from 'vitest'; | ||
|
||
import {BatchingRemoteConnection, RemoteConnection} from '../../elements'; | ||
|
||
describe('BatchingRemoteConnection', () => { | ||
it('batches mutations', async () => { | ||
const connection = createRemoteConnectionSpy(); | ||
const batchingConnection = new BatchingRemoteConnection(connection); | ||
|
||
batchingConnection.mutate([1, 2, 3]); | ||
batchingConnection.mutate([4, 5, 6]); | ||
|
||
expect(connection.mutate).not.toHaveBeenCalled(); | ||
|
||
await waitOneTick(); | ||
|
||
expect(connection.mutate).toHaveBeenCalledTimes(1); | ||
expect(connection.mutate).toHaveBeenCalledWith([1, 2, 3, 4, 5, 6]); | ||
|
||
batchingConnection.mutate([7, 8, 9]); | ||
|
||
expect(connection.mutate).toHaveBeenCalledTimes(1); | ||
|
||
await waitOneTick(); | ||
|
||
expect(connection.mutate).toHaveBeenCalledTimes(2); | ||
expect(connection.mutate).toHaveBeenCalledWith([7, 8, 9]); | ||
}); | ||
|
||
it('flushes mutations', async () => { | ||
const connection = createRemoteConnectionSpy(); | ||
const batchingConnection = new BatchingRemoteConnection(connection); | ||
|
||
batchingConnection.mutate([1, 2, 3]); | ||
batchingConnection.flush(); | ||
|
||
expect(connection.mutate).toHaveBeenCalledOnce(); | ||
expect(connection.mutate).toHaveBeenCalledWith([1, 2, 3]); | ||
|
||
await waitOneTick(); | ||
|
||
// ensure it wasn't called again | ||
expect(connection.mutate).toHaveBeenCalledOnce(); | ||
batchingConnection.mutate([4, 5, 6]); | ||
|
||
await waitOneTick(); | ||
|
||
expect(connection.mutate).toHaveBeenCalledTimes(2); | ||
expect(connection.mutate).toHaveBeenCalledWith([4, 5, 6]); | ||
}); | ||
}); | ||
|
||
async function waitOneTick() { | ||
await new Promise((resolve) => setTimeout(resolve, 0)); | ||
} | ||
|
||
function createRemoteConnectionSpy(): MockedObject<RemoteConnection> { | ||
return { | ||
mutate: vi.fn(), | ||
call: vi.fn(), | ||
}; | ||
} |