Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ember-testing-library

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ember-testing-library

Simple and complete Ember DOM testing utilities that encourage good testing practices.

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

ember-testing-library

Simple and complete Ember DOM testing utilities that encourage good testing practices.

Please note that this version of the library is not backed by DOM Testing Library like the versions for React, Vue, etc. are. I made this addon because there is no official Ember version yet.

Installation

This addon can be installed via Ember CLI as such:

ember install ember-testing-library

Usage

The recommended way to use this addon is through the setupScreen function. This function automatically injects the DOM Testing Library screen into every test in a given module.

import { setupRenderingTest } from 'ember-qunit';
import { module, test } from 'qunit';

import { TestContext as BaseTestContext } from 'ember-test-helpers';

import { setupScreen, ScreenTestContext } from 'ember-testing-library';

interface TestContext extends BaseTestContext, ScreenTestContext {}

module('Integration | Component |  Example', function (hooks) {
  setupRenderingTest(hooks);
  setupScreen(hooks);

  test('it works as expected', async function (this: TestContext, assert) {
    await render(hbs`<MyComponent />`);

    assert.dom(this.screen.getByLabelText('My text field') as HTMLElement).exists();
  });
});

Once this function has been called, this.screen can be accessed in every test in your module. The functions available in this.screen are one to one with the queries available in the standard edition of DOM Testing Library. For a thorough list of these, you can see the official docs.

Usage without injection

If you don't want to inject this.screen into every test in your module, you can use the getScreen to access the DOM Testing Library screen as a one off whenever you desire.

import { setupRenderingTest } from 'ember-qunit';
import { module, test } from 'qunit';

import { getScreen } from 'ember-testing-library';

module('Integration | Component |  Example', function (hooks) {
  setupRenderingTest(hooks);

  test('it works as expected', async function (assert) {
    await render(hbs`<MyComponent />`);

    const screen = getScreen();

    assert.dom(screen.getByLabelText('My text field') as HTMLElement).exists();
  });
});

Notes

This addon does not expose an equivalent to fireEvent like the other framework versions of DOM Testing Library do. Instead, it is recommended you use @ember/test-helpers in conjunction with this addon for event triggering purposes.

import { render, click } from '@ember/test-helpers';
import { setupRenderingTest } from 'ember-qunit';
import { module, test } from 'qunit';

import { TestContext as BaseTestContext } from 'ember-test-helpers';

import hbs from 'htmlbars-inline-precompile';
import sinon, { SinonSpy } from 'sinon';

import { setupScreen, ScreenTestContext } from 'ember-testing-library';

interface TestContext extends BaseTestContext, ScreenTestContext {
  spy: SinonSpy;
}

module('Integration | Component |  Example', function (hooks) {
  setupRenderingTest(hooks);
  setupScreen(hooks);

  hooks.beforeEach(function (this: TestContext) {
    this.spy = sinon.spy();
  });

  test('it works as expected', async function (this: TestContext, assert) {
    await render(hbs`<MyComponent @onClick={{this.spy}} />`);

    await click(this.screen.getByText('My button') as HTMLElement);

    assert.ok(this.spy.calledOnce);
  });
});

Keywords

FAQs

Package last updated on 20 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc