/* eslint-env jest */ globalThis.IS_REACT_ACT_ENVIRONMENT = true; import React, { act } from 'react'; import { createRoot } from 'react-dom/client'; import { renderToStaticMarkup, renderToString } from 'react-dom/server'; import renderer from 'react-test-renderer'; import { render } from '@testing-library/react'; import snapshot from 'jest-snapshot'; import diff from 'jest-diff'; import { html, oneLineTrim } from 'common-tags'; import htmrServer from '../src'; import htmrBrowser from '../src/index.browser'; describe('core', () => { test('it works', () => { testRender('

This is cool

'); }); // https://github.com/pveyes/htmr/issues/156 test('does not set generic key on a single root node', () => { ['', '

a

', '

ab

'].forEach( (fixture) => { const nodes = [htmrServer(fixture), htmrBrowser(fixture)]; nodes.forEach((node) => { const root = Array.isArray(node) ? node.filter(Boolean)[0] : node; expect(root.key).not.toBe('0'); }); } ); expect(htmrServer('').key).toBeNull(); expect(htmrBrowser('').key).toBeNull(); }); test('keeps keys on multiple root nodes', () => { [htmrServer, htmrBrowser].forEach((htmr) => { expect(htmr('

a

b

').map((n) => n.key)).toEqual(['0', '1']); }); }); test('throws if first argument is not a string', () => { const error = new Error('Expected HTML string'); const fixtures = [null, [], {}, 1, true]; // server & browser expect.assertions(fixtures.length * 2); fixtures.forEach((fixture) => { expect(() => htmrServer(fixture)).toThrow(error); expect(() => htmrBrowser(fixture)).toThrow(error); }); }); test('can render self closing component', () => { testRender(''); testRender('
'); testRender('
'); }); test('can render sibling element as multiple children', () => { testRender('

Multi

Component

'); }); test('can render element inside text node', () => { testRender('what are you doing?'); }); test('ignore comment', () => { testRender('
no comment
'); }); test('ignore multiline comment', () => { testRender(oneLineTrim`
  • no multiline comment
  • `); }); }); describe('attributes', () => { test('correctly map HTML attributes to react props', () => { testRender(''); testRender( '
    ' ); testRender(''); testRender(oneLineTrim` `); testRender(''); testRender(''); testRender( '' ); testRender(''); testRender(''); testRender('alt'); testRender(html`

    Avatar

    `); }); // https://github.com/pveyes/htmr/issues/103 test('correctly handle boolean attributes', () => { const { container } = render(htmrBrowser('