learningequality / kolibri-design-system

Kolibri Design System
https://design-system.learningequality.org
27 stars 65 forks source link

[Visual Testing]: Abstract the test API for developers #684

Closed AlexVelezLl closed 1 month ago

AlexVelezLl commented 1 month ago

Product

KDS.

Description

The setup of the visual testing framework in KDS has been carried out successfully. But the current way of doing snapshots involves a lot of boilerplate code, which makes the visual tests complex to write, complex to read, and the maintainability of the code is low. Example of the current behaviour:

describe('KButton', () => {
  if (!canTakeScreenshot()) {
    //... <unit tests>
  } else {
          beforeAll(async () => {
        await page.goto('http://localhost:4000/testing-playground', { waitUntil: 'networkidle2' });
      });

      async function renderComponent(component, props) {
        await page.evaluate(
          ({ component, props }) => {
            window.postMessage(
              {
                type: 'RENDER_COMPONENT',
                component: component,
                props: props,
              },
              '*'
            );
          },
          { component, props }
        );
        await page.waitForSelector('#testing-playground');

        const isComponentRendered = await page.evaluate(() => {
          const testing_playground = document.querySelector('#testing-playground');
          return testing_playground && testing_playground.children.length > 0;
        });

        if (!isComponentRendered) {
          // eslint-disable-next-line no-console
          console.error('Component did not render in the testing playground');
        }
      }

      it('renders correctly with default props', async () => {
        await renderComponent('KButton', { text: 'Test Button' });
        await percySnapshot(page, 'KButton - Default');
      });
    });
  }

So there are a lot of things that we can abstract:

The Value Add

This will allow for much easier, faster and more maintainable development of visual tests. And it will contribute a lot to developers easily adopting this pattern.

KshitijThareja commented 1 month ago

Thanks @AlexVelezLl for creating the issue. Will have the PR up soon :)

AlexVelezLl commented 1 month ago

Closed by #685.