Skip to content

Commit 8a5d1a6

Browse files
committed
fix: scripts tests
1 parent 5a272e6 commit 8a5d1a6

File tree

2 files changed

+43
-95
lines changed

2 files changed

+43
-95
lines changed
Lines changed: 35 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { transformCodeSamplesToGuideMethods } from '../snippets.js';
4-
import { SnippetSamples } from '../types.js';
3+
import { parseCodeSamples } from '../snippets.js';
4+
import { CodeSamples } from '../types.js';
55

66
describe('init', () => {
77
it('parses a multi line import', () => {
88
expect(
9-
JSON.parse(
10-
transformCodeSamplesToGuideMethods({
9+
JSON.stringify(
10+
parseCodeSamples({
1111
csharp: {
1212
foo: {
1313
default: `
@@ -42,48 +42,28 @@ var response = await client.CreateConfigAsync(
4242
`,
4343
},
4444
},
45-
} as unknown as SnippetSamples),
45+
} as unknown as CodeSamples),
46+
null,
47+
2,
4648
),
4749
).toMatchInlineSnapshot(`
48-
{
50+
"{
4951
"csharp": {
5052
"foo": {
51-
"default": "var response = await client.CreateConfigAsync(
52-
new ConfigurationWithIndex
53-
{
54-
IndexName = "<YOUR_INDEX_NAME>",
55-
SourceIndices = new List<SourceIndex>
56-
{
57-
new SourceIndex
58-
{
59-
IndexName = "<YOUR_INDEX_NAME>",
60-
Facets = new List<Facet> { new Facet { Attribute = "test" } },
61-
Generate = new List<List<string>>
62-
{
63-
new List<string> { "facetA", "facetB" },
64-
new List<string> { "facetC" },
65-
},
66-
},
67-
},
68-
Languages = new Languages(new List<string> { "french" }),
69-
Exclude = new List<string> { "test" },
70-
}
71-
);",
53+
"default": "var response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);"
7254
},
7355
"init": {
74-
"default": "var client = new QuerySuggestionsClient(
75-
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
76-
);",
77-
},
78-
},
79-
}
56+
"default": "var client = new QuerySuggestionsClient(\\n new QuerySuggestionsConfig(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\")\\n);"
57+
}
58+
}
59+
}"
8060
`);
8161
});
8262

8363
it('parses a single line import', () => {
8464
expect(
85-
JSON.parse(
86-
transformCodeSamplesToGuideMethods({
65+
JSON.stringify(
66+
parseCodeSamples({
8767
csharp: {
8868
foo: {
8969
default: `
@@ -116,48 +96,30 @@ var response = await client.CreateConfigAsync(
11696
`,
11797
},
11898
},
119-
} as unknown as SnippetSamples),
99+
} as unknown as CodeSamples),
100+
null,
101+
2,
120102
),
121103
).toMatchInlineSnapshot(`
122-
{
104+
"{
123105
"csharp": {
124106
"foo": {
125-
"default": "var response = await client.CreateConfigAsync(
126-
new ConfigurationWithIndex
127-
{
128-
IndexName = "<YOUR_INDEX_NAME>",
129-
SourceIndices = new List<SourceIndex>
130-
{
131-
new SourceIndex
132-
{
133-
IndexName = "<YOUR_INDEX_NAME>",
134-
Facets = new List<Facet> { new Facet { Attribute = "test" } },
135-
Generate = new List<List<string>>
136-
{
137-
new List<string> { "facetA", "facetB" },
138-
new List<string> { "facetC" },
139-
},
140-
},
141-
},
142-
Languages = new Languages(new List<string> { "french" }),
143-
Exclude = new List<string> { "test" },
144-
}
145-
);",
107+
"default": "var response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);"
146108
},
147109
"init": {
148-
"default": "var client = new QuerySuggestionsClient(new Client("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION"));",
149-
},
150-
},
151-
}
110+
"default": "var client = new QuerySuggestionsClient(new Client(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\"));"
111+
}
112+
}
113+
}"
152114
`);
153115
});
154116
});
155117

156118
describe('initialize', () => {
157119
it("doesn't stop at `client`", () => {
158120
expect(
159-
JSON.parse(
160-
transformCodeSamplesToGuideMethods({
121+
JSON.stringify(
122+
parseCodeSamples({
161123
csharp: {
162124
foo: {
163125
default: `
@@ -192,41 +154,21 @@ var response = await client.CreateConfigAsync(
192154
`,
193155
},
194156
},
195-
} as unknown as SnippetSamples),
157+
} as unknown as CodeSamples),
158+
null,
159+
2,
196160
),
197161
).toMatchInlineSnapshot(`
198-
{
162+
"{
199163
"csharp": {
200164
"foo": {
201-
"default": "var response = await client.CreateConfigAsync(
202-
new ConfigurationWithIndex
203-
{
204-
IndexName = "<YOUR_INDEX_NAME>",
205-
SourceIndices = new List<SourceIndex>
206-
{
207-
new SourceIndex
208-
{
209-
IndexName = "<YOUR_INDEX_NAME>",
210-
Facets = new List<Facet> { new Facet { Attribute = "test" } },
211-
Generate = new List<List<string>>
212-
{
213-
new List<string> { "facetA", "facetB" },
214-
new List<string> { "facetC" },
215-
},
216-
},
217-
},
218-
Languages = new Languages(new List<string> { "french" }),
219-
Exclude = new List<string> { "test" },
220-
}
221-
);",
165+
"default": "var response = await client.CreateConfigAsync(\\n new ConfigurationWithIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n SourceIndices = new List<SourceIndex>\\n {\\n new SourceIndex\\n {\\n IndexName = \\"<YOUR_INDEX_NAME>\\",\\n Facets = new List<Facet> { new Facet { Attribute = \\"test\\" } },\\n Generate = new List<List<string>>\\n {\\n new List<string> { \\"facetA\\", \\"facetB\\" },\\n new List<string> { \\"facetC\\" },\\n },\\n },\\n },\\n Languages = new Languages(new List<string> { \\"french\\" }),\\n Exclude = new List<string> { \\"test\\" },\\n }\\n);"
222166
},
223167
"init": {
224-
"default": "var client = new QuerySuggestionsClient(
225-
new QuerySuggestionsConfig("YOUR_APP_ID", "YOUR_API_KEY", "YOUR_APP_ID_REGION")
226-
);",
227-
},
228-
},
229-
}
168+
"default": "var client = new QuerySuggestionsClient(\\n new QuerySuggestionsConfig(\\"YOUR_APP_ID\\", \\"YOUR_API_KEY\\", \\"YOUR_APP_ID_REGION\\")\\n);"
169+
}
170+
}
171+
}"
230172
`);
231173
});
232174
});

scripts/specs/snippets.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ export function getCodeSampleLabel(language: Language): OpenAPICodeSample['label
1919
}
2020
}
2121

22-
// Iterates over the result of `transformSnippetsToCodeSamples` in order to generate a JSON file for the doc to consume.
23-
export async function bundleCodeSamplesForDoc(spec: Spec, codeSamples: CodeSamples, clientName: string): Promise<void> {
22+
export function parseCodeSamples(codeSamples: CodeSamples): CodeSamples {
2423
// first we build the end JSON file with our given code samples
2524
for (const [language, operationWithSamples] of Object.entries(codeSamples)) {
2625
for (const [operation, samples] of Object.entries(operationWithSamples)) {
@@ -50,6 +49,13 @@ export async function bundleCodeSamplesForDoc(spec: Spec, codeSamples: CodeSampl
5049
}
5150
}
5251

52+
return codeSamples;
53+
}
54+
55+
// Iterates over the result of `transformSnippetsToCodeSamples` in order to generate a JSON file for the doc to consume.
56+
export async function bundleCodeSamplesForDoc(spec: Spec, codeSamples: CodeSamples, clientName: string): Promise<void> {
57+
codeSamples = parseCodeSamples(codeSamples);
58+
5359
const codeSamplesWithParameters = { ...codeSamples } as CodeSamplesWithAPIDefinition;
5460
const dereferencedSpec = (await $RefParser.dereference(spec, {
5561
mutateInputSchema: false,

0 commit comments

Comments
 (0)