<commit_before>/* @flow */

import mongoose from 'mongoose';
import { composeWithMongoose } from '../index';

describe('github issues checks', () => {
  it('#78 Mongoose and Discriminators', () => {
    const options = { discriminatorKey: 'kind' };

    const eventSchema = new mongoose.Schema({ time: Date }, options);
    const Event = mongoose.model('GenericEvent', eventSchema);

    const clickedLinkSchema = new mongoose.Schema({ url: String }, options);
    const ClickedLinkEvent = Event.discriminator('ClickedLinkEvent', clickedLinkSchema);

    const EventTC = composeWithMongoose(Event);
    const ClickedLinkEventTC = composeWithMongoose(ClickedLinkEvent);

    expect(EventTC.getFieldNames()).toEqual(['time', '_id', 'kind']);
    expect(ClickedLinkEventTC.getFieldNames()).toEqual(['url', '_id', 'time', 'kind']);
  });
});
<commit_msg>test: improve descriminator tests<commit_after>/* @flow */

import mongoose from 'mongoose';
import MongodbMemoryServer from 'mongodb-memory-server';
import { graphql, GQC } from 'graphql-compose';
import { composeWithMongoose } from '../index';

// May require additional time for downloading MongoDB binaries
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

let mongoServer;
const opts = { useMongoClient: true };

beforeAll(async () => {
  mongoServer = new MongodbMemoryServer();
  const mongoUri = await mongoServer.getConnectionString();
  mongoose.connect(mongoUri, opts, err => {
    if (err) console.error(err);
  });
});

afterAll(() => {
  mongoose.disconnect();
  mongoServer.stop();
});

describe('github issues checks', () => {
  describe('#78 Mongoose and Discriminators', () => {
    const options = { discriminatorKey: 'kind' };

    const eventSchema = new mongoose.Schema({ refId: String }, options);
    const Event = mongoose.model('GenericEvent', eventSchema);

    const clickedLinkSchema = new mongoose.Schema({ url: String }, options);
    const ClickedLinkEvent = Event.discriminator('ClickedLinkEvent', clickedLinkSchema);

    const EventTC = composeWithMongoose(Event);
    const ClickedLinkEventTC = composeWithMongoose(ClickedLinkEvent);

    it('creating Types from models', () => {
      expect(EventTC.getFieldNames()).toEqual(['refId', '_id', 'kind']);
      expect(ClickedLinkEventTC.getFieldNames()).toEqual(['url', '_id', 'refId', 'kind']);
    });

    it('manually override resolver output type for findMany', async () => {
      const EventDescriminatorType = new graphql.GraphQLUnionType({
        name: 'EventDescriminator',
        types: [EventTC.getType(), ClickedLinkEventTC.getType()],
        resolveType: value => {
          if (value.kind === 'ClickedLinkEvent') {
            return ClickedLinkEventTC.getType();
          }
          return EventTC.getType();
        },
      });

      EventTC.getResolver('findMany').setType(new graphql.GraphQLList(EventDescriminatorType));

      // let's check graphql response

      await Event.create({ refId: 'aaa' });
      await Event.create({ refId: 'bbb' });
      await ClickedLinkEvent.create({ refId: 'ccc', url: 'url1' });
      await ClickedLinkEvent.create({ refId: 'ddd', url: 'url2' });

      GQC.rootQuery().addFields({
        eventFindMany: EventTC.getResolver('findMany'),
      });
      const schema = GQC.buildSchema();

      const res = await graphql.graphql(
        schema,
        `{
        eventFindMany {
          __typename
          ... on GenericEvent {
            refId
          }
          ... on ClickedLinkEvent {
            refId
            url
          }
        }
      }`
      );

      expect(res).toEqual({
        data: {
          eventFindMany: [
            { __typename: 'GenericEvent', refId: 'aaa' },
            { __typename: 'GenericEvent', refId: 'bbb' },
            { __typename: 'ClickedLinkEvent', refId: 'ccc', url: 'url1' },
            { __typename: 'ClickedLinkEvent', refId: 'ddd', url: 'url2' },
          ],
        },
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.1463229719484458, 'fraction_numerical': 0.0032853171594642407, 'mean_word_length': 3.1619348054679284, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4764492', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1163, 'n_words': 256}