import { Kind } from 'graphql/language'
import { resolver } from '../src/graphql/schema/types/scalar/email'

/* eslint-disable no-underscore-dangle */
const email = 'test@test'
const emailComplete = 'test@test.com'

describe('Email scalar', () => {
  describe('when email is valid', () => {
    it('serialize correctly with complete email', () => {
      expect(resolver.Email.__serialize(emailComplete))
        .to
        .eql(emailComplete)
    })

    it('serialize correctly', () => {
      expect(resolver.Email.__serialize(email))
        .to
        .eql(email)
    })

    it('parseValue correctly with complete email', () => {
      expect(resolver.Email.__parseValue(emailComplete))
        .to
        .eql(emailComplete)
    })

    it('parseValue correctly', () => {
      expect(resolver.Email.__parseValue(email))
        .to
        .eql(email)
    })

    it('parseLiteral correctly with complete email', () => {
      expect(
        resolver.Email.__parseLiteral({
          value: emailComplete,
          kind: Kind.STRING
        }, {})
      )
        .to
        .eql(emailComplete)
    })

    it('parseLiteral correctly', () => {
      expect(
        resolver.Email.__parseLiteral({
          value: email,
          kind: Kind.STRING
        }, {})
      )
        .to
        .eql(email)
    })
  })

  describe('when email is invalid', () => {
    describe('and not a string', () => {
      it('parseValue', () => {
        expect(() => resolver.Email.__parseValue(435))
          .to
          .throw(
            'Value is not string'
          )
      })

      it('parseLiteral', () => {
        expect(() => resolver.Email.__parseLiteral({
          value: '453',
          kind: Kind.INT
        }, {}))
          .to
          .throw('Value is not string : IntValue')
      })
    })

    describe('and not an email address', () => {
      it('parseValue', () => {
        expect(() => resolver.Email.__parseValue('this is a test'))
          .to
          .throw('Value is not a valid email address: this is a test')
      })

      it('parseLiteral', () => {
        expect(() => resolver.Email.__parseLiteral({
          value: 'this is a test',
          kind: Kind.STRING
        }, {}))
          .to
          .throw('Value is not a valid email address')
      })
    })
  })
})