Skip to content

Commit

Permalink
add tests for inferring date type from string, array of strings and f…
Browse files Browse the repository at this point in the history
…iltering date fields, move date related test to seperate date test suite
  • Loading branch information
pieh committed Jan 28, 2018
1 parent 2e18f75 commit b9fe96d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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",
},
],
},
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand Down Expand Up @@ -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,
Expand Down
74 changes: 51 additions & 23 deletions packages/gatsby/src/schema/__tests__/infer-graphql-type-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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` }],
Expand Down Expand Up @@ -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`, () => {
Expand Down

0 comments on commit b9fe96d

Please sign in to comment.