From 2edac8fd9c88dce8be3e0f4b3ef846fee8316baa Mon Sep 17 00:00:00 2001 From: Timothy Li Date: Wed, 13 Mar 2019 11:58:49 +0800 Subject: [PATCH 1/6] Add support for microsecond and alternative iso datetime formats --- .../gatsby/src/schema/types/__tests__/date.js | 495 ++++++++++++++++++ packages/gatsby/src/schema/types/date.js | 34 ++ 2 files changed, 529 insertions(+) create mode 100644 packages/gatsby/src/schema/types/__tests__/date.js diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js new file mode 100644 index 0000000000000..efa6b6d8c3bfd --- /dev/null +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -0,0 +1,495 @@ +const { store } = require(`../../../redux`) +const { build } = require(`../..`) + +const { createSchemaComposer } = require(`../../schema-composer`) +const { GraphQLDate, dateResolver, isDate } = require(`../date`) + +// Timestamps grabbed from https://github.com/moment/moment/blob/2e2a5b35439665d4b0200143d808a7c26d6cd30f/src/test/moment/is_valid.js + +describe(`isDate`, () => { + beforeEach(async () => { + // pass + }) + + it(`supports valid ISO 8601 datetimes`, async () => { + const tests = [ + '1970', + '2019', + '1970-01', + '2019-01', + '1970-01-01', + '2010-01-01', + '2010-01-30', + '19700101', + '20100101', + '20100130', + '2010-01-30T23+00:00', + '2010-01-30T23:59+00:00', + '2010-01-30T23:59:59+00:00', + '2010-01-30T23:59:59.999+00:00', + '2010-01-30T23:59:59.999-07:00', + '2010-01-30T00:00:00.000+07:00', + '2010-01-30T23:59:59.999-07', + '2010-01-30T00:00:00.000+07', + '1970-01-01T00:00:00.000Z', + '2012-04-01T00:00:00-05:00', + '2012-11-12T00:00:00+01:00', + ] + let i + for (i = 0; i < tests.length; i++) { + expect(isDate(tests[i])).toEqual(true) + } + }) + + it(`supports ISO datetimes without T`, async () => { + const tests = [ + '2010-01-30 23+00:00', + '2010-01-30 23:59+00:00', + '2010-01-30 23:59:59+00:00', + '2010-01-30 23:59:59.999+00:00', + '2010-01-30 23:59:59.999-07:00', + '2010-01-30 00:00:00.000+07:00', + '2010-01-30 23:59:59.999-07', + '2010-01-30 00:00:00.000+07', + '1970-01-01 00:00:00.000Z', + '2012-04-01 00:00:00-05:00', + '2012-11-12 00:00:00+01:00', + ] + let i + for (i = 0; i < tests.length; i++) { + expect(isDate(tests[i])).toEqual(true) + } + }) + + it(`supports valid ISO 8601 week dates`, async () => { + const tests = [ + '1970-W31', + '2006-W01', + '1970W31', + '2009-W53-7', + '2009W537', + ] + let i + for (i = 0; i < tests.length; i++) { + expect(isDate(tests[i])).toEqual(true) + } + }) + + it(`supports valid ISO 8601 ordinal dates`, async () => { + const tests = [ + '1970-334', + '1970334', + '2090-001', + '2090001', + ] + let i + for (i = 0; i < tests.length; i++) { + expect(isDate(tests[i])).toEqual(true) + } + }) + + it(`supports microsecond precision`, async () => { + expect(isDate("2018-08-31T23:25:16.019345+02:00")).toEqual(true) + expect(isDate("2018-08-31T23:25:16.019345Z")).toEqual(true) + }) + + it(`supports nanosecond precision`, async () => { + expect(isDate("2018-08-31T23:25:16.019345123+02:00")).toEqual(true) + expect(isDate("2018-08-31T23:25:16.019345123Z")).toEqual(true) + }) + + it.skip(`does not support precision beyond 9 digits`, async () => { + // This seems to pass + expect(isDate("2018-08-31T23:25:16.01234567899+02:00")).toEqual(false) + }) + + + it(`does not support invalid string iso 8601`, async () => { + const tests = [ + '2010-00-00', + '2010-01-00', + '2010-01-40', + '2010-01-01T24:01', // 24:00:00 is actually valid + '2010-01-01T23:60', + '2010-01-01T23:59:60', + '2010-01-40T23:59:59.9999' + ] + let i + for (i = 0; i < tests.length; i++) { + expect(isDate(tests[i])).toEqual(false) + } + }) + + + it(`does not support invalid string iso 8601 + timezone`, async () => { + const tests = [ + '2010-00-00T+00:00', + '2010-01-00T+00:00', + '2010-01-40T+00:00', + '2010-01-40T24:01+00:00', + '2010-01-40T23:60+00:00', + '2010-01-40T23:59:60+00:00', + '2010-01-40T23:59:59.9999+00:00', + '2010-01-40T23:59:59,9999+00:00', + '2012-04-01T00:00:00-5:00', // should be -05:00 + '2012-04-01T00:00:00+1:00', // should be +01:00 + ] + let i + for (i = 0; i < tests.length; i++) { + expect(isDate(tests[i])).toEqual(false) + } + }) + + + it(`does not support invalid timestamps`, async () => { + const tests = [ + undefined, + 'undefined', + null, + 'null', + [], + {}, + '', + ' ', + '2012-04-01T00:basketball' + ] + let i + for (i = 0; i < tests.length; i++) { + expect(isDate(tests[i])).toEqual(false) + } + }) + + it.skip(`supports unix timestamps`, async () => { + // Not sure if in original scope + const tests = [ + 1371065286, + 1379066897.0, + 1379066897.7, + 1379066897.00, + 1379066897.07, + 1379066897.17, + 1379066897.000, + 1379066897.007, + 1379066897.017, + 1379066897.157, + '1371065286', + '1379066897.', + '1379066897.0', + '1379066897.7', + '1379066897.00', + '1379066897.07', + '1379066897.17', + '1379066897.000', + '1379066897.007', + '1379066897.017', + '1379066897.157', + ] + let i + for (i = 0; i < tests.length; i++) { + expect(isDate(tests[i])).toEqual(true) + } + }) + +}) + + +const nodes = [ + { + id: `id1`, + internal: { type: `Test` }, + testDate: new Date(), + explicitValidDate: `2010-01-30T23:59:59.999-07:00`, + inferredValidDate: `1970-01-01T00:00:00.000Z`, + validYYYY: `1970`, + validYYMM: `2019-01`, + validYYMMDD: `2010-01-30`, + validYYMMDDNoDash: `20100101`, + validISO1: `2010-01-30T23:59:59.999+00:00`, + validISO2: `2010-01-30T23:59:59.999-07:00`, + validISO3: `2010-01-30T00:00:00.000+07:00`, + validISO4: `2010-01-30T23:59:59.999-07`, + validISO5: `2010-01-30T00:00:00.000+07`, + validISO6: `2010-01-30 00:00:00.000Z`, + validISO7: `1970-01-01T00:00:00.000Z`, + validISO8: `2012-04-01T00:00:00-05:00`, + validISO9: `2012-11-12T00:00:00+01:00`, + + validOrdinal1: `1970-334`, + validOrdinal2: `1970334`, + validOrdinal3: `2090-001`, + validOrdinal4: `2090001`, + + validWeek1: `1970-W31`, + validWeek2: `2006-W01`, + validWeek3: `1970W31`, + validWeek4: `2009-W53-7`, + validWeek5: `2009W537`, + + validMicrosecond1: `2018-08-31T23:25:16.019345+02:00`, + validMicrosecond2: `2018-08-31T23:25:16.019345Z`, + + validNanosecond1: `2018-08-31T23:25:16.019345123+02:00`, + validNanosecond2: `2018-08-31T23:25:16.019345123Z`, + + invalidHighPrecision: `2018-08-31T23:25:16.01234567899993+02:00`, + + invalidDate1: `2010-00-00`, + invalidDate2: `2010-01-00`, + invalidDate3: `2010-01-40`, + invalidDate4: `2010-01-01T24:01`, // 24:00:00 is actually valid + invalidDate5: `2010-01-01T23:60`, + invalidDate6: `2010-01-01T23:59:60`, + invalidDate7: `2010-01-40T23:59:59.9999`, + + invalidDate8: undefined, + invalidDate9: 'undefined', + invalidDate10: null, + invalidDate11: 'null', + invalidDate12: [], + invalidDate13: {}, + invalidDate14: '', + invalidDate15: ' ', + invalidDate16: '2012-04-01T00:basketball' + + }, +] + +describe(`dateResolver`, () => { + beforeEach(() => { + store.dispatch({ type: `DELETE_CACHE` }) + nodes.forEach(node => + store.dispatch({ type: `CREATE_NODE`, payload: { ...node } }) + ) + }) + + const buildTestSchema = async ({ + infer = true, + addDefaultResolvers = true, + }) => { + const inferDirective = infer ? `@infer` : `@dontInfer` + const noDefaultResolvers = addDefaultResolvers ? `false` : `true` + const typeDefs = [ + ` + type Test implements Node ${inferDirective}(noDefaultResolvers: ${noDefaultResolvers}) { + testDate: Date + explicitValidDate: Date + + invalidHighPrecision: Date + invalidDate8: Date + invalidDate9: Date + invalidDate10: Date + invalidDate11: Date + invalidDate12: Date + invalidDate13: Date + invalidDate14: Date + invalidDate15: Date + invalidDate16: Date + + }`, + ] + typeDefs.forEach(def => + store.dispatch({ type: `CREATE_TYPES`, payload: def }) + ) + + await build({}) + return store.getState().schema + } + + it(`can properly resolve datetimes`, async () => { + + const schema = await buildTestSchema({}) + const fields = schema.getType(`Test`).getFields() + + expect(fields.testDate.resolve).toBeDefined() + expect(fields.explicitValidDate.resolve).toBeDefined() + expect(fields.inferredValidDate.resolve).toBeDefined() + expect(fields.validYYYY.resolve).toBeDefined() + expect(fields.validYYMM.resolve).toBeDefined() + expect(fields.validYYMMDD.resolve).toBeDefined() + expect(fields.validYYMMDDNoDash.resolve).toBeDefined() + + expect(fields.validISO1.resolve).toBeDefined() + expect(fields.validISO2.resolve).toBeDefined() + expect(fields.validISO3.resolve).toBeDefined() + expect(fields.validISO4.resolve).toBeDefined() + expect(fields.validISO5.resolve).toBeDefined() + expect(fields.validISO6.resolve).toBeDefined() + expect(fields.validISO7.resolve).toBeDefined() + expect(fields.validISO8.resolve).toBeDefined() + expect(fields.validISO9.resolve).toBeDefined() + + expect(fields.validOrdinal1.resolve).toBeDefined() + expect(fields.validOrdinal2.resolve).toBeDefined() + expect(fields.validOrdinal3.resolve).toBeDefined() + expect(fields.validOrdinal4.resolve).toBeDefined() + + expect(fields.validWeek1.resolve).toBeDefined() + expect(fields.validWeek2.resolve).toBeDefined() + expect(fields.validWeek3.resolve).toBeDefined() + expect(fields.validWeek4.resolve).toBeDefined() + expect(fields.validWeek5.resolve).toBeDefined() + + expect(fields.validMicrosecond1.resolve).toBeDefined() + expect(fields.validMicrosecond2.resolve).toBeDefined() + + expect(fields.validNanosecond1.resolve).toBeDefined() + expect(fields.validNanosecond2.resolve).toBeDefined() + + // expect(fields.invalidHighPrecision.resolve).toBeDefined() + + expect(fields.invalidDate1.resolve).toBeUndefined() + expect(fields.invalidDate2.resolve).toBeUndefined() + expect(fields.invalidDate3.resolve).toBeUndefined() + expect(fields.invalidDate4.resolve).toBeUndefined() + expect(fields.invalidDate5.resolve).toBeUndefined() + expect(fields.invalidDate6.resolve).toBeUndefined() + expect(fields.invalidDate7.resolve).toBeUndefined() + expect(fields.invalidDate8.resolve).toBeUndefined() + expect(fields.invalidDate9.resolve).toBeUndefined() + expect(fields.invalidDate10.resolve).toBeUndefined() + expect(fields.invalidDate11.resolve).toBeUndefined() + expect(fields.invalidDate12.resolve).toBeUndefined() + expect(fields.invalidDate13.resolve).toBeUndefined() + expect(fields.invalidDate14.resolve).toBeUndefined() + expect(fields.invalidDate15.resolve).toBeUndefined() + expect(fields.invalidDate16.resolve).toBeUndefined() + + }) + + it(`can properly handle basic iso formats`, async () => { + const schema = await buildTestSchema({}) + const fields = schema.getType(`Test`).getFields() + + const validDates = [ + `2018-01-28T23:59:59.999-07:00`, + `2018-01-29T00:00:00.000Z`, + `2018-01-29`, + `20180129`, + `2018-01-29T23:59:59.999+00:00`, + `2018-01-28T19:59:59.999-07:00`, + `2018-01-30T06:00:00.001+07:00`, + `2018-01-28 17:00:00.001-07`, + `2018-01-30 04:00:00.001+07`, + `2018-01-28T17:00:00.001-07`, + `2018-01-30T04:00:00.001+07`, + `2018-01-29 00:00:00.001Z`, + `2018-01-29T00:00:00.001Z`, + `2018-01-28 23:00:00-05:00`, + `2018-01-29 23:00:00+01:00`, + `2018-01-28T23:00:00-05:00`, + `2018-01-29T23:00:00+01:00`, + `2018-029`, + `2018029`, + `2018-W05`, + `2018W05`, + `2018-W05-1`, + ] + + let i + for (i = 0; i < validDates.length; i++) { + expect( + fields[`testDate`].resolve( + { date: validDates[i] }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Jan 29, 2018`) + + } + + }) + + it(`can properly handle microsecond and nanosecond`, async () => { + const schema = await buildTestSchema({}) + const fields = schema.getType(`Test`).getFields() + + const validDates = [ + `2018-01-29T23:25:16.019345+02:00`, + `2018-01-29T23:25:16.019345Z`, + // Seems to not require nanosecond definition to not fail + `2018-01-29T23:25:16.019345123+02:00`, + `2018-01-29T23:25:16.019345123Z`, + ] + + let i + for (i = 0; i < validDates.length; i++) { + expect( + fields[`testDate`].resolve( + { date: validDates[i] }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Jan 29, 2018`) + + } + + }) + + it.skip(`errors on n second precision`, async () => { + const schema = await buildTestSchema({}) + const fields = schema.getType(`Test`).getFields() + + const invalidDates = [ + `2018-08-31T23:25:16.01234567899993+02:00`, + ] + + let i + for (i = 0; i < invalidDates.length; i++) { + expect( + fields[`testDate`].resolve( + { date: invalidDates[i] }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Invalid date`) + } + + }) + + it(`errors on invalid datetimes`, async () => { + const schema = await buildTestSchema({}) + const fields = schema.getType(`Test`).getFields() + + const invalidDates = [ + // `2018-08-31T23:25:16.01234567899993+02:00`, + `2010-00-00`, + `2010-01-00`, + `2010-01-40`, + `2010-01-01T24:01`, + `2010-01-01T23:60`, + `2010-01-01T23:59:60`, + `2010-01-40T23:59:59.9999`, + ] + + let i + for (i = 0; i < invalidDates.length; i++) { + expect( + fields[`testDate`].resolve( + { date: invalidDates[i] }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Invalid date`) + } + + }) + +}) + + + + + + diff --git a/packages/gatsby/src/schema/types/date.js b/packages/gatsby/src/schema/types/date.js index fedd02fa4a45b..a2c2b30d40d40 100644 --- a/packages/gatsby/src/schema/types/date.js +++ b/packages/gatsby/src/schema/types/date.js @@ -21,6 +21,23 @@ const ISO_8601_FORMAT = [ `YYYY-MM-DDTHHmmss`, `YYYY-MM-DDTHH:mm:ss.SSS`, `YYYY-MM-DDTHHmmss.SSS`, + `YYYY-MM-DDTHH:mm:ss.SSSSSS`, + `YYYY-MM-DDTHHmmss.SSSSSS`, + // `YYYY-MM-DDTHH:mm:ss.SSSSSSSSS`, + // `YYYY-MM-DDTHHmmss.SSSSSSSSS`, + + // Local Time (Omit T) + `YYYY-MM-DD HH`, + `YYYY-MM-DD HH:mm`, + `YYYY-MM-DD HHmm`, + `YYYY-MM-DD HH:mm:ss`, + `YYYY-MM-DD HHmmss`, + `YYYY-MM-DD HH:mm:ss.SSS`, + `YYYY-MM-DD HHmmss.SSS`, + `YYYY-MM-DD HH:mm:ss.SSSSSS`, + `YYYY-MM-DD HHmmss.SSSSSS`, + // `YYYY-MM-DD HH:mm:ss.SSSSSSSSS`, + // `YYYY-MM-DD HHmmss.SSSSSSSSS`, // Coordinated Universal Time (UTC) `YYYY-MM-DDTHHZ`, @@ -30,6 +47,23 @@ const ISO_8601_FORMAT = [ `YYYY-MM-DDTHHmmssZ`, `YYYY-MM-DDTHH:mm:ss.SSSZ`, `YYYY-MM-DDTHHmmss.SSSZ`, + `YYYY-MM-DDTHH:mm:ss.SSSSSSZ`, + `YYYY-MM-DDTHHmmss.SSSSSSZ`, + // `YYYY-MM-DDTHH:mm:ss.SSSSSSSSSZ`, + // `YYYY-MM-DDTHHmmss.SSSSSSSSSZ`, + + // // Coordinated Universal Time (UTC) (Omit T) + `YYYY-MM-DD HHZ`, + `YYYY-MM-DD HH:mmZ`, + `YYYY-MM-DD HHmmZ`, + `YYYY-MM-DD HH:mm:ssZ`, + `YYYY-MM-DD HHmmssZ`, + `YYYY-MM-DD HH:mm:ss.SSSZ`, + `YYYY-MM-DD HHmmss.SSSZ`, + `YYYY-MM-DD HH:mm:ss.SSSSSSZ`, + `YYYY-MM-DD HHmmss.SSSSSSZ`, + // `YYYY-MM-DD HH:mm:ss.SSSSSSSSSZ`, + // `YYYY-MM-DD HHmmss.SSSSSSSSSZ`, `YYYY-[W]WW`, `YYYY[W]WW`, From 56c13aa7ed877d6b9481158b2e46aabc58a2cf59 Mon Sep 17 00:00:00 2001 From: Timothy Li Date: Wed, 13 Mar 2019 13:52:25 +0800 Subject: [PATCH 2/6] Fix lint issues --- .../gatsby/src/schema/types/__tests__/date.js | 326 ++++++++---------- 1 file changed, 142 insertions(+), 184 deletions(-) diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index efa6b6d8c3bfd..5291f687607e0 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -1,8 +1,6 @@ const { store } = require(`../../../redux`) const { build } = require(`../..`) - -const { createSchemaComposer } = require(`../../schema-composer`) -const { GraphQLDate, dateResolver, isDate } = require(`../date`) +const { isDate } = require(`../date`) // Timestamps grabbed from https://github.com/moment/moment/blob/2e2a5b35439665d4b0200143d808a7c26d6cd30f/src/test/moment/is_valid.js @@ -13,186 +11,170 @@ describe(`isDate`, () => { it(`supports valid ISO 8601 datetimes`, async () => { const tests = [ - '1970', - '2019', - '1970-01', - '2019-01', - '1970-01-01', - '2010-01-01', - '2010-01-30', - '19700101', - '20100101', - '20100130', - '2010-01-30T23+00:00', - '2010-01-30T23:59+00:00', - '2010-01-30T23:59:59+00:00', - '2010-01-30T23:59:59.999+00:00', - '2010-01-30T23:59:59.999-07:00', - '2010-01-30T00:00:00.000+07:00', - '2010-01-30T23:59:59.999-07', - '2010-01-30T00:00:00.000+07', - '1970-01-01T00:00:00.000Z', - '2012-04-01T00:00:00-05:00', - '2012-11-12T00:00:00+01:00', + `1970`, + `2019`, + `1970-01`, + `2019-01`, + `1970-01-01`, + `2010-01-01`, + `2010-01-30`, + `19700101`, + `20100101`, + `20100130`, + `2010-01-30T23+00:00`, + `2010-01-30T23:59+00:00`, + `2010-01-30T23:59:59+00:00`, + `2010-01-30T23:59:59.999+00:00`, + `2010-01-30T23:59:59.999-07:00`, + `2010-01-30T00:00:00.000+07:00`, + `2010-01-30T23:59:59.999-07`, + `2010-01-30T00:00:00.000+07`, + `1970-01-01T00:00:00.000Z`, + `2012-04-01T00:00:00-05:00`, + `2012-11-12T00:00:00+01:00`, ] let i for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) + expect(isDate(tests[i])).toEqual(true) } }) it(`supports ISO datetimes without T`, async () => { const tests = [ - '2010-01-30 23+00:00', - '2010-01-30 23:59+00:00', - '2010-01-30 23:59:59+00:00', - '2010-01-30 23:59:59.999+00:00', - '2010-01-30 23:59:59.999-07:00', - '2010-01-30 00:00:00.000+07:00', - '2010-01-30 23:59:59.999-07', - '2010-01-30 00:00:00.000+07', - '1970-01-01 00:00:00.000Z', - '2012-04-01 00:00:00-05:00', - '2012-11-12 00:00:00+01:00', + `2010-01-30 23+00:00`, + `2010-01-30 23:59+00:00`, + `2010-01-30 23:59:59+00:00`, + `2010-01-30 23:59:59.999+00:00`, + `2010-01-30 23:59:59.999-07:00`, + `2010-01-30 00:00:00.000+07:00`, + `2010-01-30 23:59:59.999-07`, + `2010-01-30 00:00:00.000+07`, + `1970-01-01 00:00:00.000Z`, + `2012-04-01 00:00:00-05:00`, + `2012-11-12 00:00:00+01:00`, ] let i for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) + expect(isDate(tests[i])).toEqual(true) } }) it(`supports valid ISO 8601 week dates`, async () => { - const tests = [ - '1970-W31', - '2006-W01', - '1970W31', - '2009-W53-7', - '2009W537', - ] + const tests = [`1970-W31`, `2006-W01`, `1970W31`, `2009-W53-7`, `2009W537`] let i for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) + expect(isDate(tests[i])).toEqual(true) } }) it(`supports valid ISO 8601 ordinal dates`, async () => { - const tests = [ - '1970-334', - '1970334', - '2090-001', - '2090001', - ] + const tests = [`1970-334`, `1970334`, `2090-001`, `2090001`] let i for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) + expect(isDate(tests[i])).toEqual(true) } }) it(`supports microsecond precision`, async () => { - expect(isDate("2018-08-31T23:25:16.019345+02:00")).toEqual(true) - expect(isDate("2018-08-31T23:25:16.019345Z")).toEqual(true) + expect(isDate(`2018-08-31T23:25:16.019345+02:00`)).toEqual(true) + expect(isDate(`2018-08-31T23:25:16.019345Z`)).toEqual(true) }) it(`supports nanosecond precision`, async () => { - expect(isDate("2018-08-31T23:25:16.019345123+02:00")).toEqual(true) - expect(isDate("2018-08-31T23:25:16.019345123Z")).toEqual(true) + expect(isDate(`2018-08-31T23:25:16.019345123+02:00`)).toEqual(true) + expect(isDate(`2018-08-31T23:25:16.019345123Z`)).toEqual(true) }) it.skip(`does not support precision beyond 9 digits`, async () => { // This seems to pass - expect(isDate("2018-08-31T23:25:16.01234567899+02:00")).toEqual(false) + expect(isDate(`2018-08-31T23:25:16.01234567899+02:00`)).toEqual(false) }) - it(`does not support invalid string iso 8601`, async () => { const tests = [ - '2010-00-00', - '2010-01-00', - '2010-01-40', - '2010-01-01T24:01', // 24:00:00 is actually valid - '2010-01-01T23:60', - '2010-01-01T23:59:60', - '2010-01-40T23:59:59.9999' + `2010-00-00`, + `2010-01-00`, + `2010-01-40`, + `2010-01-01T24:01`, // 24:00:00 is actually valid + `2010-01-01T23:60`, + `2010-01-01T23:59:60`, + `2010-01-40T23:59:59.9999`, ] let i for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(false) + expect(isDate(tests[i])).toEqual(false) } }) - it(`does not support invalid string iso 8601 + timezone`, async () => { const tests = [ - '2010-00-00T+00:00', - '2010-01-00T+00:00', - '2010-01-40T+00:00', - '2010-01-40T24:01+00:00', - '2010-01-40T23:60+00:00', - '2010-01-40T23:59:60+00:00', - '2010-01-40T23:59:59.9999+00:00', - '2010-01-40T23:59:59,9999+00:00', - '2012-04-01T00:00:00-5:00', // should be -05:00 - '2012-04-01T00:00:00+1:00', // should be +01:00 + `2010-00-00T+00:00`, + `2010-01-00T+00:00`, + `2010-01-40T+00:00`, + `2010-01-40T24:01+00:00`, + `2010-01-40T23:60+00:00`, + `2010-01-40T23:59:60+00:00`, + `2010-01-40T23:59:59.9999+00:00`, + `2010-01-40T23:59:59,9999+00:00`, + `2012-04-01T00:00:00-5:00`, // should be -05:00 + `2012-04-01T00:00:00+1:00`, // should be +01:00 ] let i for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(false) + expect(isDate(tests[i])).toEqual(false) } }) - it(`does not support invalid timestamps`, async () => { const tests = [ - undefined, - 'undefined', - null, - 'null', - [], - {}, - '', - ' ', - '2012-04-01T00:basketball' + undefined, + `undefined`, + null, + `null`, + [], + {}, + ``, + ` `, + `2012-04-01T00:basketball`, ] let i for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(false) + expect(isDate(tests[i])).toEqual(false) } }) it.skip(`supports unix timestamps`, async () => { // Not sure if in original scope const tests = [ - 1371065286, - 1379066897.0, - 1379066897.7, - 1379066897.00, - 1379066897.07, - 1379066897.17, - 1379066897.000, - 1379066897.007, - 1379066897.017, - 1379066897.157, - '1371065286', - '1379066897.', - '1379066897.0', - '1379066897.7', - '1379066897.00', - '1379066897.07', - '1379066897.17', - '1379066897.000', - '1379066897.007', - '1379066897.017', - '1379066897.157', + 1371065286, + 1379066897.0, + 1379066897.7, + 1379066897.0, + 1379066897.07, + 1379066897.17, + 1379066897.0, + 1379066897.007, + 1379066897.017, + 1379066897.157, + `1371065286`, + `1379066897.`, + `1379066897.0`, + `1379066897.7`, + `1379066897.00`, + `1379066897.07`, + `1379066897.17`, + `1379066897.000`, + `1379066897.007`, + `1379066897.017`, + `1379066897.157`, ] let i for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) + expect(isDate(tests[i])).toEqual(true) } }) - }) - const nodes = [ { id: `id1`, @@ -236,21 +218,20 @@ const nodes = [ invalidDate1: `2010-00-00`, invalidDate2: `2010-01-00`, invalidDate3: `2010-01-40`, - invalidDate4: `2010-01-01T24:01`, // 24:00:00 is actually valid + invalidDate4: `2010-01-01T24:01`, // 24:00:00 is actually valid invalidDate5: `2010-01-01T23:60`, invalidDate6: `2010-01-01T23:59:60`, invalidDate7: `2010-01-40T23:59:59.9999`, invalidDate8: undefined, - invalidDate9: 'undefined', + invalidDate9: `undefined`, invalidDate10: null, - invalidDate11: 'null', + invalidDate11: `null`, invalidDate12: [], invalidDate13: {}, - invalidDate14: '', - invalidDate15: ' ', - invalidDate16: '2012-04-01T00:basketball' - + invalidDate14: ``, + invalidDate15: ` `, + invalidDate16: `2012-04-01T00:basketball`, }, ] @@ -273,7 +254,6 @@ describe(`dateResolver`, () => { type Test implements Node ${inferDirective}(noDefaultResolvers: ${noDefaultResolvers}) { testDate: Date explicitValidDate: Date - invalidHighPrecision: Date invalidDate8: Date invalidDate9: Date @@ -284,7 +264,6 @@ describe(`dateResolver`, () => { invalidDate14: Date invalidDate15: Date invalidDate16: Date - }`, ] typeDefs.forEach(def => @@ -296,7 +275,6 @@ describe(`dateResolver`, () => { } it(`can properly resolve datetimes`, async () => { - const schema = await buildTestSchema({}) const fields = schema.getType(`Test`).getFields() @@ -334,9 +312,7 @@ describe(`dateResolver`, () => { expect(fields.validNanosecond1.resolve).toBeDefined() expect(fields.validNanosecond2.resolve).toBeDefined() - // expect(fields.invalidHighPrecision.resolve).toBeDefined() - expect(fields.invalidDate1.resolve).toBeUndefined() expect(fields.invalidDate2.resolve).toBeUndefined() expect(fields.invalidDate3.resolve).toBeUndefined() @@ -353,7 +329,6 @@ describe(`dateResolver`, () => { expect(fields.invalidDate14.resolve).toBeUndefined() expect(fields.invalidDate15.resolve).toBeUndefined() expect(fields.invalidDate16.resolve).toBeUndefined() - }) it(`can properly handle basic iso formats`, async () => { @@ -387,25 +362,22 @@ describe(`dateResolver`, () => { let i for (i = 0; i < validDates.length; i++) { - expect( - fields[`testDate`].resolve( - { date: validDates[i] }, - { formatString: `MMM DD, YYYY` }, - {}, - { - fieldName: `date`, - } - ) - ).toEqual(`Jan 29, 2018`) - + expect( + fields[`testDate`].resolve( + { date: validDates[i] }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Jan 29, 2018`) } - }) it(`can properly handle microsecond and nanosecond`, async () => { const schema = await buildTestSchema({}) const fields = schema.getType(`Test`).getFields() - const validDates = [ `2018-01-29T23:25:16.019345+02:00`, `2018-01-29T23:25:16.019345Z`, @@ -416,43 +388,37 @@ describe(`dateResolver`, () => { let i for (i = 0; i < validDates.length; i++) { - expect( - fields[`testDate`].resolve( - { date: validDates[i] }, - { formatString: `MMM DD, YYYY` }, - {}, - { - fieldName: `date`, - } - ) - ).toEqual(`Jan 29, 2018`) - + expect( + fields[`testDate`].resolve( + { date: validDates[i] }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Jan 29, 2018`) } - }) it.skip(`errors on n second precision`, async () => { const schema = await buildTestSchema({}) const fields = schema.getType(`Test`).getFields() - - const invalidDates = [ - `2018-08-31T23:25:16.01234567899993+02:00`, - ] + const invalidDates = [`2018-08-31T23:25:16.01234567899993+02:00`] let i for (i = 0; i < invalidDates.length; i++) { - expect( - fields[`testDate`].resolve( - { date: invalidDates[i] }, - { formatString: `MMM DD, YYYY` }, - {}, - { - fieldName: `date`, - } - ) - ).toEqual(`Invalid date`) + expect( + fields[`testDate`].resolve( + { date: invalidDates[i] }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Invalid date`) } - }) it(`errors on invalid datetimes`, async () => { @@ -472,24 +438,16 @@ describe(`dateResolver`, () => { let i for (i = 0; i < invalidDates.length; i++) { - expect( - fields[`testDate`].resolve( - { date: invalidDates[i] }, - { formatString: `MMM DD, YYYY` }, - {}, - { - fieldName: `date`, - } - ) - ).toEqual(`Invalid date`) + expect( + fields[`testDate`].resolve( + { date: invalidDates[i] }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Invalid date`) } - }) - }) - - - - - - From 45d1d32989ac5474aa2b2df818475f910ce3edb7 Mon Sep 17 00:00:00 2001 From: Timothy Li Date: Wed, 13 Mar 2019 19:22:19 +0800 Subject: [PATCH 3/6] Update tests --- .../gatsby/src/schema/types/__tests__/date.js | 441 +++++++----------- 1 file changed, 177 insertions(+), 264 deletions(-) diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index 5291f687607e0..08d1694be096b 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -5,173 +5,138 @@ const { isDate } = require(`../date`) // Timestamps grabbed from https://github.com/moment/moment/blob/2e2a5b35439665d4b0200143d808a7c26d6cd30f/src/test/moment/is_valid.js describe(`isDate`, () => { - beforeEach(async () => { - // pass + it.each([ + `1970`, + `2019`, + `1970-01`, + `2019-01`, + `1970-01-01`, + `2010-01-01`, + `2010-01-30`, + `19700101`, + `20100101`, + `20100130`, + `2010-01-30T23+00:00`, + `2010-01-30T23:59+00:00`, + `2010-01-30T23:59:59+00:00`, + `2010-01-30T23:59:59.999+00:00`, + `2010-01-30T23:59:59.999-07:00`, + `2010-01-30T00:00:00.000+07:00`, + `2010-01-30T23:59:59.999-07`, + `2010-01-30T00:00:00.000+07`, + `1970-01-01T00:00:00.000Z`, + `2012-04-01T00:00:00-05:00`, + `2012-11-12T00:00:00+01:00`, + ])(`should return true for valid ISO 8601: %s`, dateString => { + expect(isDate(dateString)).toBeTruthy() }) - it(`supports valid ISO 8601 datetimes`, async () => { - const tests = [ - `1970`, - `2019`, - `1970-01`, - `2019-01`, - `1970-01-01`, - `2010-01-01`, - `2010-01-30`, - `19700101`, - `20100101`, - `20100130`, - `2010-01-30T23+00:00`, - `2010-01-30T23:59+00:00`, - `2010-01-30T23:59:59+00:00`, - `2010-01-30T23:59:59.999+00:00`, - `2010-01-30T23:59:59.999-07:00`, - `2010-01-30T00:00:00.000+07:00`, - `2010-01-30T23:59:59.999-07`, - `2010-01-30T00:00:00.000+07`, - `1970-01-01T00:00:00.000Z`, - `2012-04-01T00:00:00-05:00`, - `2012-11-12T00:00:00+01:00`, - ] - let i - for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) - } + it.each([ + `2010-01-30 23+00:00`, + `2010-01-30 23:59+00:00`, + `2010-01-30 23:59:59+00:00`, + `2010-01-30 23:59:59.999+00:00`, + `2010-01-30 23:59:59.999-07:00`, + `2010-01-30 00:00:00.000+07:00`, + `2010-01-30 23:59:59.999-07`, + `2010-01-30 00:00:00.000+07`, + `1970-01-01 00:00:00.000Z`, + `2012-04-01 00:00:00-05:00`, + `2012-11-12 00:00:00+01:00`, + ])(`should return true for ISO 8601 (no T): %s`, dateString => { + expect(isDate(dateString)).toBeTruthy() }) - it(`supports ISO datetimes without T`, async () => { - const tests = [ - `2010-01-30 23+00:00`, - `2010-01-30 23:59+00:00`, - `2010-01-30 23:59:59+00:00`, - `2010-01-30 23:59:59.999+00:00`, - `2010-01-30 23:59:59.999-07:00`, - `2010-01-30 00:00:00.000+07:00`, - `2010-01-30 23:59:59.999-07`, - `2010-01-30 00:00:00.000+07`, - `1970-01-01 00:00:00.000Z`, - `2012-04-01 00:00:00-05:00`, - `2012-11-12 00:00:00+01:00`, - ] - let i - for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) + it.each([`1970-W31`, `2006-W01`, `1970W31`, `2009-W53-7`, `2009W537`])( + `should return true for ISO 8601 week dates: %s`, + dateString => { + expect(isDate(dateString)).toBeTruthy() } - }) + ) - it(`supports valid ISO 8601 week dates`, async () => { - const tests = [`1970-W31`, `2006-W01`, `1970W31`, `2009-W53-7`, `2009W537`] - let i - for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) + it.each([`1970-334`, `1970334`, `2090-001`, `2090001`])( + `should return true for ISO 8601 ordinal dates: %s`, + dateString => { + expect(isDate(dateString)).toBeTruthy() } - }) + ) - it(`supports valid ISO 8601 ordinal dates`, async () => { - const tests = [`1970-334`, `1970334`, `2090-001`, `2090001`] - let i - for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) + it.each([`2018-08-31T23:25:16.019345+02:00`, `2018-08-31T23:25:16.019345Z`])( + `should return true for microsecond precision: %s`, + dateString => { + expect(isDate(dateString)).toBeTruthy() } - }) + ) - it(`supports microsecond precision`, async () => { - expect(isDate(`2018-08-31T23:25:16.019345+02:00`)).toEqual(true) - expect(isDate(`2018-08-31T23:25:16.019345Z`)).toEqual(true) + it.skip.each([ + `2018-08-31T23:25:16.019345123+02:00`, + `2018-08-31T23:25:16.019345123Z`, + ])(`should return true for nanosecond precision: %s`, dateString => { + expect(isDate(dateString)).toBeTruthy() }) - it(`supports nanosecond precision`, async () => { - expect(isDate(`2018-08-31T23:25:16.019345123+02:00`)).toEqual(true) - expect(isDate(`2018-08-31T23:25:16.019345123Z`)).toEqual(true) - }) - - it.skip(`does not support precision beyond 9 digits`, async () => { - // This seems to pass - expect(isDate(`2018-08-31T23:25:16.01234567899+02:00`)).toEqual(false) - }) - - it(`does not support invalid string iso 8601`, async () => { - const tests = [ - `2010-00-00`, - `2010-01-00`, - `2010-01-40`, - `2010-01-01T24:01`, // 24:00:00 is actually valid - `2010-01-01T23:60`, - `2010-01-01T23:59:60`, - `2010-01-40T23:59:59.9999`, - ] - let i - for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(false) + it.skip.each([`2018-08-31T23:25:16.012345678901+02:00`])( + `should return false for precision beyond 9 digits: %s`, + dateString => { + expect(isDate(dateString)).toBeFalsy() } + ) + + it.each([ + `2010-00-00`, + `2010-01-00`, + `2010-01-40`, + `2010-01-01T24:01`, // 24:00:00 is actually valid + `2010-01-01T23:60`, + `2010-01-01T23:59:60`, + `2010-01-40T23:59:59.9999`, + `2010-00-00T+00:00`, + `2010-01-00T+00:00`, + `2010-01-40T+00:00`, + `2010-01-40T24:01+00:00`, + `2010-01-40T23:60+00:00`, + `2010-01-40T23:59:60+00:00`, + `2010-01-40T23:59:59.9999+00:00`, + `2010-01-40T23:59:59,9999+00:00`, + `2012-04-01T00:00:00-5:00`, // should be -05:00 + `2012-04-01T00:00:00+1:00`, // should be +01:00 + undefined, + `undefined`, + null, + `null`, + [], + {}, + ``, + ` `, + `2012-04-01T00:basketball`, + ])(`should return false for invalid ISO 8601: %s`, dateString => { + expect(isDate(dateString)).toBeFalsy() }) - it(`does not support invalid string iso 8601 + timezone`, async () => { - const tests = [ - `2010-00-00T+00:00`, - `2010-01-00T+00:00`, - `2010-01-40T+00:00`, - `2010-01-40T24:01+00:00`, - `2010-01-40T23:60+00:00`, - `2010-01-40T23:59:60+00:00`, - `2010-01-40T23:59:59.9999+00:00`, - `2010-01-40T23:59:59,9999+00:00`, - `2012-04-01T00:00:00-5:00`, // should be -05:00 - `2012-04-01T00:00:00+1:00`, // should be +01:00 - ] - let i - for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(false) - } - }) - - it(`does not support invalid timestamps`, async () => { - const tests = [ - undefined, - `undefined`, - null, - `null`, - [], - {}, - ``, - ` `, - `2012-04-01T00:basketball`, - ] - let i - for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(false) - } - }) - - it.skip(`supports unix timestamps`, async () => { - // Not sure if in original scope - const tests = [ - 1371065286, - 1379066897.0, - 1379066897.7, - 1379066897.0, - 1379066897.07, - 1379066897.17, - 1379066897.0, - 1379066897.007, - 1379066897.017, - 1379066897.157, - `1371065286`, - `1379066897.`, - `1379066897.0`, - `1379066897.7`, - `1379066897.00`, - `1379066897.07`, - `1379066897.17`, - `1379066897.000`, - `1379066897.007`, - `1379066897.017`, - `1379066897.157`, - ] - let i - for (i = 0; i < tests.length; i++) { - expect(isDate(tests[i])).toEqual(true) - } + it.skip.each([ + 1371065286, + 1379066897.0, + 1379066897.7, + 1379066897.0, + 1379066897.07, + 1379066897.17, + 1379066897.0, + 1379066897.007, + 1379066897.017, + 1379066897.157, + `1371065286`, + `1379066897.`, + `1379066897.0`, + `1379066897.7`, + `1379066897.00`, + `1379066897.07`, + `1379066897.17`, + `1379066897.000`, + `1379066897.007`, + `1379066897.017`, + `1379066897.157`, + ])(`should return true for unix timestamps: %s`, dateString => { + expect(isDate(dateString)).toBeTruthy() }) }) @@ -331,123 +296,71 @@ describe(`dateResolver`, () => { expect(fields.invalidDate16.resolve).toBeUndefined() }) - it(`can properly handle basic iso formats`, async () => { + it.each([ + `2018-01-28T23:59:59.999-07:00`, + `2018-01-29T00:00:00.000Z`, + `2018-01-29`, + `20180129`, + `2018-01-29T23:59:59.999+00:00`, + `2018-01-28T19:59:59.999-07:00`, + `2018-01-30T06:00:00.001+07:00`, + `2018-01-28 17:00:00.001-07`, + `2018-01-30 04:00:00.001+07`, + `2018-01-28T17:00:00.001-07`, + `2018-01-30T04:00:00.001+07`, + `2018-01-29 00:00:00.001Z`, + `2018-01-29T00:00:00.001Z`, + `2018-01-28 23:00:00-05:00`, + `2018-01-29 23:00:00+01:00`, + `2018-01-28T23:00:00-05:00`, + `2018-01-29T23:00:00+01:00`, + `2018-029`, + `2018029`, + `2018-W05`, + `2018W05`, + `2018-W05-1`, + `2018-01-29T23:25:16.019345+02:00`, + `2018-01-29T23:25:16.019345Z`, + // Seems to not require nanosecond definition to not fail + `2018-01-29T23:25:16.019345123+02:00`, + `2018-01-29T23:25:16.019345123Z`, + ])(`should return "Jan 29, 2018": %s`, async dateString => { const schema = await buildTestSchema({}) const fields = schema.getType(`Test`).getFields() - - const validDates = [ - `2018-01-28T23:59:59.999-07:00`, - `2018-01-29T00:00:00.000Z`, - `2018-01-29`, - `20180129`, - `2018-01-29T23:59:59.999+00:00`, - `2018-01-28T19:59:59.999-07:00`, - `2018-01-30T06:00:00.001+07:00`, - `2018-01-28 17:00:00.001-07`, - `2018-01-30 04:00:00.001+07`, - `2018-01-28T17:00:00.001-07`, - `2018-01-30T04:00:00.001+07`, - `2018-01-29 00:00:00.001Z`, - `2018-01-29T00:00:00.001Z`, - `2018-01-28 23:00:00-05:00`, - `2018-01-29 23:00:00+01:00`, - `2018-01-28T23:00:00-05:00`, - `2018-01-29T23:00:00+01:00`, - `2018-029`, - `2018029`, - `2018-W05`, - `2018W05`, - `2018-W05-1`, - ] - - let i - for (i = 0; i < validDates.length; i++) { - expect( - fields[`testDate`].resolve( - { date: validDates[i] }, - { formatString: `MMM DD, YYYY` }, - {}, - { - fieldName: `date`, - } - ) - ).toEqual(`Jan 29, 2018`) - } - }) - - it(`can properly handle microsecond and nanosecond`, async () => { - const schema = await buildTestSchema({}) - const fields = schema.getType(`Test`).getFields() - const validDates = [ - `2018-01-29T23:25:16.019345+02:00`, - `2018-01-29T23:25:16.019345Z`, - // Seems to not require nanosecond definition to not fail - `2018-01-29T23:25:16.019345123+02:00`, - `2018-01-29T23:25:16.019345123Z`, - ] - - let i - for (i = 0; i < validDates.length; i++) { - expect( - fields[`testDate`].resolve( - { date: validDates[i] }, - { formatString: `MMM DD, YYYY` }, - {}, - { - fieldName: `date`, - } - ) - ).toEqual(`Jan 29, 2018`) - } + expect( + fields[`testDate`].resolve( + { date: dateString }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Jan 29, 2018`) }) - it.skip(`errors on n second precision`, async () => { + it.each([ + `2010-00-00`, + `2010-01-00`, + `2010-01-40`, + `2010-01-01T24:01`, + `2010-01-01T23:60`, + `2010-01-01T23:59:60`, + `2010-01-40T23:59:59.9999`, + // Combine with above statement once we figure out why it passes + // `2018-08-31T23:25:16.01234567899993+02:00`, + ])(`should return "Invalid Date": %s`, async dateString => { const schema = await buildTestSchema({}) const fields = schema.getType(`Test`).getFields() - const invalidDates = [`2018-08-31T23:25:16.01234567899993+02:00`] - - let i - for (i = 0; i < invalidDates.length; i++) { - expect( - fields[`testDate`].resolve( - { date: invalidDates[i] }, - { formatString: `MMM DD, YYYY` }, - {}, - { - fieldName: `date`, - } - ) - ).toEqual(`Invalid date`) - } - }) - - it(`errors on invalid datetimes`, async () => { - const schema = await buildTestSchema({}) - const fields = schema.getType(`Test`).getFields() - - const invalidDates = [ - // `2018-08-31T23:25:16.01234567899993+02:00`, - `2010-00-00`, - `2010-01-00`, - `2010-01-40`, - `2010-01-01T24:01`, - `2010-01-01T23:60`, - `2010-01-01T23:59:60`, - `2010-01-40T23:59:59.9999`, - ] - - let i - for (i = 0; i < invalidDates.length; i++) { - expect( - fields[`testDate`].resolve( - { date: invalidDates[i] }, - { formatString: `MMM DD, YYYY` }, - {}, - { - fieldName: `date`, - } - ) - ).toEqual(`Invalid date`) - } + expect( + fields[`testDate`].resolve( + { date: dateString }, + { formatString: `MMM DD, YYYY` }, + {}, + { + fieldName: `date`, + } + ) + ).toEqual(`Invalid date`) }) }) From 54c30bd44cdf4dfd4525e7dbd7ad9ef1d406dd0c Mon Sep 17 00:00:00 2001 From: Timothy Li Date: Wed, 13 Mar 2019 23:19:16 +0800 Subject: [PATCH 4/6] Add additional formats, tests --- .../gatsby/src/schema/types/__tests__/date.js | 22 +++++++++++++++++-- packages/gatsby/src/schema/types/date.js | 13 ++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index 08d1694be096b..8f04955ac70ed 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -24,7 +24,12 @@ describe(`isDate`, () => { `2010-01-30T00:00:00.000+07:00`, `2010-01-30T23:59:59.999-07`, `2010-01-30T00:00:00.000+07`, - `1970-01-01T00:00:00.000Z`, + `2010-01-30T23Z`, + `2010-01-30T23:59Z`, + `2010-01-30T23:59:59Z`, + `2010-01-30T23:59:59.999Z`, + `2010-01-30T00:00:00.000Z`, + `1970-01-01T00:00:00.000001Z`, `2012-04-01T00:00:00-05:00`, `2012-11-12T00:00:00+01:00`, ])(`should return true for valid ISO 8601: %s`, dateString => { @@ -43,10 +48,18 @@ describe(`isDate`, () => { `1970-01-01 00:00:00.000Z`, `2012-04-01 00:00:00-05:00`, `2012-11-12 00:00:00+01:00`, - ])(`should return true for ISO 8601 (no T): %s`, dateString => { + `1970-01-01 00:00:00.0000001 Z`, + `1970-01-01 00:00:00.000 Z`, + `1970-01-01 00:00:00 Z`, + `1970-01-01 000000 Z`, + `1970-01-01 00:00 Z`, + `1970-01-01 00 Z`, + ])(`should return true for ISO 8601 (no T, extra space): %s`, dateString => { expect(isDate(dateString)).toBeTruthy() }) + + it.each([`1970-W31`, `2006-W01`, `1970W31`, `2009-W53-7`, `2009W537`])( `should return true for ISO 8601 week dates: %s`, dateString => { @@ -306,6 +319,11 @@ describe(`dateResolver`, () => { `2018-01-30T06:00:00.001+07:00`, `2018-01-28 17:00:00.001-07`, `2018-01-30 04:00:00.001+07`, + `2018-01-29 04:00:00.001Z`, + `2018-01-29 04:00:00.001 Z`, + `2018-01-29 04:00:00 Z`, + `2018-01-29 04:00 Z`, + `2018-01-29 04 Z`, `2018-01-28T17:00:00.001-07`, `2018-01-30T04:00:00.001+07`, `2018-01-29 00:00:00.001Z`, diff --git a/packages/gatsby/src/schema/types/date.js b/packages/gatsby/src/schema/types/date.js index a2c2b30d40d40..230df593029dc 100644 --- a/packages/gatsby/src/schema/types/date.js +++ b/packages/gatsby/src/schema/types/date.js @@ -52,7 +52,7 @@ const ISO_8601_FORMAT = [ // `YYYY-MM-DDTHH:mm:ss.SSSSSSSSSZ`, // `YYYY-MM-DDTHHmmss.SSSSSSSSSZ`, - // // Coordinated Universal Time (UTC) (Omit T) + // Coordinated Universal Time (UTC) (Omit T) `YYYY-MM-DD HHZ`, `YYYY-MM-DD HH:mmZ`, `YYYY-MM-DD HHmmZ`, @@ -65,6 +65,17 @@ const ISO_8601_FORMAT = [ // `YYYY-MM-DD HH:mm:ss.SSSSSSSSSZ`, // `YYYY-MM-DD HHmmss.SSSSSSSSSZ`, + // Coordinated Universal Time (UTC) (Omit T, Extra Space before Z) + `YYYY-MM-DD HH Z`, + `YYYY-MM-DD HH:mm Z`, + `YYYY-MM-DD HHmm Z`, + `YYYY-MM-DD HH:mm:ss Z`, + `YYYY-MM-DD HHmmss Z`, + `YYYY-MM-DD HH:mm:ss.SSS Z`, + `YYYY-MM-DD HHmmss.SSS Z`, + `YYYY-MM-DD HH:mm:ss.SSSSSS Z`, + `YYYY-MM-DD HHmmss.SSSSSS Z`, + `YYYY-[W]WW`, `YYYY[W]WW`, `YYYY-[W]WW-E`, From ac8e3304a0eb8fc12ca898b2f018b3692ba373c5 Mon Sep 17 00:00:00 2001 From: Timothy Li Date: Thu, 14 Mar 2019 16:10:45 +0800 Subject: [PATCH 5/6] Fix lint issues --- packages/gatsby/src/schema/types/__tests__/date.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index 8f04955ac70ed..e3b880b6b24dc 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -58,8 +58,6 @@ describe(`isDate`, () => { expect(isDate(dateString)).toBeTruthy() }) - - it.each([`1970-W31`, `2006-W01`, `1970W31`, `2009-W53-7`, `2009W537`])( `should return true for ISO 8601 week dates: %s`, dateString => { From 609708b01a227e73904dfac2d50b53f88c2af3cc Mon Sep 17 00:00:00 2001 From: Mikhail Novikov Date: Wed, 20 Mar 2019 11:51:42 +0200 Subject: [PATCH 6/6] Add loki fix --- packages/gatsby/src/schema/types/__tests__/date.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/schema/types/__tests__/date.js b/packages/gatsby/src/schema/types/__tests__/date.js index e3b880b6b24dc..313d5e60fdedf 100644 --- a/packages/gatsby/src/schema/types/__tests__/date.js +++ b/packages/gatsby/src/schema/types/__tests__/date.js @@ -1,6 +1,7 @@ const { store } = require(`../../../redux`) const { build } = require(`../..`) const { isDate } = require(`../date`) +require(`../../../db/__tests__/fixtures/ensure-loki`)() // Timestamps grabbed from https://github.com/moment/moment/blob/2e2a5b35439665d4b0200143d808a7c26d6cd30f/src/test/moment/is_valid.js @@ -231,7 +232,7 @@ describe(`dateResolver`, () => { testDate: Date explicitValidDate: Date invalidHighPrecision: Date - invalidDate8: Date + invalidDate8: Date invalidDate9: Date invalidDate10: Date invalidDate11: Date