aboutsummaryrefslogtreecommitdiff
path: root/test/test.ts
blob: ee14a93406c87dae3915053462777ee027ca8023 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { expect } from 'chai';
import 'mocha';
const { JSDOM } = require('jsdom');
const dom = new JSDOM('', {
    url: `http://localhost:${resolvedPorts.server}`,
});
(global as any).window = dom.window;

import { reaction } from 'mobx';
import { resolvedPorts } from '../src/client/util/CurrentUserUtils';
import { Doc } from '../src/fields/Doc';
import { createSchema, defaultSpec, makeInterface } from '../src/fields/Schema';
import { Cast } from '../src/fields/Types';
import { ImageField } from '../src/fields/URLField';
describe('Document', () => {
    it('should hold fields', () => {
        const key = 'Test';
        const key2 = 'Test2';
        const field = 15;
        const doc = new Doc();
        doc[key] = field;
        const getField = Cast(doc[key], 'number');
        const getField2 = Cast(doc[key2], 'number');
        expect(getField).to.equal(field);
        expect(getField2).to.equal(undefined);
    });

    it('should update', () => {
        const doc = new Doc();
        const key = 'Test';
        const key2 = 'Test2';
        let ran = false;
        reaction(
            () => doc[key],
            field => {
                ran = true;
            }
        );
        expect(ran).to.equal(false);

        doc[key2] = 4;
        expect(ran).to.equal(false);

        doc[key] = 5;

        expect(ran).to.equal(true);
    });
});

const testSchema1 = createSchema({
    a: 'number',
    b: 'string',
    c: 'boolean',
    d: ImageField,
    e: Doc,
});

type TestDoc = makeInterface<[typeof testSchema1]>;
const TestDoc = makeInterface(testSchema1);

const testSchema2 = createSchema({
    a: defaultSpec('boolean', true),
    b: defaultSpec('number', 5),
    c: defaultSpec('string', 'hello world'),
});

type TestDoc2 = makeInterface<[typeof testSchema2]>;
const TestDoc2 = makeInterface(testSchema2);

const testSchema3 = createSchema({
    a: TestDoc2,
});

type TestDoc3 = makeInterface<[typeof testSchema3]>;
const TestDoc3 = makeInterface(testSchema3);

describe('Schema', () => {
    it('should do the right thing 1', () => {
        const test1 = new Doc();
        const test2 = new Doc();
        const ifield = new ImageField(new URL('http://google.com'));
        test1.a = 5;
        test1.b = 'hello';
        test1.c = true;
        test1.d = ifield;
        test1.e = test2;
        const doc = TestDoc(test1);
        expect(doc.a).to.equal(5);
        expect(doc.b).to.equal('hello');
        expect(doc.c).to.equal(true);
        expect(doc.d).to.equal(ifield);
        expect(doc.e).to.equal(test2);
    });

    it('should do the right thing 2', () => {
        const test1 = new Doc();
        const test2 = new Doc();
        const ifield = new ImageField(new URL('http://google.com'));
        test1.a = 'hello';
        test1.b = 5;
        test1.c = test2;
        test1.d = true;
        test1.e = ifield;
        const doc = TestDoc(test1);
        expect(doc.a).to.equal(undefined);
        expect(doc.b).to.equal(undefined);
        expect(doc.c).to.equal(undefined);
        expect(doc.d).to.equal(undefined);
        expect(doc.e).to.equal(undefined);
    });

    it('should do the right thing 3', () => {
        const test1 = new Doc();
        const test2 = new Doc();
        const ifield = new ImageField(new URL('http://google.com'));
        test1.a = 'hello';
        test1.b = 5;
        test1.c = test2;
        test1.d = true;
        test1.e = ifield;
        const doc = TestDoc(test1);
        expect(doc.a).to.equal(undefined);
        expect(doc.b).to.equal(undefined);
        expect(doc.c).to.equal(undefined);
        expect(doc.d).to.equal(undefined);
        expect(doc.e).to.equal(undefined);
    });

    it('should do the right thing 4', () => {
        const doc = TestDoc2();
        expect(doc.a).to.equal(true);
        expect(doc.b).to.equal(5);
        expect(doc.c).to.equal('hello world');

        const d2 = new Doc();
        d2.a = false;
        d2.b = 4;
        d2.c = 'goodbye';
        const doc2 = TestDoc2(d2);
        expect(doc2.a).to.equal(false);
        expect(doc2.b).to.equal(4);
        expect(doc2.c).to.equal('goodbye');

        const d3 = new Doc();
        d3.a = 'hello';
        d3.b = false;
        d3.c = 5;
        const doc3 = TestDoc2(d3);
        expect(doc3.a).to.equal(true);
        expect(doc3.b).to.equal(5);
        expect(doc3.c).to.equal('hello world');
    });

    it('should do the right thing 5', async () => {
        const test1 = new Doc();
        const test2 = new Doc();
        const doc = TestDoc3(test1);
        expect(doc.a).to.equal(undefined);
        test1.a = test2;
        const doc2 = (await doc.a)!;
        expect(doc2.a).to.equal(true);
        expect(doc2.b).to.equal(5);
        expect(doc2.c).to.equal('hello world');
    });
});