Skip to content

Commit fe88143

Browse files
committed
WIP: TS tests for UpdateData
1 parent a9da1b7 commit fe88143

File tree

1 file changed

+343
-0
lines changed

1 file changed

+343
-0
lines changed
Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
/**
2+
* @license
3+
* Copyright 2023 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0x00 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0x00
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import { expect } from 'chai';
18+
19+
import {
20+
UpdateData
21+
} from '../../../src/lite-api/reference';
22+
import { hardAssert } from '../../../src/util/assert';
23+
24+
type MyUnion = string | number;
25+
26+
type NestedObject = {
27+
boo: boolean,
28+
str: string,
29+
num: number,
30+
nul: null,
31+
und: undefined,
32+
customUnion: MyUnion
33+
}
34+
35+
36+
describe.only('UpdateData - v9', () => {
37+
type MyServerType = {
38+
// primitive types
39+
boo: boolean,
40+
str: string,
41+
num: number,
42+
nul: null,
43+
und: undefined,
44+
45+
// custom types
46+
customUnion: MyUnion,
47+
customObject: NestedObject,
48+
49+
// nested objects
50+
nested: {
51+
bar: {
52+
boo: boolean,
53+
str: string,
54+
anotherLayer: {
55+
boo: boolean,
56+
str: string,
57+
}
58+
},
59+
baz: {
60+
boo: boolean,
61+
str: string,
62+
anotherLayer: {
63+
boo: boolean,
64+
str: string,
65+
}
66+
}
67+
},
68+
69+
// index signatures nested 1 layer deep
70+
indexed: {
71+
[name: string]: {
72+
desc: boolean,
73+
num: number
74+
}
75+
},
76+
77+
// TODO v10 - index signatures nested 2 layers deep
78+
79+
// property with dots in the name
80+
'property.with.dots': boolean
81+
}
82+
83+
it("Supports properties with primitive types", () => {
84+
let testData : UpdateData<MyServerType>;
85+
testData = {
86+
boo: true,
87+
str: "string",
88+
num: 2,
89+
nul: null,
90+
und: undefined
91+
};
92+
93+
testData = {
94+
// @ts-expect-error
95+
boo: "string",
96+
// @ts-expect-error
97+
str: 1,
98+
// @ts-expect-error
99+
num: "string",
100+
// @ts-expect-error
101+
nul: "string",
102+
// @ts-expect-error
103+
und: "string",
104+
};
105+
106+
expect(true).to.be.true;
107+
});
108+
109+
it("Supports properties with custom types", () => {
110+
let testData : UpdateData<MyServerType>;
111+
testData = {
112+
customUnion: "string",
113+
customObject: {
114+
boo: true,
115+
str: "string",
116+
num: 2,
117+
nul: null,
118+
und: undefined,
119+
customUnion: 1,
120+
}
121+
};
122+
123+
testData = {
124+
// @ts-expect-error
125+
customUnion: true,
126+
127+
// @ts-expect-error
128+
customObject: true
129+
};
130+
131+
testData = {
132+
customObject: {
133+
// @ts-expect-error
134+
boo: "string",
135+
// @ts-expect-error
136+
str: 1,
137+
// @ts-expect-error
138+
num: "string",
139+
// @ts-expect-error
140+
nul: "string",
141+
// @ts-expect-error
142+
und: "string",
143+
}
144+
};
145+
146+
expect(true).to.be.true;
147+
});
148+
149+
describe("given properties with dots", () => {
150+
it("preserves the value type", () => {
151+
let testData: UpdateData<MyServerType>;
152+
153+
// Allows values of expected type
154+
testData = {
155+
'property.with.dots': true
156+
};
157+
158+
// Errors on values of unexpected type
159+
testData = {
160+
// @ts-expect-error
161+
'property.with.dots': 1
162+
};
163+
164+
expect(true).to.be.true;
165+
});
166+
167+
it("does not allow matching a sub-string|path", () => {
168+
let testData: UpdateData<MyServerType>;
169+
170+
testData = {
171+
// @ts-expect-error
172+
'property.with': true
173+
};
174+
175+
expect(true).to.be.true;
176+
});
177+
})
178+
179+
describe("given nested objects (no index properties)", () => {
180+
it("supports object replacement at each layer (with partial)", () => {
181+
let testData: UpdateData<MyServerType>;
182+
testData = {
183+
nested: {}
184+
};
185+
186+
testData = {
187+
nested: {
188+
bar: {},
189+
baz: {}
190+
}
191+
};
192+
193+
testData = {
194+
nested: {
195+
bar: {
196+
boo: true,
197+
str: "string"
198+
},
199+
baz: {
200+
str: "string"
201+
}
202+
}
203+
};
204+
205+
testData = {
206+
nested: {
207+
bar: {
208+
boo: true,
209+
str: "string",
210+
anotherLayer: {
211+
boo: false,
212+
str: "another string"
213+
}
214+
}
215+
}
216+
};
217+
218+
expect(true).to.be.true;
219+
});
220+
221+
it("preserves value types at each layer", () => {
222+
let testData: UpdateData<MyServerType>;
223+
testData = {
224+
// @ts-expect-error
225+
nested: true
226+
};
227+
228+
testData = {
229+
nested: {
230+
bar: {
231+
// @ts-expect-error
232+
str: true,
233+
// @ts-expect-error
234+
anotherLayer: true
235+
},
236+
baz: {
237+
anotherLayer: {
238+
// @ts-expect-error
239+
boo: "string value"
240+
}
241+
}
242+
}
243+
};
244+
245+
expect(true).to.be.true;
246+
});
247+
248+
it("does not allow properties that were not on the original type", () => {
249+
let testData: UpdateData<MyServerType>;
250+
testData = {
251+
// @ts-expect-error
252+
unknown: true
253+
};
254+
255+
testData = {
256+
nested: {
257+
// @ts-expect-error
258+
unknown: true
259+
}
260+
};
261+
262+
expect(true).to.be.true;
263+
});
264+
265+
it("preserves value types for dot notation", () => {
266+
let testData: UpdateData<MyServerType>;
267+
268+
// 2 layers with dot notation
269+
270+
// preserves type
271+
testData = {
272+
'nested.bar': {},
273+
'nested.baz': {},
274+
};
275+
276+
// preserves properties of nested objects
277+
testData = {
278+
'nested.bar': {
279+
boo: true,
280+
str: "string",
281+
anotherLayer: {
282+
boo: false,
283+
str: "string",
284+
}
285+
},
286+
'nested.baz': {
287+
boo: true
288+
}
289+
};
290+
291+
// preserves type - failure
292+
testData = {
293+
// @ts-expect-error
294+
'nested.bar': false,
295+
// @ts-expect-error
296+
'nested.baz': "string",
297+
};
298+
299+
// preserves properties of nested objects - failure
300+
testData = {
301+
'nested.bar': {
302+
// @ts-expect-error
303+
boo: "string"
304+
}
305+
};
306+
307+
// 3 layers with dot notation
308+
309+
// preserves type
310+
testData = {
311+
'nested.bar.boo': true,
312+
'nested.bar.anotherLayer': {},
313+
};
314+
315+
// preserves properties of nested objects
316+
testData = {
317+
'nested.bar.anotherLayer': {
318+
boo: false,
319+
str: "string"
320+
}
321+
};
322+
323+
// preserves type - failure
324+
testData = {
325+
// @ts-expect-error
326+
'nested.bar.anotherLayer': true,
327+
// @ts-expect-error
328+
'nested.baz.anotherLayer': "string",
329+
};
330+
331+
// preserves properties of nested objects - failure
332+
testData = {
333+
'nested.bar.anotherLayer': {
334+
// @ts-expect-error
335+
boo: "string"
336+
},
337+
};
338+
339+
expect(true).to.be.true;
340+
});
341+
});
342+
343+
});

0 commit comments

Comments
 (0)