Skip to content

Commit 4bdc192

Browse files
committed
[tests] Remove waitFor
1 parent faf5629 commit 4bdc192

4 files changed

Lines changed: 67 additions & 126 deletions

File tree

test/unit/common/other.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ export const pause = async (ms = 50): Promise<void> =>
2323
setTimeout(() => setTimeout(() => setTimeout(resolve, 1), ms - 2), 1),
2424
);
2525

26-
export const waitFor = async (assert: () => any, timeoutMs = 50) => {
27-
const startTime = Date.now();
28-
while (Date.now() - startTime < timeoutMs) {
29-
try {
30-
await assert();
31-
return;
32-
} catch {
33-
await pause(10);
34-
}
35-
}
36-
return assert();
37-
};
38-
3926
export const mockFetchWasm = (): void => {
4027
fetchMock.enableMocks();
4128
fetchMock.resetMocks();

test/unit/persisters/database/json.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'fake-indexeddb/auto';
22
import type {Store} from 'tinybase';
33
import {createStore} from 'tinybase';
44
import type {Persister} from 'tinybase/persisters';
5-
import {mockFetchWasm, pause, waitFor} from '../../common/other.ts';
5+
import {mockFetchWasm, pause} from '../../common/other.ts';
66
import {ALL_VARIANTS, getDatabaseFunctions} from '../common/databases.ts';
77

88
describe.each(Object.entries(ALL_VARIANTS))(
@@ -579,9 +579,7 @@ describe.each(Object.entries(ALL_VARIANTS))(
579579
store.setTables({t1: {r1: {c1: 1}}}).setValues({v1: 1});
580580
await persister.save();
581581
await pause(autoLoadPause);
582-
await waitFor(() => {
583-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
584-
}, 1000);
582+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
585583
});
586584

587585
test('autoSave1 & autoLoad2', async () => {
@@ -590,10 +588,7 @@ describe.each(Object.entries(ALL_VARIANTS))(
590588
await pause(autoLoadPause);
591589
store.setTables({t1: {r1: {c1: 1}}}).setValues({v1: 1});
592590
await pause(autoLoadPause);
593-
await waitFor(() => {
594-
// todo this is failing
595-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
596-
}, 1000);
591+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
597592
});
598593

599594
test('autoSave1 & autoLoad2, complex transactions', async () => {

test/unit/persisters/database/mergeable-json.test.ts

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type {MergeableStore} from 'tinybase';
44
import {createMergeableStore} from 'tinybase';
55
import type {Persister} from 'tinybase/persisters';
66
import {getTimeFunctions} from '../../common/mergeable.ts';
7-
import {mockFetchWasm, waitFor} from '../../common/other.ts';
7+
import {mockFetchWasm} from '../../common/other.ts';
88
import {MERGEABLE_VARIANTS, getDatabaseFunctions} from '../common/databases.ts';
99

1010
const [reset, getNow, pause] = getTimeFunctions();
@@ -377,9 +377,7 @@ describe.each(Object.entries(MERGEABLE_VARIANTS))(
377377
store.setTables({t1: {r1: {c1: 1}}}).setValues({v1: 1});
378378
await persister.save();
379379
await pause(autoLoadPause);
380-
await waitFor(() => {
381-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
382-
}, 1000);
380+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
383381
});
384382

385383
test('autoSave1 & autoLoad2', async () => {
@@ -388,9 +386,7 @@ describe.each(Object.entries(MERGEABLE_VARIANTS))(
388386
await pause(autoLoadPause);
389387
store.setTables({t1: {r1: {c1: 1}}}).setValues({v1: 1});
390388
await pause(autoLoadPause);
391-
await waitFor(() => {
392-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
393-
}, 1000);
389+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
394390
});
395391

396392
test('autoSave1 & autoLoad2, complex transactions', async () => {
@@ -404,54 +400,40 @@ describe.each(Object.entries(MERGEABLE_VARIANTS))(
404400
})
405401
.setValues({v1: 1, v2: 2});
406402
await pause(autoLoadPause);
407-
await waitFor(() => {
408-
expect(store2.getContent()).toEqual([
409-
{t1: {r1: {c1: 1, c2: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
410-
{v1: 1, v2: 2},
411-
]);
412-
}, 1000);
403+
expect(store2.getContent()).toEqual([
404+
{t1: {r1: {c1: 1, c2: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
405+
{v1: 1, v2: 2},
406+
]);
413407
store.setCell('t1', 'r1', 'c1', 2);
414408
await pause(autoLoadPause);
415-
await waitFor(() => {
416-
expect(store2.getContent()).toEqual([
417-
{t1: {r1: {c1: 2, c2: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
418-
{v1: 1, v2: 2},
419-
]);
420-
}, 1000);
409+
expect(store2.getContent()).toEqual([
410+
{t1: {r1: {c1: 2, c2: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
411+
{v1: 1, v2: 2},
412+
]);
421413
store.delCell('t1', 'r1', 'c2');
422414
await pause(autoLoadPause);
423-
await waitFor(() => {
424-
expect(store2.getContent()).toEqual([
425-
{t1: {r1: {c1: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
426-
{v1: 1, v2: 2},
427-
]);
428-
}, 1000);
415+
expect(store2.getContent()).toEqual([
416+
{t1: {r1: {c1: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
417+
{v1: 1, v2: 2},
418+
]);
429419
store.delRow('t1', 'r2');
430420
await pause(autoLoadPause);
431-
await waitFor(() => {
432-
expect(store2.getContent()).toEqual([
433-
{t1: {r1: {c1: 2}}, t2: {r1: {c1: 1}}},
434-
{v1: 1, v2: 2},
435-
]);
436-
}, 1000);
421+
expect(store2.getContent()).toEqual([
422+
{t1: {r1: {c1: 2}}, t2: {r1: {c1: 1}}},
423+
{v1: 1, v2: 2},
424+
]);
437425
store.delTable('t2');
438426
await pause(autoLoadPause);
439-
await waitFor(() => {
440-
expect(store2.getContent()).toEqual([
441-
{t1: {r1: {c1: 2}}},
442-
{v1: 1, v2: 2},
443-
]);
444-
}, 1000);
427+
expect(store2.getContent()).toEqual([
428+
{t1: {r1: {c1: 2}}},
429+
{v1: 1, v2: 2},
430+
]);
445431
store.delValue('v2');
446432
await pause(autoLoadPause);
447-
await waitFor(() => {
448-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 1}]);
449-
}, 1000);
433+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 1}]);
450434
store.setValue('v1', 2);
451435
await pause(autoLoadPause);
452-
await waitFor(() => {
453-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 2}]);
454-
}, 1000);
436+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 2}]);
455437
}, 20000);
456438
});
457439

test/unit/persisters/database/tabular.test.ts

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'fake-indexeddb/auto';
44
import type {Store} from 'tinybase';
55
import {createStore} from 'tinybase';
66
import type {Persister} from 'tinybase/persisters';
7-
import {mockFetchWasm, pause, waitFor} from '../../common/other.ts';
7+
import {mockFetchWasm, pause} from '../../common/other.ts';
88
import {ALL_VARIANTS, getDatabaseFunctions} from '../common/databases.ts';
99

1010
describe.each(Object.entries(ALL_VARIANTS))(
@@ -1091,22 +1091,16 @@ describe.each(Object.entries(ALL_VARIANTS))(
10911091
});
10921092
await persister.startAutoLoad();
10931093
await pause(autoLoadPause);
1094-
await waitFor(() => {
1095-
expect(store.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
1096-
}, 1000);
1094+
expect(store.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
10971095
await cmd(db, 'UPDATE t1 SET c1=$1 WHERE _id=$2', [2, 'r1']);
10981096
await pause(autoLoadPause);
1099-
await waitFor(() => {
1100-
expect(store.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 1}]);
1101-
}, 1000);
1097+
expect(store.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 1}]);
11021098
await cmd(db, 'UPDATE tinybase_values SET v1=$1 WHERE _id=$2', [
11031099
2,
11041100
'_',
11051101
]);
11061102
await pause(autoLoadPause);
1107-
await waitFor(() => {
1108-
expect(store.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 2}]);
1109-
}, 1000);
1103+
expect(store.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 2}]);
11101104
});
11111105

11121106
test('autoLoad, table dropped and recreated', async () => {
@@ -1145,9 +1139,7 @@ describe.each(Object.entries(ALL_VARIANTS))(
11451139
);
11461140
await cmd(db, 'INSERT INTO t1 (_id, c1) VALUES ($1, $2)', ['r1', 3]);
11471141
await pause(autoLoadPause);
1148-
await waitFor(() => {
1149-
expect(store.getContent()).toEqual([{t1: {r1: {c1: 3}}}, {v1: 1}]);
1150-
}, 1000);
1142+
expect(store.getContent()).toEqual([{t1: {r1: {c1: 3}}}, {v1: 1}]);
11511143
await cmd(db, 'DROP TABLE tinybase_values');
11521144
await cmd(
11531145
db,
@@ -1162,18 +1154,14 @@ describe.each(Object.entries(ALL_VARIANTS))(
11621154
3,
11631155
]);
11641156
await pause(autoLoadPause);
1165-
await waitFor(() => {
1166-
expect(store.getContent()).toEqual([{t1: {r1: {c1: 3}}}, {v1: 3}]);
1167-
}, 1000);
1157+
expect(store.getContent()).toEqual([{t1: {r1: {c1: 3}}}, {v1: 3}]);
11681158
await cmd(db, 'UPDATE t1 SET c1 = $1 WHERE _id = $2', [4, 'r1']);
11691159
await cmd(db, 'UPDATE tinybase_values SET v1 = $1 WHERE _id = $2', [
11701160
4,
11711161
'_',
11721162
]);
11731163
await pause(autoLoadPause);
1174-
await waitFor(() => {
1175-
expect(store.getContent()).toEqual([{t1: {r1: {c1: 4}}}, {v1: 4}]);
1176-
}, 1000);
1164+
expect(store.getContent()).toEqual([{t1: {r1: {c1: 4}}}, {v1: 4}]);
11771165
});
11781166
});
11791167

@@ -1730,10 +1718,7 @@ describe.each(Object.entries(ALL_VARIANTS))(
17301718
store1.setTables({t1: {r1: {c1: 1}}}).setValues({v1: 1});
17311719
await persister1.save();
17321720
await pause(autoLoadPause);
1733-
// todo this is failing
1734-
await waitFor(() => {
1735-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
1736-
}, 1000);
1721+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
17371722
});
17381723

17391724
test('autoSave1 & autoLoad2', async () => {
@@ -1742,9 +1727,8 @@ describe.each(Object.entries(ALL_VARIANTS))(
17421727
await pause(autoLoadPause);
17431728
store1.setTables({t1: {r1: {c1: 1}}}).setValues({v1: 1});
17441729
await pause(autoLoadPause);
1745-
await waitFor(() => {
1746-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
1747-
}, 1000);
1730+
1731+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 1}}}, {v1: 1}]);
17481732
});
17491733

17501734
test('autoSave1 & autoLoad2, complex transactions', async () => {
@@ -1759,54 +1743,47 @@ describe.each(Object.entries(ALL_VARIANTS))(
17591743
{v1: 1, v2: 2},
17601744
]);
17611745
await pause(autoLoadPause);
1762-
await waitFor(() => {
1763-
expect(store2.getContent()).toEqual([
1764-
{t1: {r1: {c1: 1, c2: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
1765-
{v1: 1, v2: 2},
1766-
]);
1767-
}, 1000);
1746+
1747+
expect(store2.getContent()).toEqual([
1748+
{t1: {r1: {c1: 1, c2: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
1749+
{v1: 1, v2: 2},
1750+
]);
17681751
store1.setCell('t1', 'r1', 'c1', 2);
17691752
await pause(autoLoadPause);
1770-
await waitFor(() => {
1771-
expect(store2.getContent()).toEqual([
1772-
{t1: {r1: {c1: 2, c2: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
1773-
{v1: 1, v2: 2},
1774-
]);
1775-
}, 1000);
1753+
1754+
expect(store2.getContent()).toEqual([
1755+
{t1: {r1: {c1: 2, c2: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
1756+
{v1: 1, v2: 2},
1757+
]);
17761758
store1.delCell('t1', 'r1', 'c2');
17771759
await pause(autoLoadPause);
1778-
await waitFor(() => {
1779-
expect(store2.getContent()).toEqual([
1780-
{t1: {r1: {c1: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
1781-
{v1: 1, v2: 2},
1782-
]);
1783-
}, 1000);
1760+
1761+
expect(store2.getContent()).toEqual([
1762+
{t1: {r1: {c1: 2}, r2: {c1: 1}}, t2: {r1: {c1: 1}}},
1763+
{v1: 1, v2: 2},
1764+
]);
17841765
store1.delRow('t1', 'r2');
17851766
await pause(autoLoadPause);
1786-
await waitFor(() => {
1787-
expect(store2.getContent()).toEqual([
1788-
{t1: {r1: {c1: 2}}, t2: {r1: {c1: 1}}},
1789-
{v1: 1, v2: 2},
1790-
]);
1791-
}, 1000);
1767+
1768+
expect(store2.getContent()).toEqual([
1769+
{t1: {r1: {c1: 2}}, t2: {r1: {c1: 1}}},
1770+
{v1: 1, v2: 2},
1771+
]);
17921772
store1.delTable('t2');
17931773
await pause(autoLoadPause);
1794-
await waitFor(() => {
1795-
expect(store2.getContent()).toEqual([
1796-
{t1: {r1: {c1: 2}}},
1797-
{v1: 1, v2: 2},
1798-
]);
1799-
}, 1000);
1774+
1775+
expect(store2.getContent()).toEqual([
1776+
{t1: {r1: {c1: 2}}},
1777+
{v1: 1, v2: 2},
1778+
]);
18001779
store1.delValue('v2');
18011780
await pause(autoLoadPause);
1802-
await waitFor(() => {
1803-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 1}]);
1804-
}, 1000);
1781+
1782+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 1}]);
18051783
store1.setValue('v1', 2);
18061784
await pause(autoLoadPause);
1807-
await waitFor(() => {
1808-
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 2}]);
1809-
}, 1000);
1785+
1786+
expect(store2.getContent()).toEqual([{t1: {r1: {c1: 2}}}, {v1: 2}]);
18101787
}, 20000);
18111788
});
18121789

0 commit comments

Comments
 (0)