Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/yorkie-team/yorkie-js-sdk i…
Browse files Browse the repository at this point in the history
…nto devtools-mvp-2-v2
  • Loading branch information
chacha912 committed Apr 15, 2024
2 parents 01fdeb3 + 3061dbb commit 73a480c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 69 deletions.
52 changes: 21 additions & 31 deletions test/integration/client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,25 @@ describe.sequential('Client', function () {
const eventCollectorC1 = new EventCollector();
const eventCollectorC2 = new EventCollector();

const stubC1 = vi.fn().mockImplementation((event) => {
if (event.type === ClientEventType.DocumentSynced) {
eventCollectorC1.add(event.value);
}
});
const stubC2 = vi.fn().mockImplementation((event) => {
if (event.type === ClientEventType.DocumentSynced) {
eventCollectorC2.add(event.value);
}
});
const stubD1 = vi.fn().mockImplementation((event) => {
eventCollectorD1.add(event.type);
});
const stubD2 = vi.fn().mockImplementation((event) => {
eventCollectorD2.add(event.type);
});

const unsub1 = {
client: c1.subscribe(stubC1),
doc: d1.subscribe(stubD1),
client: c1.subscribe((event) => {
if (event.type === ClientEventType.DocumentSynced) {
eventCollectorC1.add(event.value);
}
}),
doc: d1.subscribe((event) => {
eventCollectorD1.add(event.type);
}),
};
const unsub2 = {
client: c2.subscribe(stubC2),
doc: d2.subscribe(stubD2),
client: c2.subscribe((event) => {
if (event.type === ClientEventType.DocumentSynced) {
eventCollectorC2.add(event.value);
}
}),
doc: d2.subscribe((event) => {
eventCollectorD2.add(event.type);
}),
};

// Normal Condition
Expand Down Expand Up @@ -245,10 +240,9 @@ describe.sequential('Client', function () {

// 02. c2 changes the sync mode to realtime sync mode.
const eventCollector = new EventCollector();
const stub = vi.fn().mockImplementation((event) => {
const unsub1 = c2.subscribe((event) => {
eventCollector.add(event.type);
});
const unsub1 = c2.subscribe(stub);
await c2.changeSyncMode(d2, SyncMode.Realtime);
await eventCollector.waitFor(ClientEventType.DocumentSynced); // sync occurs when resuming

Expand Down Expand Up @@ -310,18 +304,15 @@ describe.sequential('Client', function () {
const eventCollectorD1 = new EventCollector();
const eventCollectorD2 = new EventCollector();
const eventCollectorD3 = new EventCollector();
const stub1 = vi.fn().mockImplementation((event) => {
const unsub1 = d1.subscribe((event) => {
eventCollectorD1.add(event.type);
});
const stub2 = vi.fn().mockImplementation((event) => {
const unsub2 = d2.subscribe((event) => {
eventCollectorD2.add(event.type);
});
const stub3 = vi.fn().mockImplementation((event) => {
const unsub3 = d3.subscribe((event) => {
eventCollectorD3.add(event.type);
});
const unsub1 = d1.subscribe(stub1);
const unsub2 = d2.subscribe(stub2);
const unsub3 = d3.subscribe(stub3);

// 02. [Step1] c1, c2, c3 sync in realtime.
d1.update((root) => {
Expand Down Expand Up @@ -427,10 +418,9 @@ describe.sequential('Client', function () {
const d2 = new yorkie.Document<{ version: string }>(docKey);

const eventCollector = new EventCollector();
const stub = vi.fn().mockImplementation((event) => {
const unsub1 = c2.subscribe((event) => {
eventCollector.add(event.type);
});
const unsub1 = c2.subscribe(stub);

// 01. c2 attach the doc with realtime sync mode at first.
await c1.attach(d1, { syncMode: SyncMode.Manual });
Expand Down
31 changes: 11 additions & 20 deletions test/integration/document_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,12 @@ describe('Document', function () {

const eventCollectorD1 = new EventCollector();
const eventCollectorD2 = new EventCollector();
const stub1 = vi.fn().mockImplementation((event) => {
const unsub1 = d1.subscribe((event) => {
eventCollectorD1.add(event.type);
});
const stub2 = vi.fn().mockImplementation((event) => {
const unsub2 = d2.subscribe((event) => {
eventCollectorD2.add(event.type);
});
const unsub1 = d1.subscribe(stub1);
const unsub2 = d2.subscribe(stub2);

d2.update((root) => {
root['k1'] = 'v1';
Expand Down Expand Up @@ -181,14 +179,13 @@ describe('Document', function () {
let expectedEventValue: Array<OperationInfo>;
const eventCollectorD1 = new EventCollector<EventForTest>();
const eventCollectorD2 = new EventCollector<EventForTest>();
const stub1 = vi.fn().mockImplementation((event) => {
// TODO(chacha912): Remove any type after specifying the type of DocEvent
const unsub1 = d1.subscribe((event: any) => {
eventCollectorD1.add({ type: event.type, value: event.value.operations });
});
const stub2 = vi.fn().mockImplementation((event) => {
const unsub2 = d2.subscribe((event: any) => {
eventCollectorD2.add({ type: event.type, value: event.value.operations });
});
const unsub1 = d1.subscribe(stub1);
const unsub2 = d2.subscribe(stub2);

d1.update((root) => {
root.counter = new yorkie.Counter(yorkie.IntType, 100);
Expand Down Expand Up @@ -300,18 +297,15 @@ describe('Document', function () {
const eventCollector = new EventCollector<EventForTest>();
const eventCollectorForTodos = new EventCollector<EventForTest>();
const eventCollectorForCounter = new EventCollector<EventForTest>();
const stub = vi.fn().mockImplementation((event) => {
const unsub = d1.subscribe((event: any) => {
eventCollector.add(event.value.operations);
});
const stubTodo = vi.fn().mockImplementation((event) => {
const unsubTodo = d1.subscribe('$.todos', (event: any) => {
eventCollectorForTodos.add(event.value.operations);
});
const stubCounter = vi.fn().mockImplementation((event) => {
const unsubCounter = d1.subscribe('$.counter', (event: any) => {
eventCollectorForCounter.add(event.value.operations);
});
const unsub = d1.subscribe(stub);
const unsubTodo = d1.subscribe('$.todos', stubTodo);
const unsubCounter = d1.subscribe('$.counter', stubCounter);

d2.update((root) => {
root.counter = new yorkie.Counter(yorkie.IntType, 0);
Expand Down Expand Up @@ -390,18 +384,15 @@ describe('Document', function () {
const eventCollector = new EventCollector<EventForTest>();
const eventCollectorForTodos0 = new EventCollector<EventForTest>();
const eventCollectorForObjC1 = new EventCollector<EventForTest>();
const stub = vi.fn().mockImplementation((event) => {
const unsub = d1.subscribe((event: any) => {
eventCollector.add(event.value.operations);
});
const stubTodo = vi.fn().mockImplementation((event) => {
const unsubTodo = d1.subscribe('$.todos.0', (event: any) => {
eventCollectorForTodos0.add(event.value.operations);
});
const stubObj = vi.fn().mockImplementation((event) => {
const unsubObj = d1.subscribe('$.obj.c1', (event: any) => {
eventCollectorForObjC1.add(event.value.operations);
});
const unsub = d1.subscribe(stub);
const unsubTodo = d1.subscribe('$.todos.0', stubTodo);
const unsubObj = d1.subscribe('$.obj.c1', stubObj);

d2.update((root) => {
root.todos = [{ text: 'todo1', completed: false }];
Expand Down
12 changes: 4 additions & 8 deletions test/integration/presence_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,9 @@ describe('Presence', function () {
});

const eventCollector = new EventCollector<DocEvent>();
const stub = vi.fn().mockImplementation((event) => {
const unsub = doc1.subscribe('presence', (event) => {
eventCollector.add(event);
});
const unsub = doc1.subscribe('presence', stub);

// 01. c2 attaches doc in realtime sync, and c3 attached doc in manual sync.
const doc2 = new yorkie.Document<{}, PresenceType>(docKey);
Expand Down Expand Up @@ -328,19 +327,17 @@ describe(`Document.Subscribe('presence')`, function () {
await c1.attach(doc1, {
initialPresence: { name: 'a', cursor: { x: 0, y: 0 } },
});
const stub1 = vi.fn().mockImplementation((event) => {
const unsub1 = doc1.subscribe('presence', (event) => {
eventCollectorP1.add(event);
});
const unsub1 = doc1.subscribe('presence', stub1);

const doc2 = new yorkie.Document<{}, PresenceType>(docKey);
await c2.attach(doc2, {
initialPresence: { name: 'b', cursor: { x: 0, y: 0 } },
});
const stub2 = vi.fn().mockImplementation((event) => {
const unsub2 = doc2.subscribe('presence', (event) => {
eventCollectorP2.add(event);
});
const unsub2 = doc2.subscribe('presence', stub2);
await eventCollectorP1.waitAndVerifyNthEvent(1, {
type: DocEventType.Watched,
value: { clientID: c2ID, presence: doc2.getMyPresence() },
Expand Down Expand Up @@ -383,10 +380,9 @@ describe(`Document.Subscribe('presence')`, function () {
const eventCollector = new EventCollector<DocEvent>();
const doc1 = new yorkie.Document<{}, PresenceType>(docKey);
await c1.attach(doc1, { initialPresence: { name: 'a' } });
const stub1 = vi.fn().mockImplementation((event) => {
const unsub1 = doc1.subscribe('presence', (event) => {
eventCollector.add(event);
});
const unsub1 = doc1.subscribe('presence', stub1);

const doc2 = new yorkie.Document<{}, PresenceType>(docKey);
await c2.attach(doc2, { initialPresence: { name: 'b' } });
Expand Down
16 changes: 6 additions & 10 deletions test/unit/document/document_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,10 +967,10 @@ describe.sequential('Document', function () {

type EventForTest = Array<OperationInfo>;
const eventCollector = new EventCollector<EventForTest>();
const stub = vi.fn().mockImplementation((event) => {
// TODO(chacha912): Remove any type after specifying the type of DocEvent
const unsub = doc.subscribe((event: any) => {
eventCollector.add(event.value.operations);
});
const unsub = doc.subscribe(stub);

doc.update((root) => {
root[''] = {};
Expand Down Expand Up @@ -1010,10 +1010,9 @@ describe.sequential('Document', function () {
const doc = new Document<any>('test-doc');
type EventForTest = Array<OperationInfo>;
const eventCollector = new EventCollector<EventForTest>();
const stub = vi.fn().mockImplementation((event) => {
const unsub = doc.subscribe((event: any) => {
eventCollector.add(event.value.operations);
});
const unsub = doc.subscribe(stub);

doc.update((root) => {
root.arr = [];
Expand Down Expand Up @@ -1041,10 +1040,9 @@ describe.sequential('Document', function () {
const doc = new Document<TestDoc>('test-doc');
type EventForTest = Array<OperationInfo>;
const eventCollector = new EventCollector<EventForTest>();
const stub = vi.fn().mockImplementation((event) => {
const unsub = doc.subscribe((event: any) => {
eventCollector.add(event.value.operations);
});
const unsub = doc.subscribe(stub);

doc.update((root) => {
root.cnt = new Counter(CounterType.IntegerCnt, 0);
Expand Down Expand Up @@ -1093,10 +1091,9 @@ describe.sequential('Document', function () {
const doc = new Document<TestDoc>('test-doc');
type EventForTest = Array<OperationInfo>;
const eventCollector = new EventCollector<EventForTest>();
const stub = vi.fn().mockImplementation((event) => {
const unsub = doc.subscribe((event: any) => {
eventCollector.add(event.value.operations);
});
const unsub = doc.subscribe(stub);

doc.update((root) => {
root.text = new Text();
Expand All @@ -1122,10 +1119,9 @@ describe.sequential('Document', function () {
const doc = new Document<TestDoc>('test-doc');
type EventForTest = Array<OperationInfo>;
const eventCollector = new EventCollector<EventForTest>();
const stub = vi.fn().mockImplementation((event) => {
const unsub = doc.subscribe((event: any) => {
eventCollector.add(event.value.operations);
});
const unsub = doc.subscribe(stub);

doc.update((root) => {
root.textWithAttr = new Text();
Expand Down

0 comments on commit 73a480c

Please sign in to comment.