import assert from 'assert';
import { parse } from 'graphql';
import { validate } from 'graphql/validation';
import { buildASTSchema } from 'graphql/utilities/buildASTSchema';

import { FieldsHaveDescriptions } from '../../src/rules/fields_have_descriptions';

describe('FieldsHaveDescriptions rule', () => {
  it('catches fields that have no description', () => {
    const ast = parse(`
      type QueryRoot {
        withoutDescription: String
        withoutDescriptionAgain: String!

        # Description
        withDescription: String
      }

      schema {
        query: QueryRoot
      }
    `);

    const schema = buildASTSchema(ast);
    const errors = validate(schema, ast, [FieldsHaveDescriptions]);

    assert.equal(errors.length, 2);

    assert.equal(errors[0].ruleName, 'fields-have-descriptions');
    assert.equal(
      errors[0].message,
      'The field `QueryRoot.withoutDescription` is missing a description.'
    );
    assert.deepEqual(errors[0].locations, [{ line: 3, column: 9 }]);

    assert.equal(errors[1].ruleName, 'fields-have-descriptions');
    assert.equal(
      errors[1].message,
      'The field `QueryRoot.withoutDescriptionAgain` is missing a description.'
    );
    assert.deepEqual(errors[1].locations, [{ line: 4, column: 9 }]);
  });
});

import { FieldsHaveDescriptions } from '../../src/rules/fields_have_descriptions';
import { expectFailsRule } from '../assertions';

describe('FieldsHaveDescriptions rule', () => {
  it('catches fields that have no description', () => {
    expectFailsRule(
      FieldsHaveDescriptions,
      `
      type QueryRoot {
        withoutDescription: String
        withoutDescriptionAgain: String!

        # Description
        withDescription: String
      }

      schema {
        query: QueryRoot
      }
    `,
      [
        {
          message:
            'The field `QueryRoot.withoutDescription` is missing a description.',
          locations: [{ line: 3, column: 9 }],
        },
        {
          message:
            'The field `QueryRoot.withoutDescriptionAgain` is missing a description.',
          locations: [{ line: 4, column: 9 }],
        },
      ]
    );
  });
});

Use test helpers in FieldsHaveDescriptions tests
Use test helpers in FieldsHaveDescriptions tests
