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

use fixtures / snapshots for specs instead of JS DOM #14

Open
thescientist13 opened this issue May 1, 2022 · 0 comments
Open

use fixtures / snapshots for specs instead of JS DOM #14

thescientist13 opened this issue May 1, 2022 · 0 comments
Labels
chore good first issue Good for newcomers

Comments

@thescientist13
Copy link
Member

thescientist13 commented May 1, 2022

Type of Change

  • Other (please clarify below)

Summary

In our specs currently, we convert the HTML from wcc into DOM and query it via JSDOM

So for this component

const template = document.createElement('template');

template.innerHTML = `
  <footer class="footer">
    <h4>
      <a href="https://www.greenwoodjs.io/">My Blog &copy;${new Date().getFullYear()} &#9672 Built with GreenwoodJS</a>
    </h4>
  </footer>
`;

class Footer extends HTMLElement {
  connectedCallback() {
    if (!this.shadowRoot) {
      this.attachShadow({ mode: 'open' });
      this.shadowRoot.appendChild(template.content.cloneNode(true));
    }
  }
}

export default Footer;

customElements.define('wcc-footer', Footer);

We would test it like so

import chai from 'chai';
import { JSDOM } from 'jsdom';
import { renderToString } from '../../../src/wcc.js';

const expect = chai.expect;

describe('Run WCC For ', function() {
  const LABEL = 'Single Custom Element w/ Declarative Shadow DOM';
  let dom;

  before(async function() {
    const { html } = await renderToString(new URL('./src/footer.js', import.meta.url));
    
    dom = new JSDOM(html);
  });

  describe(LABEL, function() {
      
    it('should have one top level <template> with an open shadowroot', function() {
      expect(dom.window.document.querySelectorAll('template[shadowroot="open"]').length).to.equal(1);
      expect(dom.window.document.querySelectorAll('template').length).to.equal(1);
    });

    describe('<footer> component and content', function() {
      let footer;

      before(async function() {
        footer = new JSDOM(dom.window.document.querySelectorAll('template[shadowroot="open"]')[0].innerHTML);
      });

      it('should have one <footer> tag within the <template> shadowroot', function() {
        expect(footer.window.document.querySelectorAll('footer').length).to.equal(1);
      });
  
      it('should have the expected content for the <footer> tag', function() {
        expect(footer.window.document.querySelectorAll('h4 a').textContent).to.contain(/My Blog/);
      });
    });

  });
});

But I'm curious to see if would be more pragmatic to just use fixtures instead? In other words, like in snapshot testing, capture the expected outcome as an HTML file, and just match the response to the file on disk.

Details

So for the above component test, instead of testing via DOM, we just test against a fixture file in the test directory

<wcc-footer>
  <template shadowroot="open">
    <footer class="footer">
      <h4>
        <a href="https://www.greenwoodjs.io/">My Blog &copy;${new Date().getFullYear()} &#9672 Built with GreenwoodJS</a>
      </h4>
    </footer>
  </template>
</wcc-footer>

I think it's easier to reason about and easier to edit, and should make writing tests a LOT easier. (IMO)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant