fix(examples): properly update memoized value in non-schema case (#6641)

This commit is contained in:
Tim Lai
2020-11-19 13:55:32 -08:00
committed by GitHub
parent 451da70f7e
commit d2ef8f31b9
2 changed files with 64 additions and 3 deletions

View File

@@ -332,14 +332,14 @@ export const inferSchema = (thing) => {
return thing // Hopefully this will have something schema like in it... `type` for example return thing // Hopefully this will have something schema like in it... `type` for example
} }
export const createXMLExample = (schema, config={}, o) => { export const createXMLExample = (schema, config, o) => {
const json = sampleFromSchemaGeneric(schema, config, o, true) const json = sampleFromSchemaGeneric(schema, config, o, true)
if (!json) { return } if (!json) { return }
return XML(json, { declaration: true, indent: "\t" }) return XML(json, { declaration: true, indent: "\t" })
} }
export const sampleFromSchema = (schema, config={}, o) => export const sampleFromSchema = (schema, config, o) =>
sampleFromSchemaGeneric(schema, config, o, false) sampleFromSchemaGeneric(schema, config, o, false)
export const memoizedCreateXMLExample = memoizee(createXMLExample) export const memoizedCreateXMLExample = memoizee(createXMLExample)

View File

@@ -1,5 +1,5 @@
import { fromJS } from "immutable" import { fromJS } from "immutable"
import { createXMLExample, sampleFromSchema } from "corePlugins/samples/fn" import { createXMLExample, sampleFromSchema, memoizedCreateXMLExample, memoizedSampleFromSchema } from "corePlugins/samples/fn"
describe("sampleFromSchema", () => { describe("sampleFromSchema", () => {
it("handles Immutable.js objects for nested schemas", function () { it("handles Immutable.js objects for nested schemas", function () {
@@ -1457,3 +1457,64 @@ describe("createXMLExample", function () {
}) })
}) })
}) })
describe("memoizedSampleFromSchema", () => {
it("should sequentially update memoized overrideExample", () => {
const definition = {
type: "object",
properties: {
foo: {
type: "string"
}
},
example: {
foo: null
}
}
const expected = {
foo: "override"
}
expect(memoizedSampleFromSchema(definition, {}, expected)).toEqual(expected)
const updatedExpected = {
foo: "cat"
}
expect(memoizedSampleFromSchema(definition, {}, updatedExpected)).toEqual(updatedExpected)
})
})
describe("memoizedCreateXMLExample", () => {
it("should sequentially update memoized overrideExample", () => {
const expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bar>\n\t<foo>override</foo>\n</bar>"
const definition = {
type: "object",
properties: {
foo: {
type: "string",
xml: {
name: "foo"
}
}
},
example: {
foo: null
},
xml: {
name: "bar"
}
}
const overrideExample = {
foo: "override"
}
expect(memoizedCreateXMLExample(definition, {}, overrideExample)).toEqual(expected)
const updatedOverrideExample = {
foo: "cat"
}
const updatedExpected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bar>\n\t<foo>cat</foo>\n</bar>"
expect(memoizedCreateXMLExample(definition, {}, updatedOverrideExample)).toEqual(updatedExpected)
})
})