"Fossies" - the Fresh Open Source Software Archive 
Member "angular-cli-15.2.4/tests/angular_devkit/core/json/schema/serializers/1.0.javascript_spec.ts" (16 Mar 2023, 1933 Bytes) of package /linux/www/angular-cli-15.2.4.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8
9 // tslint:disable:no-any
10 import { schema } from '@angular-devkit/core';
11
12 const { serializers } = schema;
13
14
15 export function works(registry: schema.JsonSchemaRegistry, schema: any) {
16 const value = {
17 'requiredKey': 1,
18 };
19
20 registry.addSchema('', schema);
21 const v = (new serializers.JavascriptSerializer()).serialize('', registry)(value);
22
23 expect(v.requiredKey).toBe(1);
24 expect(() => delete v.requiredKey).toThrow();
25
26 expect(v.stringKeyDefault).toBe('defaultValue');
27 v.stringKeyDefault = 'hello';
28 expect(v.stringKeyDefault).toBe('hello');
29 delete v.stringKeyDefault;
30 expect(v.stringKeyDefault).toBe('defaultValue');
31
32 expect(v.objectKey1).toBe(undefined);
33 v.objectKey1 = { stringKey: 'str' };
34 expect(v.objectKey1.stringKey).toBe('str');
35
36 expect(v.objectKey1.objectKey).toBe(undefined);
37 v.objectKey1.objectKey = { stringKey: 'str2' };
38 expect(v.objectKey1.objectKey.stringKey).toBe('str2');
39
40 expect(Object.keys(v.objectKey1)).toEqual(['stringKey', 'stringKeyDefault', 'objectKey']);
41 }
42
43
44 export function accessUndefined(registry: schema.JsonSchemaRegistry, schema: any) {
45 const value = {
46 'requiredKey': 1,
47 };
48
49 registry.addSchema('', schema);
50 const v = (new serializers.JavascriptSerializer({
51 allowAccessUndefinedObjects: true,
52 })).serialize('', registry)(value);
53
54 // Access an undefined property.
55 v.objectKey1.stringKey = 'hello';
56 expect(v.objectKey1).not.toBe(undefined);
57 expect(v.objectKey1.stringKey).toBe('hello');
58 expect(v.objectKey1.numberKey).toBe(undefined);
59 v.objectKey1.stringKey = undefined;
60 expect(v.objectKey1.stringKey).toBe(undefined);
61
62 expect(v.stringKeyDefault).toBe('defaultValue');
63 expect(v.objectKey1.stringKeyDefault).toBe('defaultValue2');
64 }