From d2ef8f31b998b3f7cc0e075e76bda0e2c0e782c2 Mon Sep 17 00:00:00 2001 From: Tim Lai Date: Thu, 19 Nov 2020 13:55:32 -0800 Subject: [PATCH] fix(examples): properly update memoized value in non-schema case (#6641) --- src/core/plugins/samples/fn.js | 4 +- test/unit/core/plugins/samples/fn.js | 63 +++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/core/plugins/samples/fn.js b/src/core/plugins/samples/fn.js index e7d8d47f..751b6166 100644 --- a/src/core/plugins/samples/fn.js +++ b/src/core/plugins/samples/fn.js @@ -332,14 +332,14 @@ export const inferSchema = (thing) => { 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) if (!json) { return } return XML(json, { declaration: true, indent: "\t" }) } -export const sampleFromSchema = (schema, config={}, o) => +export const sampleFromSchema = (schema, config, o) => sampleFromSchemaGeneric(schema, config, o, false) export const memoizedCreateXMLExample = memoizee(createXMLExample) diff --git a/test/unit/core/plugins/samples/fn.js b/test/unit/core/plugins/samples/fn.js index 353cc0e3..ae95076b 100644 --- a/test/unit/core/plugins/samples/fn.js +++ b/test/unit/core/plugins/samples/fn.js @@ -1,5 +1,5 @@ import { fromJS } from "immutable" -import { createXMLExample, sampleFromSchema } from "corePlugins/samples/fn" +import { createXMLExample, sampleFromSchema, memoizedCreateXMLExample, memoizedSampleFromSchema } from "corePlugins/samples/fn" describe("sampleFromSchema", () => { 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 = "\n\n\toverride\n" + + 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 = "\n\n\tcat\n" + expect(memoizedCreateXMLExample(definition, {}, updatedOverrideExample)).toEqual(updatedExpected) + }) +})