helpers.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { fromJS } from "immutable"
  2. import { isOAS3, isSwagger2 } from "corePlugins/oas3/helpers"
  3. const isOAS3Shorthand = (version) => isOAS3(fromJS({
  4. openapi: version
  5. }))
  6. const isSwagger2Shorthand = (version) => isSwagger2(fromJS({
  7. swagger: version
  8. }))
  9. describe("isOAS3", function () {
  10. it("should recognize valid OAS3 version values", function () {
  11. expect(isOAS3Shorthand("3.0.0")).toEqual(true)
  12. expect(isOAS3Shorthand("3.0.1")).toEqual(true)
  13. expect(isOAS3Shorthand("3.0.11111")).toEqual(true)
  14. expect(isOAS3Shorthand("3.0.0-rc0")).toEqual(true)
  15. })
  16. it("should fail for invalid OAS3 version values", function () {
  17. expect(isOAS3Shorthand("3.0")).toEqual(false)
  18. expect(isOAS3Shorthand("3.0.")).toEqual(false)
  19. expect(isOAS3Shorthand("2.0")).toEqual(false)
  20. })
  21. it("should gracefully fail for non-string values", function () {
  22. expect(isOAS3Shorthand(3.0)).toEqual(false)
  23. expect(isOAS3Shorthand(3)).toEqual(false)
  24. expect(isOAS3Shorthand({})).toEqual(false)
  25. expect(isOAS3Shorthand(null)).toEqual(false)
  26. })
  27. it("should gracefully fail when `openapi` field is missing", function () {
  28. expect(isOAS3(fromJS({
  29. openApi: "3.0.0"
  30. }))).toEqual(false)
  31. expect(isOAS3Shorthand(null)).toEqual(false)
  32. })
  33. })
  34. describe("isSwagger2", function () {
  35. it("should recognize valid Swagger 2.0 version values", function () {
  36. expect(isSwagger2Shorthand("2.0")).toEqual(true)
  37. expect(isSwagger2Shorthand("2.0-rc0")).toEqual(true)
  38. })
  39. it("should fail for invalid Swagger 2.0 version values", function () {
  40. expect(isSwagger2Shorthand("3.0")).toEqual(false)
  41. expect(isSwagger2Shorthand("3.0.")).toEqual(false)
  42. expect(isSwagger2Shorthand("2.1")).toEqual(false)
  43. expect(isSwagger2Shorthand("1.2")).toEqual(false)
  44. expect(isSwagger2Shorthand("2")).toEqual(false)
  45. })
  46. it("should gracefully fail for non-string values", function () {
  47. expect(isSwagger2Shorthand(2.0)).toEqual(false)
  48. expect(isSwagger2Shorthand(2)).toEqual(false)
  49. expect(isSwagger2Shorthand({})).toEqual(false)
  50. expect(isSwagger2Shorthand(null)).toEqual(false)
  51. })
  52. it("should gracefully fail when `swagger` field is missing", function () {
  53. expect(isSwagger2(fromJS({
  54. Swagger: "2.0"
  55. }))).toEqual(false)
  56. })
  57. })