Skip to content

Commit

Permalink
Updating tests:
Browse files Browse the repository at this point in the history
  • Loading branch information
cohitre committed Feb 14, 2024
1 parent 85ad0cd commit a66a312
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 44 deletions.
1 change: 1 addition & 0 deletions src/builders/buildDocumentReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function buildDocumentReader<T extends BaseZodDictionary>(blocks:
const useBlock = (id: string) => useDocument()[id];

return {
useDocument,
useBlock,
Block: ({ id }: { id: string }) => {
const block = useBlock(id);
Expand Down
97 changes: 64 additions & 33 deletions tests/builder/buildDocumentEditor.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { act, render } from '@testing-library/react';
import buildDocumentEditor from '../../src/builders/buildDocumentEditor';

describe('builders/buildDocumentEditor', () => {
const { useBlockState, Block, DocumentEditorProvider } = buildDocumentEditor({
const { useDocumentState, useBlockState, Block, DocumentEditorProvider } = buildDocumentEditor({
SampleBlock: {
schema: z.object({ text: z.string() }),
Component: ({ text }) => <div>{text.toUpperCase()}</div>,
Expand All @@ -21,50 +21,81 @@ describe('builders/buildDocumentEditor', () => {
},
};

describe('#useBlockState', () => {
describe('#useDocumentState', () => {
it('returns a getter and a setter tuple', () => {
let value: any;
let setValue: any;
const ViewBlockConfig = ({ id }: { id: string }) => {
const tuple = useBlockState(id);
value = tuple[0];
setValue = tuple[1];
return (
<pre>
{tuple[0].type} - {tuple[0].data.text}
</pre>
);
let tuple: any;
const ViewBlockConfig = () => {
tuple = useDocumentState();
return <pre>{JSON.stringify(tuple[0])}</pre>;
};

expect(
render(
<DocumentEditorProvider value={SAMPLE_DATA}>
<ViewBlockConfig id="my id" />
</DocumentEditorProvider>
).queryAllByText('SampleBlock - Test text!')
).toHaveLength(1);
const NODE = (
<DocumentEditorProvider value={SAMPLE_DATA}>
<ViewBlockConfig />
</DocumentEditorProvider>
);
const { rerender } = render(NODE);
expect(tuple[0]).toEqual({
'my id': {
id: 'my id',
type: 'SampleBlock',
data: { text: 'Test text!' },
},
});

act(() => {
setValue({
id: 'my id',
tuple[1]({
'another id': {
id: 'another id',
type: 'SampleBlock',
data: { text: 'changed text?' },
},
});
});

rerender(NODE);
expect(tuple[0]).toEqual({
'another id': {
id: 'another id',
type: 'SampleBlock' as const,
data: { text: 'changed text?' },
},
});
});
});

describe('#useBlockState', () => {
it('returns a getter and a setter tuple', () => {
let tuple: any;
const ViewBlockConfig = () => {
tuple = useBlockState('my id');
return <pre>{JSON.stringify(tuple[0])}</pre>;
};

const NODE = (
<DocumentEditorProvider value={SAMPLE_DATA}>
<ViewBlockConfig />
</DocumentEditorProvider>
);
const { rerender } = render(NODE);
expect(tuple[0]).toEqual({
id: 'my id',
type: 'SampleBlock',
data: { text: 'Test text!' },
});

act(() => {
tuple[1]({
...tuple[0],
data: { text: 'changed text?' },
});
});

expect(
render(
<DocumentEditorProvider value={SAMPLE_DATA}>
<ViewBlockConfig id="my id" />
</DocumentEditorProvider>
).queryAllByText('SampleBlock - changed text?')
).toHaveLength(1);
expect(value).toEqual({
rerender(NODE);
expect(tuple[0]).toEqual({
id: 'my id',
type: 'SampleBlock',
data: {
text: 'Test text!',
},
data: { text: 'changed text?' },
});
});
});
Expand Down
48 changes: 37 additions & 11 deletions tests/builder/buildDocumentReader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { render } from '@testing-library/react';
import buildDocumentReader from '../../src/builders/buildDocumentReader';

describe('builders/buildDocumentReader', () => {
const { DocumentReaderProvider, Block, useBlock } = buildDocumentReader({
const { DocumentReaderProvider, Block, useBlock, useDocument } = buildDocumentReader({
SampleBlock: {
schema: z.object({ text: z.string() }),
Component: ({ text }) => <div>{text.toUpperCase()}</div>,
Expand All @@ -21,19 +21,45 @@ describe('builders/buildDocumentReader', () => {
},
};

describe('#useDocument', () => {
it('gets the configurations dictionary', () => {
let RESULT;
const ViewBlockConfig = () => {
RESULT = useDocument();
return <pre>{JSON.stringify(RESULT)}</pre>;
};
render(
<DocumentReaderProvider value={SAMPLE_DATA}>
<ViewBlockConfig />
</DocumentReaderProvider>
);
expect(RESULT).toEqual({
'my id': {
id: 'my id',
type: 'SampleBlock',
data: { text: 'Test text!' },
},
});
});
});

describe('#useBlock', () => {
it('gets the value given an id', () => {
const ViewBlockConfig = ({ id }: { id: string }) => {
const c = useBlock(id);
return <pre>{JSON.stringify(c)}</pre>;
let RESULT;
const ViewBlockConfig = () => {
RESULT = useBlock('my id');
return <pre>{JSON.stringify(RESULT)}</pre>;
};
expect(
render(
<DocumentReaderProvider value={SAMPLE_DATA}>
<ViewBlockConfig id="my id" />
</DocumentReaderProvider>
).queryAllByText('{"id":"my id","type":"SampleBlock","data":{"text":"Test text!"}}')
).toHaveLength(1);
render(
<DocumentReaderProvider value={SAMPLE_DATA}>
<ViewBlockConfig />
</DocumentReaderProvider>
);
expect(RESULT).toEqual({
id: 'my id',
type: 'SampleBlock',
data: { text: 'Test text!' },
});
});
});

Expand Down

0 comments on commit a66a312

Please sign in to comment.