Skip to content

Commit f973552

Browse files
committed
test: 💍 simplify remote tests
1 parent 033ccce commit f973552

File tree

1 file changed

+31
-68
lines changed

1 file changed

+31
-68
lines changed
Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,65 @@
1-
import {s} from 'json-joy/lib/json-crdt';
1+
import {NodeBuilder, s} from 'json-joy/lib/json-crdt';
22
import {setup as setup0} from './setup';
33
import {until} from 'thingies/lib/until';
4-
import {tick} from 'thingies';
54

6-
const setup = async () => {
5+
const setupTwoSessions = async (schema?: undefined | NodeBuilder) => {
76
const kit = await setup0();
7+
const id = kit.blockId;
8+
const session1 = kit.sessions.make({id, schema});
9+
await until(async () => {
10+
try {
11+
await kit.getModelFromRemote(id.join('/'));
12+
return true;
13+
} catch {
14+
return false;
15+
}
16+
});
17+
const session2 = await kit.sessions.load({id, remote: {}});
18+
const stop = async () => {
19+
await session1.dispose();
20+
await session2.dispose();
21+
await kit.stop();
22+
};
23+
return [session1, session2, stop] as const;
824
};
925

1026
describe('sync through remote server', () => {
1127
test('can load block which exists remotely (no schema)', async () => {
12-
const kit = await setup0();
13-
const id = kit.blockId;
14-
const schema = undefined;
15-
const session1 = kit.sessions.make({id, schema});
16-
await until(async () => {
17-
try {
18-
const model = await kit.getModelFromRemote(id.join('/'));
19-
expect(model.view()).toEqual(undefined);
20-
return true;
21-
} catch {
22-
return false;
23-
}
24-
});
25-
const session2 = await kit.sessions.load({id, remote: {}});
28+
const [session1, session2, stop] = await setupTwoSessions();
29+
expect(session1.model.view()).toBe(undefined);
2630
expect(session2.model.view()).toBe(undefined);
27-
await session1.dispose();
28-
await session2.dispose();
29-
await kit.stop();
31+
await stop();
3032
});
3133

3234
test('can load block which exists remotely (with schema)', async () => {
33-
const kit = await setup0();
34-
const id = kit.blockId;
3535
const schema = s.obj({foo: s.str('bar')});
36-
const session1 = kit.sessions.make({id, schema});
37-
await until(async () => {
38-
try {
39-
const model = await kit.getModelFromRemote(id.join('/'));
40-
expect(model.view()).toEqual({foo: 'bar'});
41-
return true;
42-
} catch {
43-
return false;
44-
}
45-
});
46-
const session2 = await kit.sessions.load({id, remote: {}});
36+
const [session1, session2, stop] = await setupTwoSessions(schema);
37+
expect(session1.model.view()).toEqual({foo: 'bar'});
4738
expect(session2.model.view()).toEqual({foo: 'bar'});
48-
await session1.dispose();
49-
await session2.dispose();
50-
await kit.stop();
39+
await stop();
5140
});
5241

5342
test('receives a changes done remotely', async () => {
54-
const kit = await setup0();
55-
const id = kit.blockId;
5643
const schema = undefined;
57-
const session1 = kit.sessions.make({id, schema});
58-
await until(async () => {
59-
try {
60-
const model = await kit.getModelFromRemote(id.join('/'));
61-
expect(model.view()).toEqual(undefined);
62-
return true;
63-
} catch {
64-
return false;
65-
}
66-
});
67-
const session2 = await kit.sessions.load({id, remote: {}});
44+
const [session1, session2, stop] = await setupTwoSessions(schema);
45+
expect(session1.model.view()).toBe(undefined);
6846
expect(session2.model.view()).toBe(undefined);
6947
session1.model.api.root({foo: 'bar'});
7048
await until(() => session2.model.view()?.foo === 'bar');
7149
expect(session1.model.view()).toEqual({foo: 'bar'});
7250
expect(session2.model.view()).toEqual({foo: 'bar'});
73-
await session1.dispose();
74-
await session2.dispose();
75-
await kit.stop();
51+
await stop();
7652
});
7753

7854
test('receives a changes done remotely (reverse)', async () => {
79-
const kit = await setup0();
80-
const id = kit.blockId;
8155
const schema = undefined;
82-
const session1 = kit.sessions.make({id, schema});
83-
await until(async () => {
84-
try {
85-
const model = await kit.getModelFromRemote(id.join('/'));
86-
expect(model.view()).toEqual(undefined);
87-
return true;
88-
} catch {
89-
return false;
90-
}
91-
});
92-
const session2 = await kit.sessions.load({id, remote: {}});
56+
const [session1, session2, stop] = await setupTwoSessions(schema);
57+
expect(session1.model.view()).toBe(undefined);
9358
expect(session2.model.view()).toBe(undefined);
9459
session2.model.api.root({foo: 'bar'});
9560
await until(() => session1.model.view()?.foo === 'bar');
9661
expect(session1.model.view()).toEqual({foo: 'bar'});
9762
expect(session2.model.view()).toEqual({foo: 'bar'});
98-
await session1.dispose();
99-
await session2.dispose();
100-
await kit.stop();
63+
await stop();
10164
});
10265
});

0 commit comments

Comments
 (0)