Skip to content
Draft
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
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ First, import `render` from `riteway/render-component`:

```js
import render from 'riteway/render-component';
// or you can also use renderReact (which is the same as the default export)
import { renderReact } from 'riteway/render-component';
```

```js
Expand All @@ -478,6 +480,35 @@ describe('MyComponent', async assert => {
});
```

### Render Lit

To use this, import `renderLit` from `riteway/render-component`:

```js
import { renderLit } from 'riteway/render-component';
```

```js
renderLit = (TemplateResult) => CheerioObject
```

Take a [TemplateResult](https://lit.dev/docs/v1/api/lit-html/templates/#TemplateResult) Lit type and returns a [Cheerio object](https://cheerio.js.org/), that can be used in the exact same way as aforementioned default `render` function which is specifically for React.

#### Example

```js
describe('MyComponent', async assert => {
const $ = renderLit(html`<div>Test</div>`);

assert({
given: 'no params',
should: 'render something with the my-component class',
actual: $('.my-component').length,
expected: 1
});
});
```


## Match

Expand Down
23 changes: 22 additions & 1 deletion esm/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ describe('skip()', async assert => {
});
}

{
// @ts-ignore
// eslint-disable-next-line

describe('renderLitComponent', async assert => {
const { html } = await import('lit');

const text = 'Test for whatever you like!';
const $ = renderLit(html`<div>Text</div>`);
const contains = match($('.contents').html());

assert({
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test would fail, right?

given: 'A lit component',
should: 'return a working cheerio instance',
actual: contains(text),
expected: text
});
});
}

describe('countKeys()', async assert => {
assert({
given: 'an object',
Expand All @@ -151,4 +171,5 @@ describe('countKeys()', async assert => {
});
});

import './match-test';
import './match-test';import { renderLit } from '../source/render-component';

142 changes: 142 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@
"watch": "^0.13.0"
},
"dependencies": {
"@open-wc/testing-helpers": "^3.0.1",
"cheerio": "1.0.0-rc.12",
"esm": "3.2.25",
"lit": "^3.1.3",
"react-dom": "18.2.0",
"tape": "^5.7.4",
"vitest": "^1.2.1"
Expand Down
5 changes: 5 additions & 0 deletions render-component.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/// <reference types="cheerio" />
/// <reference types="react" />
/// <reference types="lit" />

import { TemplateResult } from "lit";

declare module 'riteway/render-component' {
export function renderLit(el: TemplateResult): cheerio.Root;
export function renderReact(el: JSX.Element): cheerio.Root;
export default function render(el: JSX.Element): cheerio.Root;
}
16 changes: 13 additions & 3 deletions source/render-component.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import reactDom from 'react-dom/server';
import dom from 'cheerio';
// import { fixture } from '@open-wc/testing-helpers';
// import { html } from 'lit';

const render = component =>
dom.load(reactDom.renderToStaticMarkup(component));
// export const renderLit = async (template) => {
// const el = await fixture(html`${template}`);
// await el.updateComplete; // Wait for the component to finish rendering
// if (el.shadowRoot) {
// return dom.load(el.shadowRoot.innerHTML);
// }
// return dom.load(el.innerHTML);
// };

export default render;
export const renderReact = (component) =>
dom.load(reactDom.renderToStaticMarkup(component));

export default renderReact;
17 changes: 15 additions & 2 deletions source/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import tape from 'tape';

import { describe, Try, createStream, countKeys } from './riteway';
import match from './match';
import render from './render-component';
import render, { renderReact } from './render-component';

// a function to test
const sum = (...args) => {
Expand Down Expand Up @@ -128,7 +128,20 @@ describe('skip()', async assert => {
// eslint-disable-next-line
const MyComponent = ({text}) => <div className="contents">{text}</div>;

describe('renderComponent', async assert => {
describe('renderComponent: renderRecat', async assert => {
const text = 'Test for whatever you like!';
const $ = renderReact(<MyComponent text={ text }/>);
const contains = match($('.contents').html());

assert({
given: 'A react component',
should: 'return a working cheerio instance',
actual: contains(text),
expected: text
});
});

describe('renderComponent: default export', async assert => {
const text = 'Test for whatever you like!';
const $ = render(<MyComponent text={ text }/>);
const contains = match($('.contents').html());
Expand Down