Skip to content

Commit

Permalink
Modify doc.subscribe event test to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 committed Mar 31, 2023
1 parent 894d3ef commit 42d8161
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export type ChangeInfo = {
message: string;
updates: Array<UpdateDelta>;
};
type UpdateDelta =
export type UpdateDelta =
| ModifiedWithPath<AddOpModified>
| ModifiedWithPath<IncreaseOpModified>
| ModifiedWithPath<RemoveOpModified>
Expand Down
70 changes: 69 additions & 1 deletion test/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,22 @@ import { EventEmitter } from 'events';
import { NextFn } from '@yorkie-js-sdk/src/util/observable';

import { ClientEvent } from '@yorkie-js-sdk/src/client/client';
import { DocEvent } from '@yorkie-js-sdk/src/document/document';
import {
DocEvent,
UpdateDelta,
ModifiedWithPath,
} from '@yorkie-js-sdk/src/document/document';
import {
AddOpModified,
IncreaseOpModified,
RemoveOpModified,
SetOpModified,
MoveOpModified,
EditOpModified,
StyleOpModified,
SelectOpModified,
} from '@yorkie-js-sdk/src/document/operation/operation';

import {
TextChange,
TextChangeType,
Expand All @@ -33,6 +48,59 @@ export function range(from: number, to: number): Array<number> {
return list;
}

export type TestDocEvent = {
type: string;
path: string;
} & { [key: string]: any };
export function getUpdateDeltaForTest(
updateDelta: UpdateDelta,
): TestDocEvent | null {
switch (updateDelta.type) {
case 'set': {
const { type, path, key } =
updateDelta as ModifiedWithPath<SetOpModified>;
return { type, path, key };
}
case 'add': {
const { type, path, index } =
updateDelta as ModifiedWithPath<AddOpModified>;
return { type, path, index };
}
case 'move': {
const { type, path, index, previousIndex } =
updateDelta as ModifiedWithPath<MoveOpModified>;
return { type, path, index, previousIndex };
}
case 'remove': {
const { type, path, key, index } =
updateDelta as ModifiedWithPath<RemoveOpModified>;
return key !== undefined ? { type, path, key } : { type, path, index };
}
case 'increase': {
const { type, path, value } =
updateDelta as ModifiedWithPath<IncreaseOpModified>;
return { type, path, value };
}
case 'edit': {
const { type, path, actor, from, to, value } =
updateDelta as ModifiedWithPath<EditOpModified>;
return { type, path, actor, from, to, value };
}
case 'style': {
const { type, path, actor, from, to, value } =
updateDelta as ModifiedWithPath<StyleOpModified>;
return { type, path, actor, from, to, value };
}
case 'select': {
const { type, path, actor, from, to } =
updateDelta as ModifiedWithPath<SelectOpModified>;
return { type, path, actor, from, to };
}
default:
return null;
}
}

export type Indexable = Record<string, any>;

export function waitFor(
Expand Down
51 changes: 40 additions & 11 deletions test/integration/object_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { assert } from 'chai';
import {
getUpdateDeltaForTest,
TestDocEvent,
} from '@yorkie-js-sdk/test/helper/helper';
import { JSONObject } from '@yorkie-js-sdk/src/yorkie';
import { DocEventType, Document } from '@yorkie-js-sdk/src/document/document';
import { Document, DocEventType } from '@yorkie-js-sdk/src/document/document';
import { withTwoClientsAndDocuments } from '@yorkie-js-sdk/test/integration/integration_helper';

describe('Object', function () {
Expand Down Expand Up @@ -180,20 +184,31 @@ describe('Object', function () {
};
k2: number;
}>(async (c1, d1, c2, d2) => {
const expectedEvents: Array<TestDocEvent> = [];
const expectedEvents2: Array<TestDocEvent> = [];
// TODO(hackerwins): consider replacing the below code with `createEmitterAndSpy`.
d2.subscribe((event) => {
if (event.type === DocEventType.RemoteChange) {
assert.deepEqual(event.value[0].paths.sort(), ['$.k1', '$.k2']);
}
if (event.type !== DocEventType.RemoteChange) return;

event.value.forEach(({ updates }) => {
updates.forEach((updateDelta, i) => {
assert.deepEqual(
getUpdateDeltaForTest(updateDelta)!,
expectedEvents[i],
);
});
});
});
d1.subscribe((event) => {
if (event.type === DocEventType.RemoteChange) {
assert.deepEqual(event.value[0].paths, [
'$.k1.selected',
'$.k1.layers.0.a',
'$.k2',
]);
}
if (event.type !== DocEventType.RemoteChange) return;
event.value.forEach(({ updates }) => {
updates.forEach((updateDelta, i) => {
assert.deepEqual(
getUpdateDeltaForTest(updateDelta)!,
expectedEvents2[i],
);
});
});
});
d1.update((root) => {
root['k1'] = {
Expand All @@ -205,6 +220,17 @@ describe('Object', function () {
root['k1']['selected'] = true;
root['k1']['test'] = 'change';
root['k2'] = 5;
expectedEvents.push({ type: 'set', path: '$', key: 'k1' });
expectedEvents.push({ type: 'set', path: '$.k1', key: 'id' });
expectedEvents.push({ type: 'set', path: '$.k1', key: 'selected' });
expectedEvents.push({ type: 'set', path: '$.k1', key: 'test' });
expectedEvents.push({ type: 'set', path: '$.k1', key: 'layers' });
expectedEvents.push({ type: 'add', path: '$.k1.layers', index: 0 });
expectedEvents.push({ type: 'set', path: '$.k1.layers.0', key: 'a' });
expectedEvents.push({ type: 'set', path: '$.k1.layers.0', key: 'b' });
expectedEvents.push({ type: 'set', path: '$.k1', key: 'selected' });
expectedEvents.push({ type: 'set', path: '$.k1', key: 'test' });
expectedEvents.push({ type: 'set', path: '$', key: 'k2' });
});

await c1.sync();
Expand All @@ -214,6 +240,9 @@ describe('Object', function () {
root['k1']['selected'] = false;
root['k1']['layers'][0]['a'] = 'hi2';
root['k2']++;
expectedEvents2.push({ type: 'set', path: '$.k1', key: 'selected' });
expectedEvents2.push({ type: 'set', path: '$.k1.layers.0', key: 'a' });
expectedEvents2.push({ type: 'set', path: '$', key: 'k2' });
});
await c1.sync();
await c2.sync();
Expand Down
Loading

0 comments on commit 42d8161

Please sign in to comment.