Skip to content

Commit

Permalink
feature: getEditorMetadata helper (swagger-api#1789)
Browse files Browse the repository at this point in the history
* feature: `getEditorMetadata` helper
* add documentation
* remove unused `throttle` reference
  • Loading branch information
shockey authored Jun 12, 2018
1 parent 89810dc commit 1a10679
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### `getEditorMetadata`

`getEditorMetadata` is a method that allows you to get information about the Editor's state without reaching directly into the plugin system.

Example:

```js
const editor = SwaggerEditor({ /* your configuration here */ })

SwaggerEditor.getEditorMetadata()
```

Result:

```js
{
contentString: String,
contentObject: Object,
isValid: Boolean,
errors: Array,
}
```
15 changes: 15 additions & 0 deletions src/plugins/editor-metadata/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function(system) {
return {
rootInjects: {
getEditorMetadata() {
const allErrors = system.errSelectors.allErrors()
return {
contentString: system.specSelectors.specStr(),
contentObject: system.specSelectors.specJson().toJS(),
isValid: allErrors.size === 0,
errors: allErrors.toJS()
}
}
}
}
}
163 changes: 163 additions & 0 deletions test/plugins/editor-metadata/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import expect from "expect"
import SwaggerUi from "swagger-ui"
import EditorMetadataPlugin from "plugins/editor-metadata"

function getSystem(spec) {
return new Promise((resolve) => {
const system = SwaggerUi({
spec,
domNode: null,
presets: [
SwaggerUi.plugins.SpecIndex,
SwaggerUi.plugins.ErrIndex,
SwaggerUi.plugins.DownloadUrl,
SwaggerUi.plugins.SwaggerJsIndex,
],
initialState: {
layout: undefined
},
plugins: [
EditorMetadataPlugin,
() => ({
statePlugins: {
configs: {
actions: {
loaded: () => {
return {
type: "noop"
}
}
}
}
}
})
]
})

resolve(system)
})
}

describe("editor metadata plugin", function() {
this.timeout(10 * 1000)

it("should provide a `getEditorMetadata` method", async () => {
const spec = {}

const system = await getSystem(spec)

expect(system.getEditorMetadata).toBeA(Function)
})

it("should return JS object spec content from the `getEditorMetadata` method", async () => {
const spec = {
swagger: "2.0",
paths: {
"/": {
get: {
description: "hello there!",
responses: {
"200": {
description: "ok"
}
}
}
}
}
}

const system = await getSystem(spec)

expect(system.getEditorMetadata().contentString).toEqual(`{"swagger":"2.0","paths":{"/":{"get":{"description":"hello there!","responses":{"200":{"description":"ok"}}}}}}`)
expect(system.getEditorMetadata().contentObject).toEqual(spec)
})


it("should return YAML string spec content from the `getEditorMetadata` method", async () => {
const spec = `---
swagger: '2.0'
paths:
"/":
get:
description: hello there!
responses:
'200':
description: ok`

const system = await getSystem()

system.specActions.updateSpec(spec)

expect(system.getEditorMetadata().contentString).toEqual(spec)
expect(system.getEditorMetadata().contentObject).toEqual({
swagger: "2.0",
paths: {
"/": {
get: {
description: "hello there!",
responses: {
"200": {
description: "ok"
}
}
}
}
}
})
})

it("should return isValid for a valid spec", async () => {
const spec = {
swagger: "2.0",
paths: {
"/": {
get: {
description: "hello there!",
responses: {
"200": {
description: "ok"
}
}
}
}
}
}

const system = await getSystem(spec)

expect(system.getEditorMetadata().isValid).toBeA("boolean")
expect(system.getEditorMetadata().isValid).toBe(true)
})


it("should return isValid for an invalid spec", async () => {
const spec = {
swagger: "2.0",
paths: {
"/": {
get: {
description: "hello there!",
responses: {
"200": {
description: "ok"
}
}
}
}
}
}

const err = {
type: "spec",
message: "it's broken!"
}

const system = await getSystem(spec)

system.errActions.newSpecErr(err)

expect(system.getEditorMetadata().isValid).toBeA("boolean")
expect(system.getEditorMetadata().isValid).toBe(false)
expect(system.getEditorMetadata().errors).toEqual([err])
})
})

0 comments on commit 1a10679

Please sign in to comment.