<commit_before>const { GraphQLString, graphql, defaultFieldResolver } = require(`graphql`)
const { build } = require(`../..`)
const withResolverContext = require(`../../context`)
const { buildObjectType } = require(`../../types/type-builders`)
const { store } = require(`../../../redux`)
const { dispatch } = store
const { actions } = require(`../../../redux/actions/restricted`)
const { createFieldExtension, createTypes } = actions
require(`../../../db/__tests__/fixtures/ensure-loki`)()

const report = require(`gatsby-cli/lib/reporter`)
report.error = jest.fn()
report.panic = jest.fn()
afterEach(() => {
  report.error.mockClear()
  report.panic.mockClear()
})

describe(`GraphQL field extensions`, () => {
  beforeEach(() => {
    dispatch({ type: `DELETE_CACHE` })
    const nodes = [
      {
        id: `test1`,
        internal: { type: `Test` },
        somedate: `2019-09-01`,
        otherdate: `2019-09-01`,
      },
      {
        id: `test2`,
        internal: { type: `Test` },
        somedate: `2019-09-13`,
        otherdate: `2019-09-13`,
      },
      {
        id: `test3`,
        internal: { type: `Test` },
        somedate: `2019-09-26`,
        otherdate: `2019-09-26`,
      },
      {
        id: `test4`,
        internal: { type: `Test` },
        olleh: `world`,
      },
      {
        id: `test5`,
        internal: { type: `AnotherTest` },
        date: `2019-01-01`,
      },
      {
        id: `test6`,
        internal: { type: `AnotherTest` },
        date: 0,
      },
    ]
    nodes.forEach(node => {
      dispatch({ type: `CREATE_NODE`, payload: { ...node } })
    })
  })

  it(`allows creating a cutom field extension`, async () => {
    dispatch(
      createFieldExtension({
        name: `birthday`,
        args: {
          greeting: {
            type: GraphQLString,
          },
        },
        extend(options, prevFieldConfig) {
          return {
            type: `String`,
            args: {
              emoji: {
                type: `Boolean`,
              },
            },
            resolve(source, args, context, info) {
              const fieldValue = source[info.fieldName]
              const date = new Date(fieldValue)
              if (date.getUTCMonth() === 8 && date.getUTCDate() === 26) {
                return args.emoji
                  ? `:cake:`
                  : options.greeting || `Happy birthday!`
              }
              return fieldValue
            },
          }
        },
      })<commit_msg>test(gatsby): Fix typo in test name (#15992)<commit_after>const { GraphQLString, graphql, defaultFieldResolver } = require(`graphql`)
const { build } = require(`../..`)
const withResolverContext = require(`../../context`)
const { buildObjectType } = require(`../../types/type-builders`)
const { store } = require(`../../../redux`)
const { dispatch } = store
const { actions } = require(`../../../redux/actions/restricted`)
const { createFieldExtension, createTypes } = actions
require(`../../../db/__tests__/fixtures/ensure-loki`)()

const report = require(`gatsby-cli/lib/reporter`)
report.error = jest.fn()
report.panic = jest.fn()
afterEach(() => {
  report.error.mockClear()
  report.panic.mockClear()
})

describe(`GraphQL field extensions`, () => {
  beforeEach(() => {
    dispatch({ type: `DELETE_CACHE` })
    const nodes = [
      {
        id: `test1`,
        internal: { type: `Test` },
        somedate: `2019-09-01`,
        otherdate: `2019-09-01`,
      },
      {
        id: `test2`,
        internal: { type: `Test` },
        somedate: `2019-09-13`,
        otherdate: `2019-09-13`,
      },
      {
        id: `test3`,
        internal: { type: `Test` },
        somedate: `2019-09-26`,
        otherdate: `2019-09-26`,
      },
      {
        id: `test4`,
        internal: { type: `Test` },
        olleh: `world`,
      },
      {
        id: `test5`,
        internal: { type: `AnotherTest` },
        date: `2019-01-01`,
      },
      {
        id: `test6`,
        internal: { type: `AnotherTest` },
        date: 0,
      },
    ]
    nodes.forEach(node => {
      dispatch({ type: `CREATE_NODE`, payload: { ...node } })
    })
  })

  it(`allows creating a custom field extension`, async () => {
    dispatch(
      createFieldExtension({
        name: `birthday`,
        args: {
          greeting: {
            type: GraphQLString,
          },
        },
        extend(options, prevFieldConfig) {
          return {
            type: `String`,
            args: {
              emoji: {
                type: `Boolean`,
              },
            },
            resolve(source, args, context, info) {
              const fieldValue = source[info.fieldName]
              const date = new Date(fieldValue)
              if (date.getUTCMonth() === 8 && date.getUTCDate() === 26) {
                return args.emoji
                  ? `:cake:`
                  : options.greeting || `Happy birthday!`
              }
              return fieldValue
            },
          }
        },
      })
{'fraction_non_alphanumeric': 0.1687177451369591, 'fraction_numerical': 0.027193330686780467, 'mean_word_length': 1.6619123085050185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4441901', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1495, 'n_words': 304}