Skip to content

Commit

Permalink
Add unit tests on object record mutation and query hooks (#5014)
Browse files Browse the repository at this point in the history
### Description
Add unit tests on object record mutation and query hooks

### Refs
#4884

### Demo
![Screenshot 2024-04-18 at 15 16
19](https://github.com/twentyhq/twenty/assets/140154534/c75f716a-725e-43eb-a703-3db29065fb18)

Fixes #4884

---------

Co-authored-by: gitstart-twenty <gitstart-twenty@users.noreply.github.com>
Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Thiago Nascimbeni <tnascimbeni@gmail.com>
Co-authored-by: Toledodev <rafael.toledo@engenharia.ufjf.br>
Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
  • Loading branch information
6 people committed May 7, 2024
1 parent d0759ad commit d10efb1
Show file tree
Hide file tree
Showing 12 changed files with 727 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { gql } from '@apollo/client';
import { mockedPeopleData } from '~/testing/mock-data/people';

export const query = gql`
query FindDuplicatePerson($id: UUID) {
personDuplicates(id: $id) {
edges {
node {
__typename
xLink {
label
url
}
id
createdAt
city
email
jobTitle
name {
firstName
lastName
}
phone
linkedinLink {
label
url
}
updatedAt
avatarUrl
companyId
}
cursor
}
pageInfo {
hasNextPage
startCursor
endCursor
}
totalCount
}
}
`;

export const variables = {
id: '6205681e-7c11-40b4-9e32-f523dbe54590',
};

export const responseData = {
personDuplicates: {
edges: [
{
node: { __typename: 'Person', ...mockedPeopleData[0], updatedAt: '' },
cursor: 'cursor1',
},
{
node: { __typename: 'Person', ...mockedPeopleData[1], updatedAt: '' },
cursor: 'cursor2',
},
],
pageInfo: {
hasNextPage: false,
startCursor: 'cursor1',
endCursor: 'cursor2',
},
totalCount: 2,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { renderHook } from '@testing-library/react';
import { print } from 'graphql';
import { RecoilRoot } from 'recoil';

import { useCreateManyRecordsMutation } from '@/object-record/hooks/useCreateManyRecordsMutation';

const expectedQueryTemplate = `
mutation CreatePeople($data: [PersonCreateInput!]!) {
createPeople(data: $data) {
__typename
xLink {
label
url
}
id
createdAt
city
email
jobTitle
name {
firstName
lastName
}
phone
linkedinLink {
label
url
}
updatedAt
avatarUrl
companyId
}
}
`.replace(/\s/g, '');

describe('useCreateManyRecordsMutation', () => {
it('should return a valid createManyRecordsMutation', () => {
const objectNameSingular = 'person';
const depth = 2;

const { result } = renderHook(
() =>
useCreateManyRecordsMutation({
objectNameSingular,
depth,
}),
{
wrapper: RecoilRoot,
},
);

const { createManyRecordsMutation } = result.current;

expect(createManyRecordsMutation).toBeDefined();

const printedReceivedQuery = print(createManyRecordsMutation).replace(
/\s/g,
'',
);

expect(printedReceivedQuery).toEqual(expectedQueryTemplate);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { renderHook } from '@testing-library/react';
import { print } from 'graphql';
import { RecoilRoot } from 'recoil';

import { useCreateOneRecordMutation } from '@/object-record/hooks/useCreateOneRecordMutation';

const expectedQueryTemplate = `
mutation CreateOnePerson($input: PersonCreateInput!) {
createPerson(data: $input) {
__typename
xLink {
label
url
}
id
createdAt
city
email
jobTitle
name {
firstName
lastName
}
phone
linkedinLink {
label
url
}
updatedAt
avatarUrl
companyId
}
}
`.replace(/\s/g, '');

describe('useCreateOneRecordMutation', () => {
it('should return a valid createOneRecordMutation', () => {
const objectNameSingular = 'person';
const depth = 2;

const { result } = renderHook(
() =>
useCreateOneRecordMutation({
objectNameSingular,
depth,
}),
{
wrapper: RecoilRoot,
},
);

const { createOneRecordMutation } = result.current;

expect(createOneRecordMutation).toBeDefined();

const printedReceivedQuery = print(createOneRecordMutation).replace(
/\s/g,
'',
);

expect(printedReceivedQuery).toEqual(expectedQueryTemplate);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { renderHook } from '@testing-library/react';
import { print } from 'graphql';
import { RecoilRoot } from 'recoil';

import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation';

const expectedQueryTemplate = `
mutation DeleteManyPeople($filter: PersonFilterInput!) {
deletePeople(filter: $filter) {
id
}
}
`.replace(/\s/g, '');

describe('useDeleteManyRecordsMutation', () => {
it('should return a valid deleteManyRecordsMutation', () => {
const objectNameSingular = 'person';

const { result } = renderHook(
() =>
useDeleteManyRecordsMutation({
objectNameSingular,
}),
{
wrapper: RecoilRoot,
},
);

const { deleteManyRecordsMutation } = result.current;

expect(deleteManyRecordsMutation).toBeDefined();

const printedReceivedQuery = print(deleteManyRecordsMutation).replace(
/\s/g,
'',
);

expect(printedReceivedQuery).toEqual(expectedQueryTemplate);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { renderHook } from '@testing-library/react';
import { print } from 'graphql';
import { RecoilRoot } from 'recoil';

import { useDeleteOneRecordMutation } from '@/object-record/hooks/useDeleteOneRecordMutation';

const expectedQueryTemplate = `
mutation DeleteOnePerson($idToDelete: UUID!) {
deletePerson(id: $idToDelete) {
id
}
}
`.replace(/\s/g, '');

describe('useDeleteOneRecordMutation', () => {
it('should return a valid deleteOneRecordMutation', () => {
const objectNameSingular = 'person';

const { result } = renderHook(
() =>
useDeleteOneRecordMutation({
objectNameSingular,
}),
{
wrapper: RecoilRoot,
},
);

const { deleteOneRecordMutation } = result.current;

expect(deleteOneRecordMutation).toBeDefined();

const printedReceivedQuery = print(deleteOneRecordMutation).replace(
/\s/g,
'',
);

expect(printedReceivedQuery).toEqual(expectedQueryTemplate);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { renderHook } from '@testing-library/react';
import { print } from 'graphql';
import { RecoilRoot } from 'recoil';

import { useExecuteQuickActionOnOneRecordMutation } from '@/object-record/hooks/useExecuteQuickActionOnOneRecordMutation';

const expectedQueryTemplate = `
mutation ExecuteQuickActionOnOnePerson($idToExecuteQuickActionOn: UUID!) {
executeQuickActionOnPerson(id: $idToExecuteQuickActionOn) {
__typename
xLink {
label
url
}
id
createdAt
city
email
jobTitle
name {
firstName
lastName
}
phone
linkedinLink {
label
url
}
updatedAt
avatarUrl
companyId
}
}
`.replace(/\s/g, '');

describe('useExecuteQuickActionOnOneRecordMutation', () => {
it('should return a valid executeQuickActionOnOneRecordMutation', () => {
const objectNameSingular = 'person';

const { result } = renderHook(
() =>
useExecuteQuickActionOnOneRecordMutation({
objectNameSingular,
}),
{
wrapper: RecoilRoot,
},
);

const { executeQuickActionOnOneRecordMutation } = result.current;

expect(executeQuickActionOnOneRecordMutation).toBeDefined();

const printedReceivedQuery = print(
executeQuickActionOnOneRecordMutation,
).replace(/\s/g, '');

expect(printedReceivedQuery).toEqual(expectedQueryTemplate);
});
});

0 comments on commit d10efb1

Please sign in to comment.