|
| 1 | +import '../../polyfill.ts'; |
| 2 | + |
| 3 | +import {describe, expect, it, vi, type MockedObject} from 'vitest'; |
| 4 | + |
| 5 | +import {BatchingRemoteConnection, RemoteConnection} from '../../elements'; |
| 6 | + |
| 7 | +describe('BatchingRemoteConnection', () => { |
| 8 | + it('batches mutations', async () => { |
| 9 | + const connection = createRemoteConnectionSpy(); |
| 10 | + const batchingConnection = new BatchingRemoteConnection(connection); |
| 11 | + |
| 12 | + batchingConnection.mutate([1, 2, 3]); |
| 13 | + batchingConnection.mutate([4, 5, 6]); |
| 14 | + |
| 15 | + expect(connection.mutate).not.toHaveBeenCalled(); |
| 16 | + |
| 17 | + await waitForNextTask(); |
| 18 | + |
| 19 | + expect(connection.mutate).toHaveBeenCalledTimes(1); |
| 20 | + expect(connection.mutate).toHaveBeenCalledWith([1, 2, 3, 4, 5, 6]); |
| 21 | + |
| 22 | + batchingConnection.mutate([7, 8, 9]); |
| 23 | + |
| 24 | + expect(connection.mutate).toHaveBeenCalledTimes(1); |
| 25 | + |
| 26 | + await waitForNextTask(); |
| 27 | + |
| 28 | + expect(connection.mutate).toHaveBeenCalledTimes(2); |
| 29 | + expect(connection.mutate).toHaveBeenCalledWith([7, 8, 9]); |
| 30 | + }); |
| 31 | + |
| 32 | + it('flushes mutations', async () => { |
| 33 | + const connection = createRemoteConnectionSpy(); |
| 34 | + const batchingConnection = new BatchingRemoteConnection(connection); |
| 35 | + |
| 36 | + batchingConnection.mutate([1, 2, 3]); |
| 37 | + batchingConnection.flush(); |
| 38 | + |
| 39 | + expect(connection.mutate).toHaveBeenCalledOnce(); |
| 40 | + expect(connection.mutate).toHaveBeenCalledWith([1, 2, 3]); |
| 41 | + |
| 42 | + await waitForNextTask(); |
| 43 | + |
| 44 | + // ensure it wasn't called again |
| 45 | + expect(connection.mutate).toHaveBeenCalledOnce(); |
| 46 | + batchingConnection.mutate([4, 5, 6]); |
| 47 | + |
| 48 | + await waitForNextTask(); |
| 49 | + |
| 50 | + expect(connection.mutate).toHaveBeenCalledTimes(2); |
| 51 | + expect(connection.mutate).toHaveBeenCalledWith([4, 5, 6]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('enqueues the batch function only once while items are in the queue', async () => { |
| 55 | + const connection = createRemoteConnectionSpy(); |
| 56 | + const batch = vi.fn(); |
| 57 | + const batchingConnection = new BatchingRemoteConnection(connection, { |
| 58 | + batch, |
| 59 | + }); |
| 60 | + |
| 61 | + batchingConnection.mutate([1, 2, 3]); |
| 62 | + batchingConnection.mutate([4, 5, 6]); |
| 63 | + expect(batch).toHaveBeenCalledOnce(); |
| 64 | + |
| 65 | + const batchCallback = batch.mock.calls[0][0]; |
| 66 | + batchCallback(); |
| 67 | + |
| 68 | + batchingConnection.mutate([7, 8, 9]); |
| 69 | + expect(batch).toHaveBeenCalledTimes(2); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +async function waitForNextTask() { |
| 74 | + const channel = new MessageChannel(); |
| 75 | + const promise = new Promise((resolve) => { |
| 76 | + channel.port1.onmessage = resolve; |
| 77 | + }); |
| 78 | + channel.port2.postMessage(null); |
| 79 | + |
| 80 | + await promise; |
| 81 | +} |
| 82 | + |
| 83 | +function createRemoteConnectionSpy(): MockedObject<RemoteConnection> { |
| 84 | + return { |
| 85 | + mutate: vi.fn(), |
| 86 | + call: vi.fn(), |
| 87 | + }; |
| 88 | +} |
0 commit comments