import { createXMLExample } from "corePlugins/samples/fn" import expect from "expect" describe("createXMLExample", function () { var sut = createXMLExample describe("simple types with xml property", function () { it("returns tag string when passing type string and xml:{name: \"newtagname\"}", function () { var definition = { type: "string", xml: { name: "newtagname" } } expect(sut(definition)).toEqual("\nstring") }) it("returns tag string when passing type string and xml:{name: \"newtagname\", prefix:\"test\"}", function () { var definition = { type: "string", xml: { name: "newtagname", prefix: "test" } } expect(sut(definition)).toEqual("\nstring") }) it("returns tag string when passing type string and xml:{\"namespace\": \"http://swagger.io/schema/sample\", \"prefix\": \"sample\"}", function () { var definition = { type: "string", xml: { namespace: "http://swagger.io/schema/sample", prefix: "sample", name: "name" } } expect(sut(definition)).toEqual("\nstring") }) it("returns tag string when passing type string and xml:{\"namespace\": \"http://swagger.io/schema/sample\"}", function () { var definition = { type: "string", xml: { namespace: "http://swagger.io/schema/sample", name: "name" } } expect(sut(definition)).toEqual("\nstring") }) it("returns tag test when passing default value", function () { var expected = "\ntest" var definition = { type: "string", "default": "test", xml: { name: "newtagname" } } expect(sut(definition)).toEqual(expected) }) it("returns default value when enum provided", function () { var expected = "\none" var definition = { type: "string", "default": "one", "enum": ["two", "one"], xml: { name: "newtagname" } } expect(sut(definition)).toEqual(expected) }) it("returns example value when provided", function () { var expected = "\ntwo" var definition = { type: "string", "default": "one", "example": "two", "enum": ["two", "one"], xml: { name: "newtagname" } } expect(sut(definition)).toEqual(expected) }) it("sets first enum if provided", function () { var expected = "\none" var definition = { type: "string", "enum": ["one", "two"], xml: { name: "newtagname" } } expect(sut(definition)).toEqual(expected) }) }) describe("array", function () { it("returns tag string when passing string items", function () { var expected = "\nstring" var definition = { type: "array", items: { type: "string" }, xml: { name: "tagname" } } expect(sut(definition)).toEqual(expected) }) it("returns tag string when passing string items with name", function () { var expected = "\nstring" var definition = { type: "array", items: { type: "string", xml: { name: "animal" } }, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns tag string when passing string items with name", function () { var expected = "\n\n\tstring\n" var definition = { type: "array", items: { type: "string", xml: { name: "animal" } }, xml: { wrapped: true, name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("return correct nested wrapped array", function () { var expected = "\n\n\tstring\n" var definition = { type: "array", items: { type: "array", items: { type: "string" }, xml: { name: "dog" } }, xml: { wrapped: true, name: "aliens" } } expect(sut(definition)).toEqual(expected) }) it("return correct nested wrapped array with xml", function () { var expected = "\n\n\t\n\t\tstring\n\t\n" var definition = { type: "array", items: { type: "array", items: { type: "string", xml: { name: "dog" } }, xml: { name: "dogs", wrapped: true } }, xml: { wrapped: true, name: "aliens" } } expect(sut(definition)).toEqual(expected) }) it("adds namespace to array", function () { var expected = "\nstring" var definition = { type: "array", items: { type: "string", xml: { name: "dog", namespace: "test" } }, xml: { name: "aliens", namespace: "test_new" } } expect(sut(definition)).toEqual(expected) }) it("adds prefix to array", function () { var expected = "\nstring" var definition = { type: "array", items: { type: "string", xml: { name: "dog", prefix: "test" } }, xml: { name: "aliens", prefix: "test_new" } } expect(sut(definition)).toEqual(expected) }) it("adds prefix to array with no xml in items", function () { var expected = "\nstring" var definition = { type: "array", items: { type: "string" }, xml: { name: "dog", prefix: "test" } } expect(sut(definition)).toEqual(expected) }) it("adds namespace to array with no xml in items", function () { var expected = "\nstring" var definition = { type: "array", items: { type: "string" }, xml: { name: "dog", namespace: "test" } } expect(sut(definition)).toEqual(expected) }) it("adds namespace to array with wrapped", function () { var expected = "\n\n\tstring\n" var definition = { type: "array", items: { type: "string", xml: { name: "dog" } }, xml: { wrapped: true, name: "aliens", namespace: "test" } } expect(sut(definition)).toEqual(expected) }) it("adds prefix to array with wrapped", function () { var expected = "\n\n\tstring\n" var definition = { type: "array", items: { type: "string", xml: { name: "dog" } }, xml: { wrapped: true, name: "aliens", prefix: "test" } } expect(sut(definition)).toEqual(expected) }) it("returns wrapped array when type is not passed", function () { var expected = "\n\n\tstring\n" var definition = { items: { type: "string", xml: { name: "animal" } }, xml: { wrapped: true, name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns array with default values", function () { var expected = "\none\ntwo" var definition = { items: { type: "string", xml: { name: "animal" } }, "default": ["one", "two"], xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns array with default values with wrapped=true", function () { var expected = "\n\n\tone\n\ttwo\n" var definition = { items: { type: "string", xml: { name: "animal" } }, "default": ["one", "two"], xml: { wrapped: true, name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns array with default values", function () { var expected = "\none" var definition = { items: { type: "string", "enum": ["one", "two"], xml: { name: "animal" } }, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns array with default values with wrapped=true", function () { var expected = "\n\n\tone\n" var definition = { items: { "enum": ["one", "two"], type: "string", xml: { name: "animal" } }, xml: { wrapped: true, name: "animals" } } expect(sut(definition)).toEqual(expected) }) }) describe("object", function () { it("returns object with 2 properties", function () { var expected = "\n\n\tstring\n\t0\n" var definition = { type: "object", properties: { alien: { type: "string" }, dog: { type: "integer" } }, xml: { name: "aliens" } } expect(sut(definition)).toEqual(expected) }) it("returns object with integer property and array property", function () { var expected = "\n\n\tstring\n\t0\n" var definition = { type: "object", properties: { aliens: { type: "array", items: { type: "string" } }, dog: { type: "integer" } }, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns nested objects", function () { var expected = "\n\n\t\n\t\tstring\n\t\n\tstring\n" var definition = { type: "object", properties: { aliens: { type: "object", properties: { alien: { type: "string", xml: { name: "alien" } } } }, dog: { type: "string" } }, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns object with no readonly fields for parameter", function () { var expected = "\n\n\t0\n" var definition = { type: "object", properties: { id: { type: "integer" }, dog: { readOnly: true, type: "string" } }, xml: { name: "animals" } } expect(sut(definition, { includeReadOnly: false })).toEqual(expected) }) it("returns object with passed property as attribute", function () { var expected = "\n\n\tstring\n" var definition = { type: "object", properties: { id: { type: "integer", xml: { attribute: true } }, dog: { type: "string" } }, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns object with passed property as attribute with custom name", function () { var expected = "\n\n\tstring\n" var definition = { type: "object", properties: { id: { type: "integer", xml: { attribute: true, name: "test" } }, dog: { type: "string" } }, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns object with example values in attribute", function () { var expected = "\n\n\tadmin\n" var definition = { type: "object", properties: { id: { type: "integer", xml: { attribute: true } }, role:{ type: "string" } }, xml: { name: "user" }, example: { id: 42, role: "admin" } } expect(sut(definition)).toEqual(expected) }) it("returns object with enum values in attribute", function () { var expected = "\n\n\tstring\n" var definition = { type: "object", properties: { id: { type: "string", "enum": ["one", "two"], xml: { attribute: true } }, role:{ type: "string" } }, xml: { name: "user" } } expect(sut(definition)).toEqual(expected) }) it("returns object with default values in attribute", function () { var expected = "\n\n\tstring\n" var definition = { type: "object", properties: { id: { type: "string", "default": "one", xml: { attribute: true } }, role:{ type: "string" } }, xml: { name: "user" } } expect(sut(definition)).toEqual(expected) }) it("returns object with default values in attribute", function () { var expected = "\n\n\tstring\n" var definition = { type: "object", properties: { id: { type: "string", "example": "one", xml: { attribute: true } }, role:{ type: "string" } }, xml: { name: "user" } } expect(sut(definition)).toEqual(expected) }) it("returns object with example value", function () { var expected = "\n\n\t42\n\tadmin\n" var definition = { type: "object", properties: { id: { type: "integer" }, role:{ type: "string" } }, xml: { name: "user" }, example: { id: 42, role: "admin" } } expect(sut(definition)).toEqual(expected) }) it("returns object with additional props", function () { var expected = "\n\n\tstring\n\tstring\n" var definition = { type: "object", properties: { dog: { type: "string" } }, additionalProperties: { type: "string" }, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns object with additional props =true", function () { var expected = "\n\n\tstring\n\tAnything can be here\n" var definition = { type: "object", properties: { dog: { type: "string" } }, additionalProperties: true, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) it("returns object with 2 properties with no type passed but properties", function () { var expected = "\n\n\tstring\n\t0\n" var definition = { properties: { alien: { type: "string" }, dog: { type: "integer" } }, xml: { name: "aliens" } } expect(sut(definition)).toEqual(expected) }) it("returns object with additional props with no type passed", function () { var expected = "\n\n\tstring\n" var definition = { additionalProperties: { type: "string" }, xml: { name: "animals" } } expect(sut(definition)).toEqual(expected) }) }) })