Skip to content

Commit 0df23e1

Browse files
committed
test(ExampleGenerator): valid enum option retured as example
1 parent 947d453 commit 0df23e1

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

test/ExampleGenerator.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2321,7 +2321,7 @@ describe('ExampleGenerator', () => {
23212321
schema = element._resolve(schema);
23222322
const result = element._computeJsonPropertyValue(schema);
23232323
assert.typeOf(result, 'object');
2324-
assert.deepEqual(result, { messages: [{ "referrers": "", "sequenceId": 1, "text": "", "type": "", "tz": "", "variables": "" }]})
2324+
assert.deepEqual(result, { messages: [{ "referrers": "", "sequenceId": 1, "text": "", "type": "init", "tz": "", "variables": "" }]})
23252325
});
23262326
});
23272327
});

test/W-11843862.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { fixture, assert, html } from '@open-wc/testing';
2+
import { AmfLoader } from './amf-loader.js';
3+
import '../api-example-generator.js';
4+
5+
describe('W-11843862', () => {
6+
async function basicFixture(amf) {
7+
return fixture(html`<api-example-generator
8+
.amf="${amf}"
9+
></api-example-generator>`);
10+
}
11+
12+
const apiFile = 'W-11843862';
13+
14+
[
15+
['json+ld data model', false],
16+
['Compact data model', true],
17+
].forEach(([label, compact]) => {
18+
describe(/** @type {string} */ (label), () => {
19+
let element;
20+
let amf;
21+
22+
before(async () => {
23+
amf = await AmfLoader.load(/** @type {boolean} */ (compact), apiFile);
24+
});
25+
26+
beforeEach(async () => {
27+
element = await basicFixture(amf);
28+
});
29+
30+
it('Returns first enum option as example', () => {
31+
const headers = AmfLoader.lookupExpectsHeaderSchema(amf, '/cas/ad/contribution/external/v1/bill/list', 'get');
32+
const examples = element.computeExamples(headers[0], 'application/json', {});
33+
assert.lengthOf(examples, 1);
34+
assert.equal(examples[0].value, '1')
35+
});
36+
});
37+
});
38+
});

test/amf-loader.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ AmfLoader.lookupPayload = function(model, endpoint, operation) {
6666
return helper._ensureArray(helper._computePayload(expects));
6767
};
6868

69+
/**
70+
* Looks for headers in an operation
71+
* @param {any} model
72+
* @param {string} endpoint Endpoint's path
73+
* @param {string} operation Operation name (lowercase)
74+
* @return {Array<Object>}
75+
*/
76+
AmfLoader.lookupExpectsHeaderSchema = function(model, endpoint, operation) {
77+
const op = AmfLoader.lookupOperation(model, endpoint, operation);
78+
const expects = helper._computeExpects(op);
79+
if (!expects) {
80+
throw new Error(`The ${operation.toUpperCase()} ${endpoint} operation has no request definition.`);
81+
}
82+
const headers = helper._computeHeaders(expects);
83+
const sKey = helper._getAmfKey(helper.ns.aml.vocabularies.shapes.schema);
84+
return headers[0][sKey];
85+
};
86+
6987
/**
7088
* Looks for a payload in the responses list of an operation
7189
* @param {any} model

0 commit comments

Comments
 (0)