diff --git a/src/core/plugins/json-schema-5-samples/fn/index.js b/src/core/plugins/json-schema-5-samples/fn/index.js index e1fcf8623fe..490057bec56 100644 --- a/src/core/plugins/json-schema-5-samples/fn/index.js +++ b/src/core/plugins/json-schema-5-samples/fn/index.js @@ -19,6 +19,7 @@ const primitives = { "string_email": () => "user@example.com", "string_date-time": () => new Date().toISOString(), "string_date": () => new Date().toISOString().substring(0, 10), + "string_time": () => new Date().toISOString().substring(11, 23), "string_uuid": () => "3fa85f64-5717-4562-b3fc-2c963f66afa6", "string_hostname": () => "example.com", "string_ipv4": () => "198.51.100.42", diff --git a/test/unit/core/plugins/json-schema-5-samples/fn/index.js b/test/unit/core/plugins/json-schema-5-samples/fn/index.js index d3b6ea15c47..08bf2269691 100644 --- a/test/unit/core/plugins/json-schema-5-samples/fn/index.js +++ b/test/unit/core/plugins/json-schema-5-samples/fn/index.js @@ -8,6 +8,22 @@ import { } from "core/plugins/json-schema-5-samples/fn/index" describe("sampleFromSchema", () => { + const oriDate = Date + + beforeEach(() => { + // eslint-disable-next-line no-global-assign + Date = function () { + this.toISOString = function () { + return "2025-04-18T09:13:28.927Z" + } + } + }) + + afterEach(() => { + // eslint-disable-next-line no-global-assign + Date = oriDate + }) + it("handles Immutable.js objects for nested schemas", function () { let definition = fromJS({ "type": "object", @@ -341,12 +357,9 @@ describe("sampleFromSchema", () => { format: "date-time" } - // 0-20 chops off milliseconds - // necessary because test latency can cause failures - // it would be better to mock Date globally and expect a string - KS 11/18 - let expected = new Date().toISOString().substring(0, 20) + let expected = "2025-04-18T09:13:28.927Z" - expect(sampleFromSchema(definition)).toContain(expected) + expect(sampleFromSchema(definition)).toEqual(expected) }) it("returns example value for date property", () => { @@ -355,7 +368,18 @@ describe("sampleFromSchema", () => { format: "date" } - let expected = new Date().toISOString().substring(0, 10) + let expected = "2025-04-18" + + expect(sampleFromSchema(definition)).toEqual(expected) + }) + + it("returns example value for time property", () => { + let definition = { + type: "string", + format: "time" + } + + let expected = "09:13:28.927" expect(sampleFromSchema(definition)).toEqual(expected) })