-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathexampleGeneration.spec.tsx
More file actions
214 lines (172 loc) · 6.31 KB
/
exampleGeneration.spec.tsx
File metadata and controls
214 lines (172 loc) · 6.31 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import * as Sampler from '@stoplight/json-schema-sampler';
import { JSONSchema7 } from 'json-schema';
import { generateExamplesFromJsonSchema } from './exampleGeneration';
const modelWithNoExamples: JSONSchema7 = require('../../__fixtures__/models/model-with-no-examples.json');
describe('generateExamplesFromJsonSchema', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('returns error message when example generation fails ', () => {
const errorMessage = 'This is a mocked Error message';
jest.spyOn(Sampler, 'sample').mockImplementation(() => {
throw Error(errorMessage);
});
const example = generateExamplesFromJsonSchema(modelWithNoExamples);
expect(example).toEqual([{ label: '', data: `Example cannot be created for this schema\nError: ${errorMessage}` }]);
});
it('generates example from schema with examples array', () => {
const schema: JSONSchema7 = {
type: 'object',
properties: {
name: { type: 'string' },
},
examples: [{ name: 'Alice' }, { name: 'Bob' }],
};
jest.spyOn(Sampler, 'sample').mockReturnValue({ name: 'generated' });
const result = generateExamplesFromJsonSchema(schema);
expect(result).toHaveLength(2);
expect(result[0].label).toBe('default');
expect(JSON.parse(result[0].data)).toEqual({ name: 'generated' });
expect(result[1].label).toBe('example-1');
expect(JSON.parse(result[1].data)).toEqual({ name: 'generated' });
});
it('generates example from schema with x-examples', () => {
const schema: JSONSchema7 & { 'x-examples'?: unknown } = {
type: 'object',
properties: {
code: { type: 'string' },
message: { type: 'string' },
},
'x-examples': {
'Example 1': {
value: {
code: 'error',
message: 'Something bad happened!',
},
},
},
};
jest.spyOn(Sampler, 'sample').mockReturnValue({ code: 'generated', message: 'generated' });
const result = generateExamplesFromJsonSchema(schema);
expect(result).toHaveLength(1);
expect(result[0].label).toBe('default');
});
it('returns x-examples early when x-stoplight is present', () => {
const schema: JSONSchema7 & { 'x-examples'?: unknown; 'x-stoplight'?: unknown } = {
type: 'object',
properties: {
code: { type: 'string' },
},
'x-stoplight': { id: 'test' },
'x-examples': {
'My Example': {
value: {
code: 'test-code',
},
},
},
};
const sampleSpy = jest.spyOn(Sampler, 'sample');
const result = generateExamplesFromJsonSchema(schema);
expect(sampleSpy).not.toHaveBeenCalled();
expect(result).toHaveLength(1);
expect(result[0].label).toBe('My Example');
expect(JSON.parse(result[0].data)).toEqual({ code: 'test-code' });
});
it('returns examples array early when x-stoplight is present', () => {
const schema: JSONSchema7 & { 'x-stoplight'?: unknown } = {
type: 'object',
properties: {
name: { type: 'string' },
},
examples: [{ name: 'Alice' }],
'x-stoplight': { id: 'test' },
};
const sampleSpy = jest.spyOn(Sampler, 'sample');
const result = generateExamplesFromJsonSchema(schema);
expect(sampleSpy).not.toHaveBeenCalled();
expect(result).toHaveLength(1);
expect(result[0].label).toBe('default');
expect(JSON.parse(result[0].data)).toEqual({ name: 'Alice' });
});
it('generates a default example when schema has no examples', () => {
jest.spyOn(Sampler, 'sample').mockReturnValue({ code: 'string' });
const result = generateExamplesFromJsonSchema(modelWithNoExamples);
expect(result).toHaveLength(1);
expect(result[0].label).toBe('default');
expect(JSON.parse(result[0].data)).toEqual({ code: 'string' });
});
it('returns empty data when Sampler.sample returns null and no examples exist', () => {
jest.spyOn(Sampler, 'sample').mockReturnValue(null);
const schema: JSONSchema7 = {
type: 'object',
};
const result = generateExamplesFromJsonSchema(schema);
expect(result).toEqual([{ label: 'default', data: '' }]);
});
it('handles x-examples without value wrapper', () => {
const schema: JSONSchema7 & { 'x-examples'?: unknown } = {
type: 'object',
properties: {
code: { type: 'string' },
},
'x-examples': {
'Direct Example': {
code: 'direct-value',
extra: 'field',
},
},
};
jest.spyOn(Sampler, 'sample').mockReturnValue({ code: 'generated' });
const result = generateExamplesFromJsonSchema(schema);
expect(result).toHaveLength(1);
expect(result[0].label).toBe('default');
});
it('skips non-object entries in x-examples', () => {
const schema: JSONSchema7 & { 'x-examples'?: unknown; 'x-stoplight'?: unknown } = {
type: 'object',
properties: {
code: { type: 'string' },
},
'x-stoplight': { id: 'test' },
'x-examples': {
'Valid Example': {
value: { code: 'valid' },
},
'Invalid Example': 'not-an-object',
'Another Invalid': 42,
},
};
const result = generateExamplesFromJsonSchema(schema);
expect(result).toHaveLength(1);
expect(result[0].label).toBe('Valid Example');
});
it('labels multiple examples in the examples array correctly', () => {
const schema: JSONSchema7 & { 'x-stoplight'?: unknown } = {
type: 'object',
properties: {
name: { type: 'string' },
},
examples: [{ name: 'First' }, { name: 'Second' }, { name: 'Third' }],
'x-stoplight': { id: 'test' },
};
const result = generateExamplesFromJsonSchema(schema);
expect(result).toHaveLength(3);
expect(result[0].label).toBe('default');
expect(result[1].label).toBe('example-1');
expect(result[2].label).toBe('example-2');
});
it('restores original examples on schema after sampling', () => {
const originalExamples = [{ name: 'Alice' }, { name: 'Bob' }];
const schema: JSONSchema7 = {
type: 'object',
properties: {
name: { type: 'string' },
},
examples: [...originalExamples],
};
jest.spyOn(Sampler, 'sample').mockReturnValue({ name: 'generated' });
generateExamplesFromJsonSchema(schema);
expect(schema.examples).toEqual(originalExamples);
});
});