aboutsummaryrefslogtreecommitdiff
path: root/src/debug/Test.tsx
blob: 033615be67898b405eece3dc309cd8c94d5eb3cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { serialize, deserialize, map } from 'serializr';
import { URLField, Doc, createSchema, makeInterface, makeStrictInterface, List, ListSpec } from '../fields/NewDoc';
import { SerializationHelper } from '../client/util/SerializationHelper';

const schema1 = createSchema({
    hello: "number",
    test: "string",
    fields: "boolean",
    url: URLField,
    testDoc: Doc
});

const TestDoc = makeInterface(schema1);
type TestDoc = makeInterface<typeof schema1>;

const schema2 = createSchema({
    hello: URLField,
    test: "boolean",
    fields: { List: "number" } as ListSpec<number>,
    url: "number",
    testDoc: URLField
});

const Test2Doc = makeStrictInterface(schema2);
type Test2Doc = makeStrictInterface<typeof schema2>;

const schema3 = createSchema({
    test: "boolean",
});

const Test3Doc = makeStrictInterface(schema3);
type Test3Doc = makeStrictInterface<typeof schema3>;

const assert = (bool: boolean) => {
    if (!bool) throw new Error();
};

class Test extends React.Component {
    onClick = () => {
        const url = new URLField(new URL("http://google.com"));
        const doc = new Doc();
        const doc2 = new Doc();
        doc.hello = 5;
        doc.fields = "test";
        doc.test = "hello doc";
        doc.url = url;
        doc.testDoc = doc2;


        const test1: TestDoc = TestDoc(doc);
        const test2: Test2Doc = Test2Doc(doc);
        assert(test1.hello === 5);
        assert(test1.fields === undefined);
        assert(test1.test === "hello doc");
        assert(test1.url === url);
        assert(test1.testDoc === doc2);
        test1.myField = 20;
        assert(test1.myField === 20);

        assert(test2.hello === undefined);
        // assert(test2.fields === "test");
        assert(test2.test === undefined);
        assert(test2.url === undefined);
        assert(test2.testDoc === undefined);
        test2.url = 35;
        assert(test2.url === 35);
    }

    render() {
        return <button onClick={this.onClick}>Click me</button>;
    }
}

ReactDOM.render(
    <Test />,
    document.getElementById('root')
);