Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martmull committed Dec 18, 2024
1 parent 26aede7 commit ea0406d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,14 @@ import {
useExportFetchRecords,
} from '../useExportFetchRecords';

import { PERSON_FRAGMENT_WITH_DEPTH_ZERO_RELATIONS } from '@/object-record/hooks/__mocks__/personFragments';
import { useObjectOptionsForBoard } from '@/object-record/object-options-dropdown/hooks/useObjectOptionsForBoard';
import { recordGroupFieldMetadataComponentState } from '@/object-record/record-group/states/recordGroupFieldMetadataComponentState';
import { useRecoilComponentStateV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentStateV2';
import { ViewType } from '@/views/types/ViewType';
import { MockedResponse } from '@apollo/client/testing';
import { expect } from '@storybook/test';
import gql from 'graphql-tag';
import { getJestMetadataAndApolloMocksAndActionMenuWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksAndContextStoreWrapper';
import { generatedMockObjectMetadataItems } from '~/testing/mock-data/generatedMockObjectMetadataItems';

const defaultResponseData = {
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
startCursor: '',
endCursor: '',
},
totalCount: 1,
};
import { useLazyFetchAllRecords } from '@/object-record/hooks/useLazyFetchAllRecords';

const mockPerson = {
__typename: 'Person',
Expand Down Expand Up @@ -76,62 +64,8 @@ const mockPerson = {
workPreference: 'workPreference',
};

const mocks: MockedResponse[] = [
{
request: {
query: gql`
query FindManyPeople(
$filter: PersonFilterInput
$orderBy: [PersonOrderByInput]
$lastCursor: String
$limit: Int
) {
people(
filter: $filter
orderBy: $orderBy
first: $limit
after: $lastCursor
) {
edges {
node {
${PERSON_FRAGMENT_WITH_DEPTH_ZERO_RELATIONS}
}
cursor
}
pageInfo {
hasNextPage
hasPreviousPage
startCursor
endCursor
}
totalCount
}
}
`,
variables: {
filter: {},
limit: 30,
orderBy: [{ position: 'AscNullsFirst' }],
},
},
result: jest.fn(() => ({
data: {
people: {
...defaultResponseData,
edges: [
{
node: mockPerson,
cursor: '1',
},
],
},
},
})),
},
];

const WrapperWithResponse = getJestMetadataAndApolloMocksAndActionMenuWrapper({
apolloMocks: mocks,
const Wrapper = getJestMetadataAndApolloMocksAndActionMenuWrapper({
apolloMocks: [],
componentInstanceId: 'recordIndexId',
contextStoreTargetedRecordsRule: {
mode: 'selection',
Expand All @@ -140,43 +74,36 @@ const WrapperWithResponse = getJestMetadataAndApolloMocksAndActionMenuWrapper({
contextStoreCurrentObjectMetadataNameSingular: 'person',
});

const graphqlEmptyResponse = [
{
...mocks[0],
result: jest.fn(() => ({
data: {
people: {
...defaultResponseData,
edges: [],
},
},
})),
},
];

const WrapperWithEmptyResponse =
getJestMetadataAndApolloMocksAndActionMenuWrapper({
apolloMocks: graphqlEmptyResponse,
componentInstanceId: 'recordIndexId',
contextStoreTargetedRecordsRule: {
mode: 'selection',
selectedRecordIds: [],
},
contextStoreCurrentObjectMetadataNameSingular: 'person',
});
jest.mock('@/object-record/hooks/useLazyFetchAllRecords', () => ({
useLazyFetchAllRecords: jest.fn(),
}));

describe('useRecordData', () => {
const recordIndexId = 'people';
const objectMetadataItem = generatedMockObjectMetadataItems.find(
(item) => item.nameSingular === 'person',
);
let mockFetchAllRecords: jest.Mock;

beforeEach(() => {
// Mock the hook's implementation
mockFetchAllRecords = jest.fn();
(useLazyFetchAllRecords as jest.Mock).mockReturnValue({
progress: 100,
isDownloading: false,
fetchAllRecords: mockFetchAllRecords, // Mock the function
});
});
if (!objectMetadataItem) {
throw new Error('Object metadata item not found');
}

describe('data fetching', () => {
it('should handle no records', async () => {
const callback = jest.fn();

mockFetchAllRecords.mockReturnValue([]);

const { result } = renderHook(
() =>
useExportFetchRecords({
Expand All @@ -188,7 +115,7 @@ describe('useRecordData', () => {
viewType: ViewType.Kanban,
}),
{
wrapper: WrapperWithEmptyResponse,
wrapper: Wrapper,
},
);

Expand All @@ -203,6 +130,7 @@ describe('useRecordData', () => {

it('should call the callback function with fetched data', async () => {
const callback = jest.fn();
mockFetchAllRecords.mockReturnValue([mockPerson]);
const { result } = renderHook(
() =>
useExportFetchRecords({
Expand All @@ -212,7 +140,7 @@ describe('useRecordData', () => {
pageSize: 30,
delayMs: 0,
}),
{ wrapper: WrapperWithResponse },
{ wrapper: Wrapper },
);

await act(async () => {
Expand All @@ -226,6 +154,7 @@ describe('useRecordData', () => {

it('should call the callback function with kanban field included as column if view type is kanban', async () => {
const callback = jest.fn();
mockFetchAllRecords.mockReturnValue([mockPerson]);
const { result } = renderHook(
() => {
const [recordGroupFieldMetadata, setRecordGroupFieldMetadata] =
Expand Down Expand Up @@ -254,7 +183,7 @@ describe('useRecordData', () => {
};
},
{
wrapper: WrapperWithResponse,
wrapper: Wrapper,
},
);

Expand Down Expand Up @@ -316,6 +245,7 @@ describe('useRecordData', () => {

it('should not call the callback function with kanban field included as column if view type is table', async () => {
const callback = jest.fn();
mockFetchAllRecords.mockReturnValue([mockPerson]);
const { result } = renderHook(
() => {
const [recordGroupFieldMetadata, setRecordGroupFieldMetadata] =
Expand Down Expand Up @@ -345,7 +275,7 @@ describe('useRecordData', () => {
};
},
{
wrapper: WrapperWithResponse,
wrapper: Wrapper,
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ export const useExportFetchRecords = ({

const getTableData = async () => {
const result = await fetchAllRecords();
callback(result, finalColumns);
if (result.length > 0) {
callback(result, finalColumns);
}
};

return {
Expand Down

0 comments on commit ea0406d

Please sign in to comment.