@alfheim/generator-nef-tests

Yeoman generator for basic NEF component testing suite

Usage no npm install needed!

<script type="module">
  import alfheimGeneratorNefTests from 'https://cdn.skypack.dev/@alfheim/generator-nef-tests';
</script>

README

@alfheim/generator-nef-tests

Travis npm (scoped with tag) GitHub license PRs welcome

This package is part of the battery used to create new components for the Nasdaq Experience Framework. This package creates an <ComponentName>.styles.ts[x] file, which contains styles for the created component.

Note: It is recommended that you install and use generator-nef-component rather than installing this package directly, unless you need to modify the individual file generators.

By default, a testing file for the component named Test created using this generator looks as follows:

import { mount, render, shallow } from "enzyme";
import React from "react";
import faker from "faker";

import StyledTest from "./Test.styles";

import Test from ".";

describe("Test", () => {
  let props;
  let mountedTest;
  let renderedTest;
  let shallowTest;

  const mountTestComponent = () => {
    if (!mountedTest) {
      mountedTest = mount(<Test {...props} />);
    }
    return mountedTest;
  };

  const renderTestComponent = () => {
    if (!renderedTest) {
      renderedTest = render(<Test {...props} />);
    }
    return renderedTest;
  };

  const shallowTestComponent = () => {
    if (!shallowTest) {
      shallowTest = shallow(<Test {...props} />);
    }
    return shallowTest;
  };

  beforeEach(() => {
    props = {
      children: faker.lorem.paragraph(),
      className: faker.random.word(),
      id: faker.random.word(),
      name: faker.random.word(),
      style: { color: faker.internet.color() }
    };
    mountedTest = undefined;
    renderedTest = undefined;
    shallowTest = undefined;
  });

  // Shallow / unit tests begin here
  it(`should always render a 'StyledTest' component`, () => {
    expect(shallowTestComponent().find(StyledTest).length).toBe(1);
  });

  it(`should always pass all its props to the 'StyledTest'`, () => {
    expect(
      shallowTestComponent()
        .find(StyledTest)
        .props()
    ).toMatchObject(props);
  });

  // Render / mount / integration tests begin here
});

Options

Additionally, the following options can be provided when calling the generator:

  • --no-mount-tests: disable mount tests when generating the test file
  • --no-render-tests: disable render tests when generating the test file
  • --no-shallow-tests: disable shallow tests when generating the test file

Getting started

To install, simply run:

yarn add @alfheim/generator-nef-tests --dev

or

npm install @alfheim/generator-nef-tests --dev 

Contributing

We'd love to have your helping hand on alfheim! Go over to our issues section and see if there's anything we're looking for help with OR open up a PR if you have an idea for a way to improve the library.

License

Alfheim is open source software licensed as MIT.