How to use the @leafygreen-ui/lib.typeIs.element function in @leafygreen-ui/lib

To help you get started, we’ve selected a few @leafygreen-ui/lib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mongodb / leafygreen-ui / packages / syntax / src / Syntax.spec.tsx View on Github external
describe('packages/Syntax', () => {
  const { container } = render(
    
      {codeSnippet}
    ,
  );

  const code = container.firstChild as HTMLElement;

  if (!code || !typeIs.element(code)) {
    throw new Error('Code element not found');
  }

  test(`renders "${className}" in the code element's classList`, () => {
    expect(code.classList.contains(className)).toBe(true);
  });

  test("doesn't highlight code when language is 'none'", () => {
    // Text nodes in HTMLCollections are ignored since they are not considered "elements",
    // so we check that children is empty here since we expect a text node to be rendered.
    //
    // https://www.w3.org/TR/domcore/#concept-element
    expect(code.children.length).toBe(0);
  });

  test("highlights code when language is 'javascript'", () => {
github mongodb / leafygreen-ui / packages / code / src / LineNumbers.spec.tsx View on Github external
describe('packages/Syntax', () => {
  const { container } = render(
    ,
  );

  const rootElement = container.firstChild as HTMLElement;

  if (!rootElement || !typeIs.element(rootElement)) {
    throw new Error('Code element not found');
  }

  test(`renders "${className}" in the root element's classList`, () => {
    expect(rootElement.classList.contains(className)).toBe(true);
  });

  test(`renders ${lineCount} line numbers`, () => {
    expect(rootElement.children.length).toBe(lineCount);
  });
});
github mongodb / leafygreen-ui / packages / radio-box-group / src / RadioBoxGroup.spec.tsx View on Github external
describe('packages/RadioBoxGroup', () => {
  const { container } = render(
    
      Input 1
      <h1>Will Remain As Text</h1>
      Input 2
    ,
  );

  const radioBoxGroupContainer = container.firstChild;

  if (!typeIs.element(radioBoxGroupContainer)) {
    throw new Error('Could not find radio box group container element');
  }

  const text = radioBoxGroupContainer.children[1];

  test('renders children of Radio Box Group, that are not themselves Radio Boxes, as is, without converting them to RadioBoxes', () =&gt; {
    expect(text.tagName.toLowerCase()).toBe('h1');
  });

  describe('when controlled', () =&gt; {
    const controlledOnChange = jest.fn();

    render(
      
        Option 1
        Option 2
github mongodb / leafygreen-ui / packages / radio-box-group / src / RadioBoxGroup.spec.tsx View on Github external
render(
      
        Option 1
      ,
      { container },
    );

    const radioBoxGroup = container.firstChild;

    if (!typeIs.element(radioBoxGroup)) {
      throw new Error('Could not find radio box group element');
    }

    const radioBoxLabel = radioBoxGroup.firstChild;

    if (!typeIs.element(radioBoxLabel)) {
      throw new Error('Could not find label element');
    }

    const radioBoxInput = radioBoxLabel.firstChild;

    if (!typeIs.input(radioBoxInput)) {
      throw new Error('Could not find radio box input element');
    }

    fireEvent.click(radioBoxLabel);

    test('onChange fires once when the label is clicked', () =&gt; {
      expect(uncontrolledOnChange.mock.calls.length).toBe(1);
    });

    test('radio box becomes checked when clicked', () =&gt; {
github mongodb / leafygreen-ui / packages / toggle / src / Toggle.spec.tsx View on Github external
describe('when uncontrolled', () =&gt; {
    const uncontrolledOnClick = jest.fn();
    const uncontrolledOnChange = jest.fn();
    const uncontrolledContainer = render(
      ,
    ).container.firstChild;

    if (!typeIs.element(uncontrolledContainer)) {
      throw new Error('Could not find uncontrolled container element');
    }

    const uncontrolledCheckbox = uncontrolledContainer.children[0];

    if (!typeIs.input(uncontrolledCheckbox)) {
      throw new Error('Could not find uncontrolled checkbox input element');
    }

    fireEvent.click(uncontrolledContainer);

    test('onClick fires once when the label is clicked', () =&gt; {
      expect(uncontrolledOnClick.mock.calls.length).toBe(1);
    });

    test('onChange fires once when the label is clicked', () =&gt; {
github mongodb / leafygreen-ui / packages / radio-box-group / src / RadioBoxGroup.spec.tsx View on Github external
test('renders as disabled, when the disabled prop is set', () =&gt; {
    const { container } = render(
      
        Input 2
      ,
    );

    const radioBoxContainer = container.firstChild;

    if (!typeIs.element(radioBoxContainer)) {
      throw new Error('Could not find radio box container element');
    }

    const radioBox = radioBoxContainer.firstChild;

    if (!typeIs.input(radioBox)) {
      throw new Error('Could not find radio box input element');
    }

    expect(radioBox.getAttribute('aria-disabled')).toBe('true');
  });
});
github mongodb / leafygreen-ui / packages / radio-box-group / src / RadioBoxGroup.spec.tsx View on Github external
Option 1
        Option 2
      ,
      { container },
    );

    const radioBoxGroup = container.firstChild;

    if (!typeIs.element(radioBoxGroup)) {
      throw new Error('Could not find radio box group element');
    }

    const firstRadioBoxLabel = radioBoxGroup.firstChild;

    if (!typeIs.element(firstRadioBoxLabel)) {
      throw new Error('Could not find label element');
    }

    const firstRadioBoxInput = firstRadioBoxLabel.firstChild;
    const secondRadioBoxInput = radioBoxGroup.children[1].firstChild;

    if (
      !typeIs.input(firstRadioBoxInput) ||
      !typeIs.input(secondRadioBoxInput)
    ) {
      throw new Error('Could not find input element');
    }

    fireEvent.click(secondRadioBoxInput);

    test('initial value set by radio box group when prop provided', () =&gt; {
github mongodb / leafygreen-ui / packages / radio-group / src / Radio.spec.tsx View on Github external
describe('packages/Radio', () =&gt; {
  const className = 'radio-test-class';
  const { container } = render(
    
      Radio 1
    ,
  );

  const radio = container.firstChild;

  if (!typeIs.element(radio)) {
    throw new Error('Could not find controlled container component');
  }

  const input = radio.firstChild;

  if (!typeIs.input(input)) {
    throw new Error('Could not find input element');
  }

  test(`renders "${className}" in the labels's class list`, () =&gt; {
    expect(radio.classList.contains(className)).toBe(true);
  });

  test(`renders disabled radio when disabled prop is set`, () =&gt; {
    expect(input.disabled).toBe(true);
    expect(input.getAttribute('aria-disabled')).toBe('true');
github mongodb / leafygreen-ui / packages / radio-group / src / RadioGroup.spec.tsx View on Github external
describe('packages/RadioGroup', () =&gt; {
  const className = 'test-radiogroup-class';
  const { container } = render(
    
      Radio Button 1
    ,
  );

  const controlledContainer = container.firstChild;

  if (!typeIs.element(controlledContainer)) {
    throw new Error('Could not find controlled container component');
  }

  test(`renders "${className}" in the container labels classList`, () =&gt; {
    expect(controlledContainer.classList.contains(className)).toBe(true);
  });
});
github mongodb / leafygreen-ui / packages / radio-box-group / src / RadioBoxGroup.spec.tsx View on Github external
describe('when uncontrolled', () =&gt; {
    const uncontrolledOnChange = jest.fn();

    render(
      
        Option 1
      ,
      { container },
    );

    const radioBoxGroup = container.firstChild;

    if (!typeIs.element(radioBoxGroup)) {
      throw new Error('Could not find radio box group element');
    }

    const radioBoxLabel = radioBoxGroup.firstChild;

    if (!typeIs.element(radioBoxLabel)) {
      throw new Error('Could not find label element');
    }

    const radioBoxInput = radioBoxLabel.firstChild;

    if (!typeIs.input(radioBoxInput)) {
      throw new Error('Could not find radio box input element');
    }

    fireEvent.click(radioBoxLabel);