forked from swagger-api/swagger-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature:
getEditorMetadata
helper (swagger-api#1789)
* feature: `getEditorMetadata` helper * add documentation * remove unused `throttle` reference
- Loading branch information
Showing
3 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
}) | ||
}) |