-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathstring-intersection.test.ts
More file actions
101 lines (79 loc) · 4 KB
/
string-intersection.test.ts
File metadata and controls
101 lines (79 loc) · 4 KB
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { expect, test, beforeAll } from 'vitest';
import { ComponentDefinition } from '../../src/components/interfaces';
import { buildProject } from './test-helpers';
let codeEditor: ComponentDefinition;
beforeAll(() => {
const result = buildProject('string-intersection');
expect(result).toHaveLength(1);
[codeEditor] = result;
});
test('should properly handle union types with string intersection for custom values', () => {
const languageProp = codeEditor.properties.find(def => def.name === 'language');
expect(languageProp?.name).toBe('language');
expect(languageProp?.description).toBe('Specifies the programming language.');
expect(languageProp?.optional).toBe(false);
expect(languageProp?.type).toBe('string');
// Check inline type structure
expect(languageProp?.inlineType?.name).toBe('CodeEditorProps.Language');
expect(languageProp?.inlineType?.type).toBe('union');
if (languageProp?.inlineType?.type === 'union') {
expect(languageProp.inlineType.valueDescriptions).toBeUndefined();
}
// The intersection type "string & { _?: undefined; }" should be converted to "string"
// String literal values should appear without quotes
const values = (languageProp?.inlineType as any)?.values;
expect(values).toHaveLength(6);
expect(values).toContain('javascript');
expect(values).toContain('html');
expect(values).toContain('ruby');
expect(values).toContain('python');
expect(values).toContain('java');
expect(values).toContain('string'); // The intersection type becomes "string" to indicate custom values are allowed
});
test('should treat the union as primitive string type', () => {
const languageProp = codeEditor.properties.find(def => def.name === 'language');
// The type should be 'string' not the full union name
expect(languageProp?.type).toBe('string');
});
test('should convert intersection helper to "string" in values array', () => {
const languageProp = codeEditor.properties.find(def => def.name === 'language');
// Should not contain the raw "string & { _?: undefined; }" syntax
const hasRawIntersectionType =
languageProp?.inlineType?.type === 'union' &&
languageProp.inlineType.values.some((value: string) => value.includes('string &') || value.includes('_?:'));
expect(hasRawIntersectionType).toBe(false);
// But should contain "string" to indicate custom values are allowed
const hasStringValue =
languageProp?.inlineType?.type === 'union' && languageProp.inlineType.values.includes('string');
expect(hasStringValue).toBe(true);
});
test('should detect intersection types with string & pattern', () => {
const languageProp = codeEditor.properties.find(def => def.name === 'language');
// Verify that the union contains both string literals and the intersection type
expect(languageProp?.inlineType?.type).toBe('union');
if (languageProp?.inlineType?.type === 'union') {
const values = languageProp.inlineType.values;
// Should have the literal values
expect(values).toContain('javascript');
expect(values).toContain('html');
expect(values).toContain('ruby');
expect(values).toContain('python');
expect(values).toContain('java');
// Should have 'string' representing the intersection type
expect(values).toContain('string');
// All values should be treated as string type (primitive detection)
expect(languageProp.type).toBe('string');
}
});
test('should recognize intersection type as string-compatible', () => {
const languageProp = codeEditor.properties.find(def => def.name === 'language');
// The union with intersection type should be recognized as primitive string
expect(languageProp?.type).toBe('string');
if (languageProp?.inlineType?.type === 'union') {
// All values in the union should be compatible with string type
const allValuesAreStrings = languageProp.inlineType.values.every((value: string) => typeof value === 'string');
expect(allValuesAreStrings).toBe(true);
}
});