Skip to content

Commit

Permalink
Add docType to test cases to clarify document type
Browse files Browse the repository at this point in the history
  • Loading branch information
raararaara committed Oct 22, 2024
1 parent e0ba8fe commit 0ed6957
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/sdk/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ export class Client {
doc.update((root) => {
for (const [k, v] of Object.entries(options.initialRoot!)) {
if (!crdtObject.get(k)) {
// TODO(hackerwins): type cast
(root as Record<string, unknown>)[k] = v;
// TODO(raararaara): Need a way to accurately infer the type of `k` for indexing.
root[k as keyof T] = v;
}
}
});
Expand Down
64 changes: 40 additions & 24 deletions packages/sdk/test/integration/document_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import {
EventCollector,
assertThrowsAsync,
Indexable,
} from '@yorkie-js-sdk/test/helper/helper';
import type { CRDTElement } from '@yorkie-js-sdk/src/document/crdt/element';
import {
Expand Down Expand Up @@ -1181,86 +1182,101 @@ describe('Document', function () {
describe('With various types', () => {
interface TestCase {
name: string;
input: JSONElement;
input: JSONElement | Indexable;
expectedJSON: string;
}

const testCases: Array<TestCase> = [
{
name: 'json.Tree',
name: 'tree',
input: new Tree({
type: 'doc',
children: [
{ type: 'p', children: [{ type: 'text', value: 'ab' }] },
],
}),
expectedJSON: `{"k":{"type":"doc","children":[{"type":"p","children":[{"type":"text","value":"ab"}]}]}}`,
expectedJSON: `{"tree":{"type":"doc","children":[{"type":"p","children":[{"type":"text","value":"ab"}]}]}}`,
},
{
name: 'json.Text',
name: 'text',
input: new Text(),
expectedJSON: `{"k":[]}`,
expectedJSON: `{"text":[]}`,
},
{
name: 'json.Counter',
name: 'counter',
input: new Counter(CounterType.IntegerCnt, 1),
expectedJSON: `{"k":1}`,
expectedJSON: `{"counter":1}`,
},
{
name: 'null',
input: null,
expectedJSON: `{"k":null}`,
expectedJSON: `{"null":null}`,
},
{
name: 'boolean',
input: true,
expectedJSON: `{"k":true}`,
expectedJSON: `{"boolean":true}`,
},
{
name: 'number',
input: 1,
expectedJSON: `{"k":1}`,
expectedJSON: `{"number":1}`,
},
{
name: 'Long',
name: 'long',
input: Long.MAX_VALUE,
expectedJSON: `{"k":9223372036854775807}`,
expectedJSON: `{"long":9223372036854775807}`,
},
{
name: 'Object',
name: 'object',
input: { k: 'v' },
expectedJSON: `{"k":{"k":"v"}}`,
expectedJSON: `{"object":{"k":"v"}}`,
},
{
name: 'Array',
name: 'array',
input: [1, 2],
expectedJSON: `{"k":[1,2]}`,
expectedJSON: `{"array":[1,2]}`,
},
// TODO(hackerwins): We need to consider the case where the value is
// a byte array and a date.
{
name: 'Bytes',
name: 'bytes',
input: new Uint8Array([1, 2]),
expectedJSON: `{"k":1,2}`,
expectedJSON: `{"bytes":1,2}`,
},
// TODO(hackerwins): Encode Date type to JSON
// {
// name: 'Date',
// input: new Date(0),
// expectedJSON: `{"k":"1970-01-01T00:00:00.000Z"}`,
// },
];

for (const { name: caseName, input, expectedJSON } of testCases) {
it(`Can support various types: ${caseName}`, async function ({ task }) {
for (const { name: name, input, expectedJSON } of testCases) {
it(`Can support various types: ${name}`, async function ({ task }) {
const c1 = new yorkie.Client(testRPCAddr);
await c1.activate();
const docKey = toDocKey(
`${task.name}-${caseName}-${new Date().getTime()}`,
`${task.name}-${name}-${new Date().getTime()}`,
);
const doc = new yorkie.Document(docKey);

type docType = {
tree?: Tree;
text?: Text;
counter?: Counter;
null?: null;
boolean?: boolean;
number?: number;
long?: Long;
object?: Object;
array?: Array<JSONElement>;
bytes?: Uint8Array;
// date: Date;
};
const doc = new yorkie.Document<docType>(docKey);

await c1.attach(doc, {
initialRoot: {
k: input,
[name]: input,
},
});
assert.equal(doc.toSortedJSON(), expectedJSON);
Expand Down

0 comments on commit 0ed6957

Please sign in to comment.