Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export const StructuredContentCommands = Extension.create({
const structuredContentTags = getStructuredContentTagsById(id, state);

if (!structuredContentTags.length) {
return true;
return false;
}

const { schema } = editor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ describe('updateStructuredContentById', () => {
}).toThrow('Invalid structured content id - must be an integer, got: abc-123');
});

it('returns false when no structured content matches the requested ID', () => {
const originalDoc = editor.state.doc.toJSON();

const didUpdate = editor.commands.updateStructuredContentById('missing-structured-content-id', {
text: 'New Content',
});

expect(didUpdate).toBe(false);
expect(editor.state.doc.toJSON()).toEqual(originalDoc);
});

describe('keepTextNodeStyles option', () => {
it('preserves marks from the first text node when keepTextNodeStyles is true', () => {
const didUpdate = editor.commands.updateStructuredContentById(INLINE_ID, {
Expand Down
93 changes: 93 additions & 0 deletions packages/template-builder/src/tests/updateField.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { createRef } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act, cleanup, render, waitFor } from '@testing-library/react';
import SuperDocTemplateBuilder from '../index';
import type { SuperDocTemplateBuilderHandle } from '../types';

const updateStructuredContentByIdMock = vi.fn(() => false);

vi.mock('superdoc', () => {
class MockSuperDoc {
activeEditor: any;
superdocStore: any;

constructor(options: { onReady?: () => void }) {
this.activeEditor = {
state: {},
view: {
coordsAtPos: () => ({ left: 0, top: 0, bottom: 0 }),
},
commands: {
updateStructuredContentById: updateStructuredContentByIdMock,
},
helpers: {
structuredContentCommands: {
getStructuredContentTags: () => [],
},
},
on: vi.fn(),
};

this.superdocStore = {
documents: [{ getPresentationEditor: () => ({ coordsAtPos: () => ({ left: 0, top: 0, bottom: 0 }) }) }],
};

queueMicrotask(() => options.onReady?.());
}

destroy() {}

setDocumentMode() {}
}

return { SuperDoc: MockSuperDoc };
});

const renderBuilder = async (props = {}) => {
const ref = createRef<SuperDocTemplateBuilderHandle>();
const onReady = vi.fn();

render(
<SuperDocTemplateBuilder
ref={ref}
document={{ mode: 'editing' }}
fields={{ available: [] }}
onReady={onReady}
{...props}
/>,
);

await waitFor(() => expect(onReady).toHaveBeenCalledTimes(1));
await waitFor(() => expect(ref.current).not.toBeNull());

return ref;
};

describe('SuperDocTemplateBuilder updateField', () => {
beforeEach(() => {
updateStructuredContentByIdMock.mockReset();
updateStructuredContentByIdMock.mockReturnValue(false);
});

afterEach(() => {
cleanup();
});

it('returns false when the editor command does not update a field', async () => {
const onFieldUpdate = vi.fn();
const onFieldsChange = vi.fn();
const ref = await renderBuilder({ onFieldUpdate, onFieldsChange });
let result = true;

await act(async () => {
result = ref.current!.updateField('missing-field-id', { alias: 'New Name' });
});

expect(result).toBe(false);
expect(updateStructuredContentByIdMock).toHaveBeenCalledWith('missing-field-id', {
attrs: { alias: 'New Name' },
});
expect(onFieldUpdate).not.toHaveBeenCalled();
expect(onFieldsChange).not.toHaveBeenCalled();
});
});
Loading