refactor(samples): move example generation code to samples plugin (#8727)
Refs #8577
This commit is contained in:
195
test/unit/core/plugins/samples/fn/get-sample-schema.js
Normal file
195
test/unit/core/plugins/samples/fn/get-sample-schema.js
Normal file
@@ -0,0 +1,195 @@
|
||||
/**
|
||||
* @prettier
|
||||
*/
|
||||
import {
|
||||
memoizedSampleFromSchema,
|
||||
memoizedCreateXMLExample,
|
||||
} from "core/plugins/samples/fn/index"
|
||||
import makeGetSampleSchema from "core/plugins/samples/fn/get-sample-schema"
|
||||
import makeGetJsonSampleSchema from "core/plugins/samples/fn/get-json-sample-schema"
|
||||
import makeGetYamlSampleSchema from "core/plugins/samples/fn/get-yaml-sample-schema"
|
||||
import makeGetXmlSampleSchema from "core/plugins/samples/fn/get-xml-sample-schema"
|
||||
|
||||
describe("getSampleSchema", () => {
|
||||
const oriDate = Date
|
||||
const getSystem = () => ({
|
||||
fn: {
|
||||
memoizedSampleFromSchema,
|
||||
memoizedCreateXMLExample,
|
||||
getJsonSampleSchema: makeGetJsonSampleSchema(getSystem),
|
||||
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
||||
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
||||
},
|
||||
})
|
||||
const getSampleSchema = makeGetSampleSchema(getSystem)
|
||||
|
||||
beforeEach(() => {
|
||||
Date = function () {
|
||||
this.toISOString = function () {
|
||||
return "2018-07-07T07:07:05.189Z"
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
Date = oriDate
|
||||
})
|
||||
|
||||
it("should stringify string values if json content-type", () => {
|
||||
// Given
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "string",
|
||||
format: "date-time",
|
||||
},
|
||||
"text/json"
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(JSON.stringify(new Date().toISOString()))
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify string values for other content-types", () => {
|
||||
// Given
|
||||
const res = getSampleSchema({
|
||||
type: "string",
|
||||
format: "date-time",
|
||||
})
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(new Date().toISOString())
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify non-object values", () => {
|
||||
// Given
|
||||
const res = getSampleSchema({
|
||||
type: "number",
|
||||
})
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify non-object values if content-type is json", () => {
|
||||
// Given
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "number",
|
||||
},
|
||||
"application/json"
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
|
||||
it("should stringify object when literal string example is provided if json content-type", () => {
|
||||
// Given
|
||||
const expected = "<MyObject></MyObject>"
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "object",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
expected
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(JSON.stringify(expected))
|
||||
})
|
||||
|
||||
it("should parse valid json literal example if json content-type", () => {
|
||||
// Given
|
||||
const expected = { test: 123 }
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "object",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
JSON.stringify(expected)
|
||||
)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual.test).toEqual(123)
|
||||
})
|
||||
|
||||
it("should handle number example with string schema as string", () => {
|
||||
// Given
|
||||
const expected = 123
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "string",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
expected
|
||||
)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual("123")
|
||||
})
|
||||
|
||||
it("should handle number literal example with string schema as string", () => {
|
||||
// Given
|
||||
const expected = "123"
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "string",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
expected
|
||||
)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual("123")
|
||||
})
|
||||
|
||||
it("should handle number literal example with number schema as number", () => {
|
||||
// Given
|
||||
const expected = "123"
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "number",
|
||||
},
|
||||
"text/json",
|
||||
{},
|
||||
expected
|
||||
)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual(123)
|
||||
})
|
||||
|
||||
it("should return yaml example if yaml is contained in the content-type", () => {
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "object",
|
||||
},
|
||||
"text/yaml",
|
||||
{},
|
||||
{ test: 123 }
|
||||
)
|
||||
|
||||
expect(res).toEqual("test: 123")
|
||||
})
|
||||
|
||||
it("should return yaml example if yml is contained in the content-type", () => {
|
||||
const res = getSampleSchema(
|
||||
{
|
||||
type: "object",
|
||||
},
|
||||
"text/yml",
|
||||
{},
|
||||
{ test: 123 }
|
||||
)
|
||||
|
||||
expect(res).toEqual("test: 123")
|
||||
})
|
||||
})
|
||||
@@ -1,5 +1,5 @@
|
||||
import { fromJS } from "immutable"
|
||||
import { createXMLExample, sampleFromSchema, memoizedCreateXMLExample, memoizedSampleFromSchema } from "corePlugins/samples/fn"
|
||||
import { createXMLExample, sampleFromSchema, memoizedCreateXMLExample, memoizedSampleFromSchema } from "corePlugins/samples/fn/index"
|
||||
|
||||
describe("sampleFromSchema", () => {
|
||||
it("handles Immutable.js objects for nested schemas", function () {
|
||||
@@ -24,7 +24,6 @@ import {
|
||||
requiresValidationURL,
|
||||
extractFileNameFromContentDispositionHeader,
|
||||
deeplyStripKey,
|
||||
getSampleSchema,
|
||||
paramToIdentifier,
|
||||
paramToValue,
|
||||
generateCodeVerifier,
|
||||
@@ -1615,139 +1614,6 @@ describe("utils", () => {
|
||||
|
||||
})
|
||||
|
||||
describe("getSampleSchema", () => {
|
||||
const oriDate = Date
|
||||
|
||||
beforeEach(() => {
|
||||
Date = function () {
|
||||
this.toISOString = function () {
|
||||
return "2018-07-07T07:07:05.189Z"
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
Date = oriDate
|
||||
})
|
||||
|
||||
it("should stringify string values if json content-type", () => {
|
||||
// Given
|
||||
const res = getSampleSchema({
|
||||
type: "string",
|
||||
format: "date-time"
|
||||
}, "text/json")
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(JSON.stringify(new Date().toISOString()))
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify string values for other content-types", () => {
|
||||
// Given
|
||||
const res = getSampleSchema({
|
||||
type: "string",
|
||||
format: "date-time"
|
||||
})
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(new Date().toISOString())
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify non-object values", () => {
|
||||
// Given
|
||||
const res = getSampleSchema({
|
||||
type: "number"
|
||||
})
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
|
||||
it("should not unnecessarily stringify non-object values if content-type is json", () => {
|
||||
// Given
|
||||
const res = getSampleSchema({
|
||||
type: "number"
|
||||
}, "application/json")
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(0)
|
||||
})
|
||||
|
||||
it("should stringify object when literal string example is provided if json content-type", () => {
|
||||
// Given
|
||||
const expected = "<MyObject></MyObject>"
|
||||
const res = getSampleSchema({
|
||||
type: "object",
|
||||
}, "text/json", {}, expected)
|
||||
|
||||
// Then
|
||||
expect(res).toEqual(JSON.stringify(expected))
|
||||
})
|
||||
|
||||
it("should parse valid json literal example if json content-type", () => {
|
||||
// Given
|
||||
const expected = {test: 123}
|
||||
const res = getSampleSchema({
|
||||
type: "object",
|
||||
}, "text/json", {}, JSON.stringify(expected))
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual.test).toEqual(123)
|
||||
})
|
||||
|
||||
it("should handle number example with string schema as string", () => {
|
||||
// Given
|
||||
const expected = 123
|
||||
const res = getSampleSchema({
|
||||
type: "string",
|
||||
}, "text/json", {}, expected)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual("123")
|
||||
})
|
||||
|
||||
it("should handle number literal example with string schema as string", () => {
|
||||
// Given
|
||||
const expected = "123"
|
||||
const res = getSampleSchema({
|
||||
type: "string",
|
||||
}, "text/json", {}, expected)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual("123")
|
||||
})
|
||||
|
||||
it("should handle number literal example with number schema as number", () => {
|
||||
// Given
|
||||
const expected = "123"
|
||||
const res = getSampleSchema({
|
||||
type: "number",
|
||||
}, "text/json", {}, expected)
|
||||
|
||||
// Then
|
||||
const actual = JSON.parse(res)
|
||||
expect(actual).toEqual(123)
|
||||
})
|
||||
|
||||
it("should return yaml example if yaml is contained in the content-type", () => {
|
||||
const res = getSampleSchema({
|
||||
type: "object",
|
||||
}, "text/yaml", {}, {test: 123})
|
||||
|
||||
expect(res).toEqual("test: 123")
|
||||
})
|
||||
|
||||
it("should return yaml example if yml is contained in the content-type", () => {
|
||||
const res = getSampleSchema({
|
||||
type: "object",
|
||||
}, "text/yml", {}, {test: 123})
|
||||
|
||||
expect(res).toEqual("test: 123")
|
||||
})
|
||||
})
|
||||
|
||||
describe("paramToIdentifier", () => {
|
||||
it("should convert an Immutable parameter map to an identifier", () => {
|
||||
const param = fromJS({
|
||||
|
||||
Reference in New Issue
Block a user