Skip to content

Commit

Permalink
chore: fix text types
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarosabu committed Oct 17, 2024
1 parent 58d129c commit 19dfeb2
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions src/richtext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest';
import { richTextResolver } from './richtext';
import { createTextVNode, h } from 'vue';
import type { VNode } from 'vue';
import type { BlockTypes, MarkTypes, type Node, NodeResolver } from './types';
import { BlockTypes, MarkTypes, StoryblokRichTextNode, StoryblokRichTextNodeResolver } from './types';
import { StoryblokComponent } from '@storyblok/vue';

describe('richtext', () => {
Expand All @@ -18,7 +18,7 @@ describe('richtext', () => {
},
],
};
const html = render(paragraph as Node<string>);
const html = render(paragraph as StoryblokRichTextNode<string>);
expect(html).toBe('<p key="p-2">Hello, world!</p>');
});

Expand All @@ -36,7 +36,7 @@ describe('richtext', () => {
},
],
};
const html = render(heading as Node<string>);
const html = render(heading as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<h1 key="h1-2">Headline 1</h1>');
});

Expand Down Expand Up @@ -65,7 +65,7 @@ describe('richtext', () => {
},
],
};
const html = render(list as Node<string>);
const html = render(list as StoryblokRichTextNode<string>);
expect(html).toBe('<ul key="ul-5"><li key="li-3">Item 1</li><li key="li-5">Item 2</li></ul>');
});

Expand Down Expand Up @@ -95,7 +95,7 @@ describe('richtext', () => {
},
],
};
const html = render(list as Node<string>);
const html = render(list as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<ol order="1" key="ol-5"><li key="li-3">Item 1</li><li key="li-5">Item 2</li></ol>');
});

Expand All @@ -116,7 +116,7 @@ describe('richtext', () => {
},
},
};
const html = render(image as Node<string>);
const html = render(image as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<img src="https://example.com/image.jpg" alt="An image" title="An image" key="img-1" />');
});

Expand All @@ -136,7 +136,7 @@ describe('richtext', () => {
const node = {
type: BlockTypes[type as keyof typeof BlockTypes],
};
const html = render(node as Node<string>);
const html = render(node as StoryblokRichTextNode<string>);

expect(html).toBe(`<${tagMap[type]} key="${tagMap[type]}-${index + 1}" />`);
});
Expand All @@ -150,7 +150,7 @@ describe('richtext', () => {
emoji: '🚀',
},
};
const html = render(emoji as Node<string>);
const html = render(emoji as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<span data-type="emoji" data-name="undefined" emoji="🚀" key="emoji-1"><img src="undefined" alt="undefined" style="width: 1.25em; height: 1.25em; vertical-align: text-top" draggable="false" loading="lazy" /></span>');
});

Expand All @@ -165,7 +165,7 @@ describe('richtext', () => {
},
],
};
const html = render(code as Node<string>);
const html = render(code as StoryblokRichTextNode<string>);
expect(html).toBe('<pre key="code-2"><code key="code-2">console.log(&quot;Hello, world!&quot;)</code></pre>');
});

Expand All @@ -174,7 +174,7 @@ describe('richtext', () => {
const hr = {
type: 'horizontal_rule',
};
const html = render(hr as Node<string>);
const html = render(hr as StoryblokRichTextNode<string>);
expect(html).toBe('<hr key="hr-1" />');
});

Expand All @@ -183,7 +183,7 @@ describe('richtext', () => {
const br = {
type: 'hard_break',
};
const html = render(br as Node<string>);
const html = render(br as StoryblokRichTextNode<string>);
expect(html).toBe('<br key="br-1" />');
});

Expand All @@ -203,7 +203,7 @@ describe('richtext', () => {
},
],
};
const html = render(quote as Node<string>);
const html = render(quote as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<blockquote key="blockquote-3"><p key="p-3">Quote</p></blockquote>');
});
});
Expand All @@ -221,7 +221,7 @@ describe('richtext', () => {
},
],
};
const html = render(text as Node<string>);
const html = render(text as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<p key="p-5"><em key="em-5"><strong key="strong-4">Bold and italic</strong></em></p>');
});

Expand All @@ -237,7 +237,7 @@ describe('richtext', () => {
},
],
};
const html = render(text as Node<string>);
const html = render(text as unknown as StoryblokRichTextNode<string>);
// Update the expected HTML to reflect the styles
expect(html).toBe('<p key="p-5"><span style="color: blue" key="span-5"><span style="color: red" key="span-4">Bold and italic</span></span></p>');
});
Expand All @@ -258,7 +258,7 @@ describe('richtext', () => {
},
],
};
const html = render(link as Node<string>);
const html = render(link as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<a target="_blank" href="https://alvarosaburido.dev" key="a-3">External link</a>');
});

Expand All @@ -278,7 +278,7 @@ describe('richtext', () => {
},
],
};
const html = render(link as Node<string>);
const html = render(link as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<a target="_self" href="#anchor" key="a-3">Anchor link</a>');
});

Expand All @@ -297,7 +297,7 @@ describe('richtext', () => {
},
],
};
const html = render(link as Node<string>);
const html = render(link as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<a href="mailto:[email protected]" key="a-3">[email protected]</a>');
});

Expand All @@ -319,7 +319,7 @@ describe('richtext', () => {
},
],
};
const html = render(link as Node<string>);
const html = render(link as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<a uuid="2bbf3ee7-acbe-401c-ade5-cf33e6e0babb" target="_blank" href="/" key="a-3">Internal Link</a>');
});

Expand All @@ -338,7 +338,7 @@ describe('richtext', () => {
},
],
};
const html = render(link as Node<string>);
const html = render(link as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<a href="https://a.storyblok.com/f/67536/400x303/ccbe9ca7b3/nuxt-logo.png" key="a-3">Asset link</a>');
});

Expand All @@ -349,7 +349,7 @@ describe('richtext', () => {
type: 'text',
marks: [{ type: 'bold' }],
};
const html = render(bold as Node<string>);
const html = render(bold as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<strong key="strong-3">Bold</strong>');
});

Expand All @@ -360,7 +360,7 @@ describe('richtext', () => {
type: 'text',
marks: [{ type: 'italic' }],
};
const html = render(italic as Node<string>);
const html = render(italic as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<em key="em-3">Italic</em>');
});

Expand All @@ -371,7 +371,7 @@ describe('richtext', () => {
type: 'text',
marks: [{ type: 'underline' }],
};
const html = render(underline as Node<string>);
const html = render(underline as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<u key="u-3">Underline</u>');
});

Expand All @@ -382,7 +382,7 @@ describe('richtext', () => {
type: 'text',
marks: [{ type: 'strike' }],
};
const html = render(strike as Node<string>);
const html = render(strike as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<s key="s-3">Strike</s>');
});

Expand All @@ -393,7 +393,7 @@ describe('richtext', () => {
type: 'text',
marks: [{ type: 'code' }],
};
const html = render(code as Node<string>);
const html = render(code as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<code key="code-3">Code</code>');
});

Expand All @@ -404,7 +404,7 @@ describe('richtext', () => {
type: 'text',
marks: [{ type: 'superscript' }],
};
const html = render(superscript as Node<string>);
const html = render(superscript as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<sup key="sup-3">Superscript</sup>');
});

Expand All @@ -415,7 +415,7 @@ describe('richtext', () => {
type: 'text',
marks: [{ type: 'subscript' }],
};
const html = render(subscript as Node<string>);
const html = render(subscript as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<sub key="sub-3">Subscript</sub>');
});

Expand All @@ -426,7 +426,7 @@ describe('richtext', () => {
type: 'text',
marks: [{ type: 'highlight' }],
};
const html = render(highlight as Node<string>);
const html = render(highlight as unknown as StoryblokRichTextNode<string>);
expect(html).toBe('<mark key="mark-3">Highlight</mark>');
});
});
Expand All @@ -444,7 +444,7 @@ describe('richtext', () => {
},
],
};
const vnode = render(paragraph as Node<string>);
const vnode = render(paragraph as StoryblokRichTextNode<VNode>);
expect(vnode.__v_isVNode).toBeTruthy();
expect(vnode.type).toBe('p');
});
Expand All @@ -462,15 +462,15 @@ describe('richtext', () => {
},
],
};
const vnode = render(paragraph as Node<string>);
const vnode = render(paragraph as StoryblokRichTextNode<VNode>);
expect(vnode?.children[0].children).toBe('Hello, world!');
});
it('should overwrite a resolver', async () => {
const RouterLink = h('span');

const { render } = richTextResolver({
resolvers: {
[MarkTypes.LINK]: (node: Node<VNode>) => h(RouterLink, {
[MarkTypes.LINK]: (node: StoryblokRichTextNode<VNode>) => h(RouterLink, {
to: node.attrs?.href,
target: node.attrs?.target,
}, node.text),
Expand All @@ -492,13 +492,13 @@ describe('richtext', () => {
},
],
};
const node = render(link as Node<string>);
const node = render(link as unknown as StoryblokRichTextNode<VNode>);
expect(node.type).toBe('span');
expect(node.props.to).toBe('/about');
expect(node?.props?.to).toBe('/about');
});

it('should render a blok component', async () => {
const componentResolver: NodeResolver<VNode> = (node: Node<VNode>): VNode => {
const componentResolver: StoryblokRichTextNodeResolver<VNode> = (node: StoryblokRichTextNode<VNode>): VNode => {
return h(StoryblokComponent, {
blok: node?.attrs?.body[0],
id: node.attrs?.id,
Expand Down Expand Up @@ -531,8 +531,8 @@ describe('richtext', () => {
],
},
};
const vnode = render(paragraph as Node<string>);
expect(vnode.props.blok.component).toBe('test-button');
const vnode = render(paragraph as unknown as StoryblokRichTextNode<VNode>);
expect(vnode?.props?.blok.component).toBe('test-button');
});
});
});

0 comments on commit 19dfeb2

Please sign in to comment.