From b9fe96d8550af8803abd7396ba247f7697bff094 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Sat, 27 Jan 2018 01:11:27 +0100 Subject: [PATCH] add tests for inferring date type from string, array of strings and filtering date fields, move date related test to seperate date test suite --- .../infer-graphql-type-test.js.snap | 30 ++++---- .../infer-graphql-input-type-test.js | 23 +++++- .../__tests__/infer-graphql-type-test.js | 74 +++++++++++++------ 3 files changed, 87 insertions(+), 40 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap b/packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap index 9fb28ffd45b81..f3fb4e067e9cc 100644 --- a/packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap +++ b/packages/gatsby/src/schema/__tests__/__snapshots__/infer-graphql-type-test.js.snap @@ -1,5 +1,20 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`GraphQL type inferance Handles dates Date type inference 1`] = ` +Object { + "data": Object { + "listNode": Array [ + Object { + "dateObject": "2012-11-05T00:00:00.000Z", + }, + Object { + "dateObject": "2012-11-05T00:00:00.000Z", + }, + ], + }, +} +`; + exports[`GraphQL type inferance Infers graphql type from array of nodes 1`] = ` Object { "data": Object { @@ -115,18 +130,3 @@ Object { }, } `; - -exports[`GraphQL type inferance handles date objects 1`] = ` -Object { - "data": Object { - "listNode": Array [ - Object { - "dateObject": "2012-11-05T00:00:00.000Z", - }, - Object { - "dateObject": "2012-11-05T00:00:00.000Z", - }, - ], - }, -} -`; diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js index dd11b0e4448d9..5bbc1e05d9d0c 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-input-type-test.js @@ -78,6 +78,7 @@ function queryResult(nodes, query, { types = [] } = {}) { describe(`GraphQL Input args`, () => { const nodes = [ { + index: 0, name: `The Mad Max`, hair: 1, date: `2006-07-22T22:39:53.000Z`, @@ -103,9 +104,9 @@ describe(`GraphQL Input args`, () => { boolean: true, }, { + index: 1, name: `The Mad Wax`, hair: 2, - date: `2006-07-22T22:39:53.000Z`, anArray: [1, 2, 5, 4], frontmatter: { date: `2006-07-22T22:39:53.000Z`, @@ -116,9 +117,10 @@ describe(`GraphQL Input args`, () => { boolean: false, }, { + index: 2, name: `The Mad Wax`, hair: 0, - date: `2006-07-22T22:39:53.000Z`, + date: `2006-07-29T22:39:53.000Z`, frontmatter: { date: `2006-07-22T22:39:53.000Z`, title: `The world of shave and adventure`, @@ -375,6 +377,23 @@ describe(`GraphQL Input args`, () => { expect(result.data.allNode.edges[0].node.name).toEqual(`The Mad Wax`) }) + it(`filters date fields`, async () => { + let result = await queryResult( + nodes, + ` + { + allNode(filter: {date: { ne: null }}) { + edges { node { index }} + } + } + ` + ) + expect(result.errors).not.toBeDefined() + expect(result.data.allNode.edges.length).toEqual(2) + expect(result.data.allNode.edges[0].node.index).toEqual(0) + expect(result.data.allNode.edges[1].node.index).toEqual(2) + }) + it(`sorts results`, async () => { let result = await queryResult( nodes, diff --git a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js index 8fc2190ce8c27..b6c3f7002bb1d 100644 --- a/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js +++ b/packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js @@ -123,29 +123,6 @@ describe(`GraphQL type inferance`, () => { expect(result.data.listNode[0].number).toEqual(1.1) }) - it(`handles integer with valid date format`, async () => { - let result = await queryResult( - [{ number: 2018 }, { number: 1987 }], - ` - number - ` - ) - expect(result.data.listNode[0].number).toEqual(2018) - }) - - it(`handles date objects`, async () => { - let result = await queryResult( - [ - { dateObject: new Date(Date.UTC(2012, 10, 5)) }, - { dateObject: new Date(Date.UTC(2012, 10, 5)) }, - ], - ` - dateObject - ` - ) - expect(result).toMatchSnapshot() - }) - it(`filters out empty objects`, async () => { let result = await queryResult( [{ foo: {}, bar: `baz` }], @@ -211,6 +188,57 @@ describe(`GraphQL type inferance`, () => { expect(Object.keys(fields.foo.type.getFields())).toHaveLength(4) }) + describe(`Handles dates`, () => { + it(`Handles integer with valid date format`, async () => { + let result = await queryResult( + [{ number: 2018 }, { number: 1987 }], + ` + number + ` + ) + expect(result.data.listNode[0].number).toEqual(2018) + }) + + it(`Date type inference`, async () => { + let result = await queryResult( + [ + { dateObject: new Date(Date.UTC(2012, 10, 5)) }, + { dateObject: new Date(Date.UTC(2012, 10, 5)) }, + ], + ` + dateObject + ` + ) + expect(result).toMatchSnapshot() + }) + + it(`Infers from date strings`, async () => { + let result = await queryResult( + [{ date: `1012-11-01` }], + ` + date(formatString:"DD.MM.YYYY") + ` + ) + + expect(result.errors).not.toBeDefined() + expect(result.data.listNode[0].date).toEqual(`01.11.1012`) + }) + + it(`Infers from arrays of date strings`, async () => { + let result = await queryResult( + [{ date: [`1012-11-01`, `10390203`] }], + ` + date(formatString:"DD.MM.YYYY") + ` + ) + + expect(result.errors).not.toBeDefined() + expect(result.data.listNode[0].date.length).toEqual(2) + expect(result.data.listNode[0].date[0]).toEqual(`01.11.1012`) + expect(result.data.listNode[0].date[1]).toEqual(`03.02.1039`) + }) + }) + xdescribe(`Linked inference from config mappings`) describe(`Linked inference from file URIs`, () => {