Skip to content

The open alternative to Salesforce, designed for AI.

12.9 KB
Raw Blame
import { PERSON_GQL_FIELDS } from 'test/integration/constants/person-gql-fields.constants';
import {
  TEST_PERSON_1_ID,
  TEST_PERSON_2_ID,
  TEST_PERSON_3_ID,
} from 'test/integration/constants/test-person-ids.constants';
import { createManyOperationFactory } from 'test/integration/graphql/utils/create-many-operation-factory.util';
import { createOneOperationFactory } from 'test/integration/graphql/utils/create-one-operation-factory.util';
import { deleteManyOperationFactory } from 'test/integration/graphql/utils/delete-many-operation-factory.util';
import { deleteOneOperationFactory } from 'test/integration/graphql/utils/delete-one-operation-factory.util';
import { destroyManyOperationFactory } from 'test/integration/graphql/utils/destroy-many-operation-factory.util';
import { destroyOneOperationFactory } from 'test/integration/graphql/utils/destroy-one-operation-factory.util';
import { findManyOperationFactory } from 'test/integration/graphql/utils/find-many-operation-factory.util';
import { findOneOperationFactory } from 'test/integration/graphql/utils/find-one-operation-factory.util';
import { makeGraphqlApiRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';
import { updateManyOperationFactory } from 'test/integration/graphql/utils/update-many-operation-factory.util';
import { updateOneOperationFactory } from 'test/integration/graphql/utils/update-one-operation-factory.util';
import { deleteAllRecords } from 'test/integration/utils/delete-all-records';
import { generateRecordName } from 'test/integration/utils/generate-record-name';

describe('people resolvers (integration)', () => {
  beforeAll(async () => {
    await deleteAllRecords('person');
  });

  it('1. should create and return people', async () => {
    const personJobTitle1 = generateRecordName(TEST_PERSON_1_ID);
    const personJobTitle2 = generateRecordName(TEST_PERSON_2_ID);
    const graphqlOperation = createManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
      data: [
        {
          id: TEST_PERSON_1_ID,
          jobTitle: personJobTitle1,
        },
        {
          id: TEST_PERSON_2_ID,
          jobTitle: personJobTitle2,
        },
      ],
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.createPeople).toHaveLength(2);

    // @ts-expect-error legacy noImplicitAny
    response.body.data.createPeople.forEach((person) => {
      expect(person).toHaveProperty('jobTitle');
      expect([personJobTitle1, personJobTitle2]).toContain(person.jobTitle);

      expect(person).toHaveProperty('id');
      expect(person).toHaveProperty('jobTitle');
      expect(person).toHaveProperty('avatarUrl');
      expect(person).toHaveProperty('avatarFile');
      expect(person).toHaveProperty('intro');
      expect(person).toHaveProperty('searchVector');
      expect(person).toHaveProperty('name');
      expect(person).toHaveProperty('createdAt');
      expect(person).toHaveProperty('deletedAt');
    });
  });

  it('1b. should create and return one person', async () => {
    const personJobTitle3 = generateRecordName(TEST_PERSON_3_ID);

    const graphqlOperation = createOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      data: {
        id: TEST_PERSON_3_ID,
        jobTitle: personJobTitle3,
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    const createdPerson = response.body.data.createPerson;

    expect(createdPerson).toHaveProperty('jobTitle');
    expect(createdPerson.jobTitle).toEqual(personJobTitle3);

    expect(createdPerson).toHaveProperty('id');
    expect(createdPerson).toHaveProperty('jobTitle');
    expect(createdPerson).toHaveProperty('avatarUrl');
    expect(createdPerson).toHaveProperty('avatarFile');
    expect(createdPerson).toHaveProperty('intro');
    expect(createdPerson).toHaveProperty('searchVector');
    expect(createdPerson).toHaveProperty('name');
    expect(createdPerson).toHaveProperty('createdAt');
    expect(createdPerson).toHaveProperty('deletedAt');
  });

  it('2. should find many people', async () => {
    const graphqlOperation = findManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    const data = response.body.data.people;

    expect(data).toBeDefined();
    expect(Array.isArray(data.edges)).toBe(true);

    const edges = data.edges;

    if (edges.length > 0) {
      const person = edges[0].node;

      expect(person).toHaveProperty('id');
      expect(person).toHaveProperty('jobTitle');
      expect(person).toHaveProperty('avatarUrl');
      expect(person).toHaveProperty('avatarFile');
      expect(person).toHaveProperty('intro');
      expect(person).toHaveProperty('searchVector');
      expect(person).toHaveProperty('name');
      expect(person).toHaveProperty('createdAt');
      expect(person).toHaveProperty('deletedAt');
    }
  });

  it('2b. should find one person', async () => {
    const graphqlOperation = findOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          eq: TEST_PERSON_3_ID,
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    const person = response.body.data.person;

    expect(person).toHaveProperty('jobTitle');

    expect(person).toHaveProperty('id');
    expect(person).toHaveProperty('jobTitle');
    expect(person).toHaveProperty('avatarUrl');
    expect(person).toHaveProperty('avatarFile');
    expect(person).toHaveProperty('intro');
    expect(person).toHaveProperty('searchVector');
    expect(person).toHaveProperty('name');
    expect(person).toHaveProperty('createdAt');
    expect(person).toHaveProperty('deletedAt');
  });

  it('3. should update many people', async () => {
    const graphqlOperation = updateManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
      data: {
        jobTitle: 'Updated City',
      },
      filter: {
        id: {
          in: [TEST_PERSON_1_ID, TEST_PERSON_2_ID],
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    const updatedPeople = response.body.data.updatePeople;

    expect(updatedPeople).toHaveLength(2);

    // @ts-expect-error legacy noImplicitAny
    updatedPeople.forEach((person) => {
      expect(person.jobTitle).toEqual('Updated City');
    });
  });

  it('3b. should update one person', async () => {
    const graphqlOperation = updateOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      data: {
        jobTitle: 'New City',
      },
      recordId: TEST_PERSON_3_ID,
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    const updatedPerson = response.body.data.updatePerson;

    expect(updatedPerson.jobTitle).toEqual('New City');
  });

  it('4. should find many people with updated city', async () => {
    const graphqlOperation = findManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        jobTitle: {
          eq: 'Updated City',
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.people.edges).toHaveLength(2);
  });

  it('4b. should find one person with updated city', async () => {
    const graphqlOperation = findOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        jobTitle: {
          eq: 'New City',
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.person.jobTitle).toEqual('New City');
  });

  it('5. should delete many people', async () => {
    const graphqlOperation = deleteManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          in: [TEST_PERSON_1_ID, TEST_PERSON_2_ID],
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    const deletePeople = response.body.data.deletePeople;

    expect(deletePeople).toHaveLength(2);

    // @ts-expect-error legacy noImplicitAny
    deletePeople.forEach((person) => {
      expect(person.deletedAt).toBeTruthy();
    });
  });

  it('5b. should delete one person', async () => {
    const graphqlOperation = deleteOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      recordId: TEST_PERSON_3_ID,
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.deletePerson.deletedAt).toBeTruthy();
  });

  it('6. should not find many people anymore', async () => {
    const graphqlOperation = findManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          in: [TEST_PERSON_1_ID, TEST_PERSON_2_ID],
        },
      },
    });

    const findPeopleResponse = await makeGraphqlApiRequest(graphqlOperation);

    expect(findPeopleResponse.body.data.people.edges).toHaveLength(0);
  });

  it('6b. should not find one person anymore', async () => {
    const graphqlOperation = findOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          eq: TEST_PERSON_3_ID,
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.person).toBeNull();
  });

  it('7. should find many deleted people with deletedAt filter', async () => {
    const graphqlOperation = findManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          in: [TEST_PERSON_1_ID, TEST_PERSON_2_ID],
        },
        not: {
          deletedAt: {
            is: 'NULL',
          },
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.people.edges).toHaveLength(2);
  });

  it('7b. should find one deleted person with deletedAt filter', async () => {
    const graphqlOperation = findOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          eq: TEST_PERSON_3_ID,
        },
        not: {
          deletedAt: {
            is: 'NULL',
          },
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.person.id).toEqual(TEST_PERSON_3_ID);
  });

  it('8. should destroy many people', async () => {
    const graphqlOperation = destroyManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          in: [TEST_PERSON_1_ID, TEST_PERSON_2_ID],
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.destroyPeople).toHaveLength(2);
  });

  it('8b. should destroy one person', async () => {
    const graphqlOperation = destroyOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      recordId: TEST_PERSON_3_ID,
    });

    const destroyPeopleResponse = await makeGraphqlApiRequest(graphqlOperation);

    expect(destroyPeopleResponse.body.data.destroyPerson).toBeTruthy();
  });

  it('9. should not find many people anymore', async () => {
    const graphqlOperation = findManyOperationFactory({
      objectMetadataSingularName: 'person',
      objectMetadataPluralName: 'people',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          in: [TEST_PERSON_1_ID, TEST_PERSON_2_ID],
        },
        not: {
          deletedAt: {
            is: 'NULL',
          },
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.people.edges).toHaveLength(0);
  });

  it('9b. should not find one person anymore', async () => {
    const graphqlOperation = findOneOperationFactory({
      objectMetadataSingularName: 'person',
      gqlFields: PERSON_GQL_FIELDS,
      filter: {
        id: {
          eq: TEST_PERSON_3_ID,
        },
        not: {
          deletedAt: {
            is: 'NULL',
          },
        },
      },
    });

    const response = await makeGraphqlApiRequest(graphqlOperation);

    expect(response.body.data.person).toBeNull();
  });
});