Skip to content

Commit

Permalink
chore: cleanup typescript issues
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishelgert committed Oct 23, 2023
1 parent 70931cf commit a16bb22
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 61 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
- checkout
- yarn_install
- run: yarn lint
- run: yarn tsc
- run: yarn build
unit-tests:
executor: linux-node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ const locale = 'en-US';

describe('InspectorMode', () => {
let inspectorMode: InspectorMode;
const targetOrigin = ['https://app.contentful.com'];

beforeEach(() => {
inspectorMode = new InspectorMode({ locale });
inspectorMode = new InspectorMode({ locale, targetOrigin });
});

afterEach(() => {
Expand Down
16 changes: 8 additions & 8 deletions packages/live-preview-sdk/src/__tests__/react.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('useContentfulLiveUpdates', () => {
});

expect(subscribe).toHaveBeenCalledTimes(1);
expect(subscribe).toHaveBeenCalledWith({
expect(subscribe).toHaveBeenCalledWith('edit', {
data: initialData,
locale,
callback: expect.any(Function),
Expand Down Expand Up @@ -108,15 +108,15 @@ describe('useContentfulLiveUpdates', () => {

const updatedData1 = createData('4', 'Hello World');
act(() => {
subscribe.mock.calls[0][0].callback(updatedData1);
subscribe.mock.calls[0][1].callback(updatedData1);
vi.advanceTimersToNextTimer();
});

expect(result.current).toEqual(updatedData1);

const updatedData2 = createData('4', 'Hello World!');
act(() => {
subscribe.mock.calls[0][0].callback(updatedData2);
subscribe.mock.calls[0][1].callback(updatedData2);
vi.advanceTimersToNextTimer();
});

Expand All @@ -142,11 +142,11 @@ describe('useContentfulLiveUpdates', () => {

const updatedData = createData('5', 'Hello World');
act(() => {
subscribe.mock.calls[0][0].callback(createData('5', 'Hello W'));
subscribe.mock.calls[0][0].callback(createData('5', 'Hello Wo'));
subscribe.mock.calls[0][0].callback(createData('5', 'Hello Wor'));
subscribe.mock.calls[0][0].callback(createData('5', 'Hello Worl'));
subscribe.mock.calls[0][0].callback(updatedData);
subscribe.mock.calls[0][1].callback(createData('5', 'Hello W'));
subscribe.mock.calls[0][1].callback(createData('5', 'Hello Wo'));
subscribe.mock.calls[0][1].callback(createData('5', 'Hello Wor'));
subscribe.mock.calls[0][1].callback(createData('5', 'Hello Worl'));
subscribe.mock.calls[0][1].callback(updatedData);
vi.advanceTimersToNextTimer();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock('@contentful/visual-sdk');

describe('resolveReference', () => {
const locale = 'en-US';
const sendMessage = vi.fn();

beforeEach(() => {
(EditorEntityStore as Mock).mockImplementation(() => ({
Expand Down Expand Up @@ -51,6 +52,7 @@ describe('resolveReference', () => {
entityReferenceMap: new Map([['1', asset]]),
referenceId: '1',
locale,
sendMessage,
});

expect(result).toEqual(expected);
Expand All @@ -62,6 +64,7 @@ describe('resolveReference', () => {
referenceId: cdaAsset.sys.id,
isAsset: true,
locale,
sendMessage,
});

expect(result.typeName).toBe('Asset');
Expand Down Expand Up @@ -102,6 +105,7 @@ describe('resolveReference', () => {
entityReferenceMap: new Map([['1', entry]]),
referenceId: '1',
locale,
sendMessage,
});

expect(result).toEqual(expected);
Expand All @@ -112,6 +116,7 @@ describe('resolveReference', () => {
entityReferenceMap: new Map(),
referenceId: cdaEntry.sys.id,
locale,
sendMessage,
});

expect(result.typeName).toBe('TopicProductFeature');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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();

beforeEach(() => {
(resolveReference as Mock).mockResolvedValue({
Expand Down Expand Up @@ -59,6 +60,7 @@ describe('Update GraphQL Entry', () => {
updateFromEntryEditor: update,
locale,
entityReferenceMap,
sendMessage,
});
};

Expand Down
52 changes: 48 additions & 4 deletions packages/live-preview-sdk/src/graphql/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { BLOCKS, INLINES } from '@contentful/rich-text-types';
import { Asset, Entry } from 'contentful';
import type { SetOptional } from 'type-fest';

import { isPrimitiveField, updatePrimitiveField, resolveReference, clone, debug } from '../helpers';
import {
isPrimitiveField,
updatePrimitiveField,
resolveReference,
clone,
debug,
SendMessage,
} from '../helpers';
import { SUPPORTED_RICHTEXT_EMBEDS, isAsset, isRichText } from '../helpers/entities';
import {
CollectionItem,
Expand Down Expand Up @@ -35,6 +42,7 @@ export async function updateEntry({
locale,
entityReferenceMap,
gqlParams,
sendMessage,
}: UpdateEntryProps): Promise<Entity & { sys: SysProps }> {
if (dataFromPreviewApp.sys.id !== updateFromEntryEditor.sys.id) {
return dataFromPreviewApp;
Expand All @@ -59,6 +67,7 @@ export async function updateEntry({
name,
locale,
gqlParams,
sendMessage,
});
} else if (field.type === 'Link') {
await updateSingleRefField({
Expand All @@ -68,6 +77,7 @@ export async function updateEntry({
locale,
entityReferenceMap,
gqlParams,
sendMessage,
});
} else if (field.type === 'Array' && field.items?.type === 'Link') {
await updateMultiRefField({
Expand All @@ -77,6 +87,7 @@ export async function updateEntry({
locale,
entityReferenceMap,
gqlParams,
sendMessage,
});
}
}
Expand All @@ -101,6 +112,7 @@ async function processNode(
assets: RichTextLink,
entityReferenceMap: EntityReferenceMap,
locale: string,
sendMessage: SendMessage,
gqlParams?: GraphQLParams
) {
// Check if the node is an embedded entity
Expand All @@ -119,6 +131,7 @@ async function processNode(
entityReferenceMap,
locale,
gqlParams,
sendMessage,
});
} else if (node.data.target.sys.linkType === 'Asset') {
ref = await updateReferenceAssetField({
Expand All @@ -127,6 +140,7 @@ async function processNode(
entityReferenceMap,
locale,
gqlParams,
sendMessage,
});
}

Expand Down Expand Up @@ -160,7 +174,15 @@ async function processNode(
// since embedded entries can be part of other rich text content (e.g. embedded inline entries)
// we need to recursively check for these entries to display them
for (const contentNode of node.content) {
await processNode(contentNode, entries, assets, entityReferenceMap, locale, gqlParams);
await processNode(
contentNode,
entries,
assets,
entityReferenceMap,
locale,
sendMessage,
gqlParams
);
}
}
}
Expand All @@ -169,14 +191,15 @@ async function processRichTextField(
richTextNode: any | null,
entityReferenceMap: EntityReferenceMap,
locale: string,
sendMessage: SendMessage,
gqlParams?: GraphQLParams
): Promise<{ entries: RichTextLink; assets: RichTextLink }> {
const entries: RichTextLink = { block: [], inline: [], hyperlink: [] };
const assets: RichTextLink = { block: [], inline: [], hyperlink: [] };

if (richTextNode) {
for (const node of richTextNode.content) {
await processNode(node, entries, assets, entityReferenceMap, locale, gqlParams);
await processNode(node, entries, assets, entityReferenceMap, locale, sendMessage, gqlParams);
}
}

Expand All @@ -193,6 +216,7 @@ async function updateRichTextField({
locale,
entityReferenceMap,
gqlParams,
sendMessage,
}: UpdateFieldProps) {
if (!dataFromPreviewApp[name]) {
dataFromPreviewApp[name] = {};
Expand All @@ -207,6 +231,7 @@ async function updateRichTextField({
dataFromPreviewApp[name].json,
entityReferenceMap,
locale,
sendMessage,
gqlParams
);
}
Expand All @@ -217,12 +242,14 @@ async function updateReferenceAssetField({
entityReferenceMap,
locale,
gqlParams,
sendMessage,
}: SetOptional<Required<UpdateReferenceFieldProps>, 'gqlParams'>) {
const { reference } = await resolveReference({
entityReferenceMap,
referenceId: updatedReference.sys.id,
isAsset: true,
locale,
sendMessage,
});

return updateAsset(
Expand All @@ -242,11 +269,13 @@ async function updateReferenceEntryField({
entityReferenceMap,
locale,
gqlParams,
sendMessage,
}: SetOptional<Required<UpdateReferenceFieldProps>, 'gqlParams'>) {
const { reference, typeName } = await resolveReference({
entityReferenceMap,
referenceId: updatedReference.sys.id,
locale,
sendMessage,
});

// If we have the typename of the updated reference, we can work with it
Expand All @@ -267,7 +296,13 @@ async function updateReferenceEntryField({
if (isRichText(value)) {
// richtext
merged[key] = { json: value };
merged[key].links = await processRichTextField(value, entityReferenceMap, locale);
merged[key].links = await processRichTextField(
value,
entityReferenceMap,
locale,
sendMessage,
gqlParams
);
}

if ('sys' in value) {
Expand All @@ -280,6 +315,7 @@ async function updateReferenceEntryField({
entityReferenceMap,
name: key,
gqlParams,
sendMessage,
});
}
} else if (Array.isArray(value) && value[0]?.sys) {
Expand All @@ -293,6 +329,7 @@ async function updateReferenceEntryField({
entityReferenceMap,
name: key,
gqlParams,
sendMessage,
});
} else {
// primitive fields
Expand All @@ -309,6 +346,7 @@ async function updateReferenceField({
entityReferenceMap,
locale,
gqlParams,
sendMessage,
}: UpdateReferenceFieldProps) {
if (!updatedReference) {
return null;
Expand All @@ -330,6 +368,7 @@ async function updateReferenceField({
entityReferenceMap,
locale,
gqlParams,
sendMessage,
});
}

Expand All @@ -339,6 +378,7 @@ async function updateReferenceField({
entityReferenceMap,
locale,
gqlParams,
sendMessage,
});
}

Expand All @@ -349,6 +389,7 @@ async function updateSingleRefField({
locale,
entityReferenceMap,
gqlParams,
sendMessage,
}: UpdateFieldProps) {
const updatedReference = updateFromEntryEditor?.fields?.[name] as Asset | Entry | undefined;
dataFromPreviewApp[name] = await updateReferenceField({
Expand All @@ -359,6 +400,7 @@ async function updateSingleRefField({
entityReferenceMap: entityReferenceMap as EntityReferenceMap,
locale,
gqlParams,
sendMessage,
});
}

Expand All @@ -369,6 +411,7 @@ async function updateMultiRefField({
locale,
entityReferenceMap,
gqlParams,
sendMessage,
}: UpdateFieldProps) {
const fieldName = buildCollectionName(name);

Expand All @@ -387,6 +430,7 @@ async function updateMultiRefField({
entityReferenceMap: entityReferenceMap as EntityReferenceMap,
locale,
gqlParams,
sendMessage,
});

return result;
Expand Down
Loading

0 comments on commit a16bb22

Please sign in to comment.