qunit-dom

repository·master·Indexed 41 days ago

https://github.com/mainmatter/qunit-dom

High-level DOM assertions for the QUnit testing framework, version 3.6.0. Provides the `assert.dom()` API to write expressive tests for element existence, visibility, attributes, ARIA roles, CSS classes, styles, text content, and input values.

Tokens
6.4K
Snippets
28
Records
32
Agent score
48%

What's inside qunit-dom

  1. Lint and build the test-app

    master

    Use the following commands to maintain code quality and build the application for different environments:

    Linting:

    • npm run lint: Run linting checks.
    • npm run lint:fix: Run linting and automatically fix issues.

    Building:

    • ember build: Build for development.
    • ember build --environment production: Build for production.
    npm run lint
    npm run lint:fix
    
    ember build
    ember build --environment production
  2. Set up the test-app development environment

    master

    To collaborate on the test-app Ember application, ensure you have Git, Node.js (with npm), Ember CLI, and Google Chrome installed. Follow these steps to clone and install dependencies:

    1. Clone the repository.
    2. Navigate to the test-app directory.
    3. Run npm install to install dependencies.
    git clone <repository-url>
    cd test-app
    npm install
  3. Setup qunit-dom in Ember projects (ember-qunit v6.x+)

    master

    For Ember projects using ember-qunit version 6.x or higher, you must import and run the setup function in your tests/test-helper.js file. This attaches the DOM assertion APIs to the QUnit assert object.

    // tests/test-helper.js
    import * as QUnit from 'qunit';
    import { setup } from 'qunit-dom';
    
    //... 
    
    setup(QUnit.assert);
    
    //... 
  4. Use DOM assertions with assert.dom()

    master

    Once installed and set up, you can perform high-level DOM assertions using the assert.dom(selector) syntax. This allows you to check for existence, classes, text content, focus, values, and attributes.

    test('the title is welcoming', function(assert) {
      assert.dom('#title').hasText('Welcome to QUnit');
    });
    
    // Other examples:
    assert.dom('h1').exists();
    assert.dom('h1').hasClass('title');
    assert.dom('h1').hasText('Welcome to Ember, John Doe!');
    assert.dom('input').isFocused();
    assert.dom('input').hasValue(/.+ Doe/);
    assert.dom('input').hasAttribute('type', 'text');
  5. Configure TypeScript for qunit-dom

    master

    To ensure TypeScript and your editor recognize the extended types on the QUnit assert object, add import 'qunit-dom'; to your tests/test-helper.ts file. If you are currently using .js, rename it to .ts first.

    // tests/test-helper.ts
    import 'qunit-dom';
  6. Load qunit-dom via <script> tag

    master

    If using a browser-based setup, load qunit-dom.js immediately after qunit.js.

    <script src="https://unpkg.com/qunitjs/qunit/qunit.js"></script>
    <script src="https://unpkg.com/qunit-dom/dist/qunit-dom.js"></script>
  7. Assert element identity and selectors

    master

    Use these methods to compare elements or their relationship to selectors:

    • Identity: is(expected) asserts the element is the exact same instance as expected (reference equality ===). isNot(unexpected) asserts they are different instances.
    • Tag Name: hasTagName(tagName) asserts the element's tagName matches. doesNotHaveTagName(tagName) asserts it does not.
    • Selector Match: matchesSelector(compareSelector) asserts that the target element is also selected by the compareSelector. doesNotMatchSelector(compareSelector) asserts the target element is NOT selected by the compareSelector.
    assert.dom('#title').is(element);
    assert.dom('#title').hasTagName('h1');
    assert.dom('p.red').matchesSelector('div.wrapper p:last-child');
    assert.dom('input').doesNotMatchSelector('input[disabled]');
  8. Assert text content

    master

    Verify the text inside an element using textContent. Whitespace is collapsed/stripped when using string comparisons. To test specific whitespace integrity (line breaks, tabs, etc.), pass a RegExp instead of a string.

    • Exact/Pattern Match: hasText(expected) (alias: matchesText) asserts the text matches a string or RegExp. hasNoText() asserts the text is empty.
    • Partial Match: includesText(text) (aliases: containsText, hasTextContaining) asserts the text contains a substring.
    • Presence: hasAnyText() asserts the text is not empty.
    • Absence: doesNotIncludeText(text) (aliases: doesNotContainText, doesNotHaveTextContaining) asserts the text does not contain the substring.
    // Matches collapsed whitespace
    assert.dom('#title').hasText('Welcome to QUnit');
    
    // Matches specific whitespace using RegExp
    assert.dom('.foo').hasText(/[12]\d{3}/);
    
    // Partial match
    assert.dom('#title').includesText('Welcome');
    
    // Check if empty
    assert.dom('div').hasNoText();