<commit_before>/**
 * Copyright 2013-2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @emails oncall+relay
 */

'use strict';

require('configureForRelayOSS');

const Relay = require('Relay');
const RelayConnectionInterface = require('RelayConnectionInterface');
const RelayTestUtils = require('RelayTestUtils');

const flattenRelayQuery = require('flattenRelayQuery');
const inferRelayFieldsFromData = require('inferRelayFieldsFromData');

describe('inferRelayFieldsFromData', () => {
  const {getVerbatimNode, matchers} = RelayTestUtils;
  let HAS_NEXT_PAGE, HAS_PREV_PAGE, PAGE_INFO;

  beforeEach(() => {
    jest.resetModuleRegistry();

    ({
      HAS_NEXT_PAGE,
      HAS_PREV_PAGE,
      PAGE_INFO,
    } = RelayConnectionInterface);

    jasmine.addMatchers({
      ...RelayTestUtils.matchers,
      toEqualFields() {
        return {
          compare(actual, expected) {
            expected = flattenRelayQuery(getVerbatimNode(expected));
            actual = flattenRelayQuery(expected.clone(actual));
            // NOTE: Generated fields might get in the way.
            return matchers.toEqualQueryNode().compare(actual, expected);
          },
        };
      },
    });
  });

  it('generates metadata for fields', () => {
    const query = inferRelayFieldsFromData({
      id: '123',
    });
    expect(query).toEqualFields(Relay.QL`
      fragment on Actor {
        id,
      }
    `);
    expect(query[0].isPlural()).toBe(false);
  });

  it('infers scalar fields from scalars', () => {
    expect(inferRelayFieldsFromData({
      id: '123',
      name: 'Alice',
    })).toEqualFields(Relay.QL`
      fragment on Actor {
        id,
        name,
      }
    `);
  });

  it('infers nested fields from objects', () => {
    expect(inferRelayFieldsFromData({
      id: '123',
      address: {
        city: 'Menlo Park',
      },
    })).toEqualFields(Relay.QL`
      fragment on Actor {
        id,
        address {
          city,
        },
      }
    `);
  });

  it('infers unterminated fields from null', () => {
    expect(inferRelayFieldsFromData({
      id: '123',
      address: null,
    })).toEqualFields(Relay.QL`
      fragment on Actor {
        id,
        address,
      }
    `);
  });

  it('infers plural fields from arrays of scalars', () => {<commit_msg>Manually test fields inferred from null payload.<commit_after>/**
 * Copyright 2013-2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @emails oncall+relay
 */

'use strict';

require('configureForRelayOSS');

const Relay = require('Relay');
const RelayConnectionInterface = require('RelayConnectionInterface');
const RelayQuery = require('RelayQuery');
const RelayTestUtils = require('RelayTestUtils');

const flattenRelayQuery = require('flattenRelayQuery');
const inferRelayFieldsFromData = require('inferRelayFieldsFromData');

describe('inferRelayFieldsFromData', () => {
  const {getVerbatimNode, matchers} = RelayTestUtils;
  let HAS_NEXT_PAGE, HAS_PREV_PAGE, PAGE_INFO;

  beforeEach(() => {
    jest.resetModuleRegistry();

    ({
      HAS_NEXT_PAGE,
      HAS_PREV_PAGE,
      PAGE_INFO,
    } = RelayConnectionInterface);

    jasmine.addMatchers({
      ...RelayTestUtils.matchers,
      toEqualFields() {
        return {
          compare(actual, expected) {
            expected = flattenRelayQuery(getVerbatimNode(expected));
            actual = flattenRelayQuery(expected.clone(actual));
            // NOTE: Generated fields might get in the way.
            return matchers.toEqualQueryNode().compare(actual, expected);
          },
        };
      },
    });
  });

  it('generates metadata for fields', () => {
    const query = inferRelayFieldsFromData({
      id: '123',
    });
    expect(query).toEqualFields(Relay.QL`
      fragment on Actor {
        id,
      }
    `);
    expect(query[0].isPlural()).toBe(false);
  });

  it('infers scalar fields from scalars', () => {
    expect(inferRelayFieldsFromData({
      id: '123',
      name: 'Alice',
    })).toEqualFields(Relay.QL`
      fragment on Actor {
        id,
        name,
      }
    `);
  });

  it('infers nested fields from objects', () => {
    expect(inferRelayFieldsFromData({
      id: '123',
      address: {
        city: 'Menlo Park',
      },
    })).toEqualFields(Relay.QL`
      fragment on Actor {
        id,
        address {
          city,
        },
      }
    `);
  });

  it('infers unterminated fields from null', () => {
    const inferredFields = inferRelayFieldsFromData({
      id: '123',
      address: null,
    });

    expect(inferredFields[0] instanceof RelayQuery.Field).toBe(true);
    expect(inferredFields[0].isScalar()).toBe(true);
    expect(inferredFields[0].getSchemaName()).toBe('id');

    expect(inferredFields[1] instanceof RelayQuery.Field).toBe(true);
    // Though this is not scalar, there is no way we can infer this from
    // `address: null`. Defaults to true.
    expect(inferredFields[1].isScalar()).toBe(true);
    expect(inferredFields[1].getSchemaName()).toBe('address');
  });

  it('infers plural fields from arrays of scalars', () => {
{'fraction_non_alphanumeric': 0.12932276657060518, 'fraction_numerical': 0.008645533141210375, 'mean_word_length': 2.824380165289256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5104314', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1639, 'n_words': 386}