Skip to content

Commit 1ca6d55

Browse files
committed
add promise unsettled helper per reviewer suggestion, run yarn format
1 parent 3a9ec19 commit 1ca6d55

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

packages/data-connect/test/unit/streamTransport.test.ts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,34 @@ interface TransportWithInternals {
147147
): Promise<void>;
148148
}
149149

150+
/**
151+
* Asserts that a promise does not settle within the given timeout.
152+
* @param promise The promise to test.
153+
* @param timeout The timeout in milliseconds (defaults to 3000ms). Note that the test runner's timeout defaults to 5000ms.
154+
*/
155+
async function expectNotToSettle(
156+
promise: Promise<unknown>,
157+
timeout: number = 3000
158+
): Promise<void> {
159+
const unsettled = { settled: false };
160+
const result = await Promise.race<{
161+
settled: boolean;
162+
resolvedWith?: unknown;
163+
rejectedWith?: unknown;
164+
}>([
165+
promise
166+
.then(resolvedWith => ({ settled: true, resolvedWith }))
167+
.catch(rejectedWith => ({ settled: true, rejectedWith })),
168+
new Promise(resolve => {
169+
setTimeout(() => resolve(unsettled), timeout);
170+
})
171+
]);
172+
expect(result).to.equal(
173+
unsettled,
174+
`expected promise to not settle within ${timeout}ms!`
175+
);
176+
}
177+
150178
describe('AbstractDataConnectStreamTransport', () => {
151179
const dcOptions: DataConnectOptions = {
152180
projectId: 'p',
@@ -397,15 +425,6 @@ describe('AbstractDataConnectStreamTransport', () => {
397425

398426
const queryPromise = transport.invokeQuery(queryName1, variables1);
399427

400-
const didNotSettle = 'invokeQuery unsettled after 3 seconds';
401-
const hasSettled = 'invokeQuery DID settle!!!';
402-
const hasQueryPromiseSettled = Promise.race([
403-
queryPromise.then(() => hasSettled),
404-
new Promise(resolve => {
405-
setTimeout(() => resolve(didNotSettle), 3000);
406-
})
407-
]);
408-
409428
const expectedKey = transport.getMapKey(queryName1, variables1);
410429
expect(transport.activeQueryExecuteRequests.has(expectedKey)).to.be
411430
.true;
@@ -423,9 +442,7 @@ describe('AbstractDataConnectStreamTransport', () => {
423442
expect(sentMessage.execute).to.not.be.undefined;
424443
expect(sentMessage.execute?.operationName).to.equal(queryName1);
425444
expect(sentMessage.execute?.variables).to.deep.equal(variables1);
426-
await expect(hasQueryPromiseSettled).to.eventually.equal(
427-
didNotSettle
428-
);
445+
await expectNotToSettle(queryPromise);
429446
});
430447
});
431448

@@ -438,15 +455,6 @@ describe('AbstractDataConnectStreamTransport', () => {
438455
variables1
439456
);
440457

441-
const didNotSettle = 'invokeMutation unsettled after 3 seconds';
442-
const hasSettled = 'invokeMutation DID settle!!!';
443-
const hasMutationPromiseSettled = Promise.race([
444-
mutationPromise.then(() => hasSettled),
445-
new Promise(resolve => {
446-
setTimeout(() => resolve(didNotSettle), 3000);
447-
})
448-
]);
449-
450458
const expectedKey = transport.getMapKey(mutationName1, variables1);
451459
const activeRequests =
452460
transport.activeMutationExecuteRequests.get(expectedKey);
@@ -463,9 +471,7 @@ describe('AbstractDataConnectStreamTransport', () => {
463471
expect(sentMessage.execute).to.not.be.undefined;
464472
expect(sentMessage.execute?.operationName).to.equal(mutationName1);
465473
expect(sentMessage.execute?.variables).to.deep.equal(variables1);
466-
await expect(hasMutationPromiseSettled).to.eventually.equal(
467-
didNotSettle
468-
);
474+
await expectNotToSettle(mutationPromise);
469475
});
470476
});
471477

0 commit comments

Comments
 (0)