Skip to content

Commit b3e9529

Browse files
committed
create tests
1 parent 288d74c commit b3e9529

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

test/unit/definitionGenerator.spec.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const path = require("path");
99

1010
const serverlessMock = require("../helpers/serverless");
1111
const modelsDocument = require("../models/models/models.json");
12+
const alternativeModelsDocument = require("../models/models/models-alt.json");
1213

1314
const schemaHandler = require('../../src/schemaHandler');
1415

@@ -1254,5 +1255,89 @@ describe("DefinitionGenerator", () => {
12541255
expect(response["200"].headers).to.have.property("x-rate-limit");
12551256
});
12561257
});
1258+
1259+
describe(`mediaTypeObjects`, function () {
1260+
describe(`content style`, function () {
1261+
it(`should add examples when examples are specified`, async function () {
1262+
const modelsWithExample = structuredClone(modelsDocument);
1263+
modelsWithExample.models.at(0).content["application/json"].examples = [{ name: '404Error', summary: 'an example of a 404 error', description: 'This is what a 404 error looks like', value: '404' }]
1264+
Object.assign(mockServerless.service.custom.documentation, modelsWithExample);
1265+
1266+
const description = "this is a description";
1267+
const responseMock = {
1268+
methodResponses: [
1269+
{
1270+
responseBody: { description: description },
1271+
responseModels: { 'application/json': 'ErrorResponse' },
1272+
statusCode: 200,
1273+
},
1274+
],
1275+
};
1276+
1277+
const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/ErrorResponse')
1278+
1279+
const definitionGenerator = new DefinitionGenerator(
1280+
mockServerless,
1281+
logger
1282+
);
1283+
1284+
const response = await definitionGenerator.createResponses(responseMock);
1285+
1286+
expect(response).to.be.an("object");
1287+
expect(response).to.have.property("200");
1288+
expect(response["200"]).to.have.property('content')
1289+
expect(response["200"].content).to.be.an('object')
1290+
expect(response["200"].content).to.have.property('application/json')
1291+
expect(response["200"].content['application/json']).to.be.an('object')
1292+
expect(response["200"].content['application/json']).to.have.property('examples')
1293+
expect(response["200"].content['application/json'].examples).to.be.an('object')
1294+
expect(response["200"].content['application/json'].examples).to.have.property('404Error')
1295+
1296+
1297+
stub.restore()
1298+
});
1299+
});
1300+
1301+
describe(`contentType style`, function () {
1302+
it(`should add examples when examples are specified`, async function () {
1303+
const altModelsWithExample = structuredClone(alternativeModelsDocument);
1304+
altModelsWithExample.models.at(0).examples = [{ name: '404Error', summary: 'an example of a 404 error', description: 'This is what a 404 error looks like', value: '404' }]
1305+
Object.assign(mockServerless.service.custom.documentation, altModelsWithExample);
1306+
1307+
const description = "this is a description";
1308+
const responseMock = {
1309+
methodResponses: [
1310+
{
1311+
responseBody: { description: description },
1312+
responseModels: { 'application/json': 'ErrorResponse' },
1313+
statusCode: 200,
1314+
},
1315+
],
1316+
};
1317+
1318+
const stub = sinon.stub(schemaHandler.prototype, 'createSchema').resolves('#components/schemas/ErrorResponse')
1319+
1320+
const definitionGenerator = new DefinitionGenerator(
1321+
mockServerless,
1322+
logger
1323+
);
1324+
1325+
1326+
const response = await definitionGenerator.createResponses(responseMock);
1327+
1328+
expect(response).to.be.an("object");
1329+
expect(response).to.have.property("200");
1330+
expect(response["200"]).to.have.property('content')
1331+
expect(response["200"].content).to.be.an('object')
1332+
expect(response["200"].content).to.have.property('application/json')
1333+
expect(response["200"].content['application/json']).to.be.an('object')
1334+
expect(response["200"].content['application/json']).to.have.property('examples')
1335+
expect(response["200"].content['application/json'].examples).to.be.an('object')
1336+
expect(response["200"].content['application/json'].examples).to.have.property('404Error')
1337+
1338+
stub.restore()
1339+
});
1340+
});
1341+
});
12571342
});
12581343
});

0 commit comments

Comments
 (0)