Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gql): inifite loop with circular references [] #383

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 28 additions & 93 deletions packages/live-preview-sdk/src/__tests__/resolveReference.spec.ts
Original file line number Diff line number Diff line change
@@ -1,126 +1,61 @@
/* @vitest-environment jsdom */
import { EditorEntityStore } from '@contentful/visual-sdk';
import type { Asset, Entry } from 'contentful';
import { describe, vi, it, beforeEach, afterEach, expect, Mock } from 'vitest';

import { resolveReference } from '../helpers/resolveReference';
import cdaAsset from './fixtures/cdaAsset.json';
import cdaEntry from './fixtures/cdaEntry.json';
import type { GetStore } from '../types';

vi.mock('@contentful/visual-sdk');

describe('resolveReference', () => {
const locale = 'en-US';
const sendMessage = vi.fn();
const getStore = vi.fn<Parameters<GetStore>, ReturnType<GetStore>>();
let store: EditorEntityStore;

beforeEach(() => {
(EditorEntityStore as Mock).mockImplementation(() => ({
__mocked__: true,
fetchEntry: async () => cdaEntry,
fetchAsset: async () => cdaAsset,
}));

store = new EditorEntityStore({
entities: [],
locale,
sendMessage: vi.fn(),
subscribe: vi.fn(),
timeoutDuration: 10,
});

getStore.mockImplementation(() => store);
});

afterEach(() => {
vi.clearAllMocks();
});

describe('asset', () => {
const asset = {
fields: {
file: { fileName: 'abc.jpg' },
},
sys: {
id: '1',
},
} as unknown as Asset;

const expected = {
reference: {
fields: {
file: { fileName: 'abc.jpg' },
},
sys: {
id: '1',
},
},
typeName: 'Asset',
};

it('resolves it directly from the map', async () => {
const result = await resolveReference({
entityReferenceMap: new Map([['1', asset]]),
referenceId: '1',
locale,
sendMessage,
});

expect(result).toEqual(expected);
it('resolves an assets correctly from the store', async () => {
const result = await resolveReference({
referenceId: cdaAsset.sys.id,
isAsset: true,
locale,
getStore,
});

it('resolves it from the editor', async () => {
const result = await resolveReference({
entityReferenceMap: new Map(),
referenceId: cdaAsset.sys.id,
isAsset: true,
locale,
sendMessage,
});

expect(result.typeName).toBe('Asset');
expect(result.reference.fields).toEqual(cdaAsset.fields);
});
expect(result.typeName).toBe('Asset');
expect(result.reference.fields).toEqual(cdaAsset.fields);
});

describe('entries', () => {
const entry = {
fields: {
title: 'Hello World',
},
sys: {
id: '1',
contentType: {
sys: { id: 'helloWorld' },
},
},
} as unknown as Entry;

const expected = {
reference: {
fields: {
title: 'Hello World',
},
sys: {
id: '1',
contentType: {
sys: { id: 'helloWorld' },
},
},
},
typeName: 'HelloWorld',
};

it('resolves it directly from the map', async () => {
const result = await resolveReference({
entityReferenceMap: new Map([['1', entry]]),
referenceId: '1',
locale,
sendMessage,
});

expect(result).toEqual(expected);
it('resolves an entry correctly from the store', async () => {
const result = await resolveReference({
referenceId: cdaEntry.sys.id,
locale,
getStore,
});

it('resolves it from the editor', async () => {
const result = await resolveReference({
entityReferenceMap: new Map(),
referenceId: cdaEntry.sys.id,
locale,
sendMessage,
});

expect(result.typeName).toBe('TopicProductFeature');
expect(result.reference.fields).toEqual(cdaEntry.fields);
});
expect(result.typeName).toBe('TopicProductFeature');
expect(result.reference.fields).toEqual(cdaEntry.fields);
});
});
1 change: 1 addition & 0 deletions packages/live-preview-sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const TOOLTIP_HEIGHT = 32;
export const TOOLTIP_PADDING_LEFT = 5;

export const MAX_DEPTH = 10;
export const MAX_RTE_DEPTH = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we need 2 for rich text?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 Level depth for the direct references
Assuming it's an entry it could be relevant to resolve also the asset of this entry, therefore then 2


export const LIVE_PREVIEW_EDITOR_SOURCE = 'live-preview-editor' as const;
export const LIVE_PREVIEW_SDK_SOURCE = 'live-preview-sdk' as const;
11 changes: 5 additions & 6 deletions packages/live-preview-sdk/src/graphql/__tests__/entries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { describe, it, expect, vi, afterEach, beforeEach, Mock } from 'vitest';

import * as Utils from '../../helpers';
import { resolveReference } from '../../helpers/resolveReference';
import { SysProps, EntityReferenceMap, Entity, ContentType } from '../../types';
import type { SysProps, Entity, ContentType, GetStore } from '../../types';
import { updateEntry } from '../entries';
import defaultContentTypeJSON from './fixtures/contentType.json';
import entry from './fixtures/entry.json';
import { MAX_DEPTH } from '../../constants';

const EN = 'en-US';

Expand All @@ -19,7 +20,7 @@ const defaultContentType = defaultContentTypeJSON as ContentTypeProps;
// Note: we can get rid of expect.objectContaining, if we iterate over the provided data instead of the ContentType.fields
describe('Update GraphQL Entry', () => {
const testReferenceId = '18kDTlnJNnDIJf6PsXE5Mr';
const sendMessage = vi.fn();
const getStore = vi.fn<Parameters<GetStore>, ReturnType<GetStore>>();

beforeEach(() => {
(resolveReference as Mock).mockResolvedValue({
Expand All @@ -45,22 +46,20 @@ describe('Update GraphQL Entry', () => {
data,
update = entry as unknown as Entry,
locale = EN,
entityReferenceMap = new EntityReferenceMap(),
contentType = defaultContentType,
}: {
data: Entity & { sys: SysProps };
update?: Entry;
locale?: string;
entityReferenceMap?: EntityReferenceMap;
contentType?: ContentType;
}) => {
return updateEntry({
contentType,
dataFromPreviewApp: data,
updateFromEntryEditor: update,
locale,
entityReferenceMap,
sendMessage,
getStore,
maxDepth: MAX_DEPTH,
});
};

Expand Down
Loading