Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Should we re-write template engine tests to compare against native behavior? #212

Open
theengineear opened this issue Nov 18, 2024 · 0 comments

Comments

@theengineear
Copy link
Collaborator

Pros

  • Makes it more obvious that we aim to emulate native behavior, always.

Cons

  • Obfuscates actual behavior (rather than compare with an expectation, you compare with another unknown value).
  • Need to create additional code to test our code — i.e., need to build the emulators. Tests will become longer.

Example of testing basic rendering

it('renders basic string', () => {
  const native = container => {
    const div = document.createElement('div');
    Object.assign(div, { id: 'target', textContent: 'No interpolation.' });
    container.replaceChildren();
    container.append(div);
  };
  const engine = container => {
    const result = html`<div id="target">No interpolation.</div>`;
    render(container, result);
  };
  const nativeContainer = document.createElement('div');
  const engineContainer = document.createElement('div');
  document.body.append(nativeContainer, engineContainer);
  native(nativeContainer);
  engine(engineContainer);
  assert(
    nativeContainer.childNodes.length ===
    engineContainer.childNodes.length
  );
  assert(
    nativeContainer.querySelector('#target').textContent ===
    engineContainer.querySelector('#target').textContent
  );
  nativeContainer.remove();
  engineContainer.remove();
});

… currently, this test reads as follows …

it('renders basic string', () => {
  const getTemplate = () => {
    return html`<div id="target">No interpolation.</div>`;
  };
  const container = document.createElement('div');
  document.body.append(container);
  render(container, getTemplate());
  assert(container.childNodes.length === 1);
  assert(container.querySelector('#target').textContent === 'No interpolation.');
  container.remove();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant