devrafalko / jasmine-dom-custom-matchers

jasmine 2.0 DOM custom matchers comparing relations, attributes, styles and states of html element and text nodes
https://devrafalko.github.io/jasmine-dom-custom-matchers/index.html
MIT License
18 stars 4 forks source link

In This Documentation

  1. Description
  2. Browser Support
  3. Methods
  4. Tips
  5. Implementation
  6. How to use
  7. See also
  8. License

Description

How does jasmine-dom-custom-matchers work?

A custom matchers syntax has changed with the release of jasmine 2.0. This library provides 19 custom matchers adapted to the new way of matchers constructing. It allows you to compare DOM Objects relations and states.

What can I use jasmine-dom-custom-matchers for?
DOM Custom Matchers list
Where can I check how jasmine-dom-custom-matchers work?

Examine the Samples described below to find out how you can use DOM custom matchers.

Browser Support

Chrome Firefox IE Edge Safari Opera iOS Safari Opera Mini
4+ 2+ 9-11 12+ 3.1+ 9+ 3.2+ all

Methods

expect(actual).toBeHTMLElement(name)
expect(actual).toBeHTMLText(content)
expect(actual).toBeDocumentNode()
expect(actual).toContainHTMLElement(descendant)
expect(actual).toContainText(content)

to check whether the [HTML Text] Object equals text content, use toBeHTMLText(content) with expected text content

expect(actual).toBeChildOf(parent)
expect(actual).toBeNthChild(index)
expect(actual).toBeParentOf(child)
expect(actual).toHaveSameParent(node)
expect(actual).toHaveChildren(numOfChildren,operator)
expect(actual).toBeNextSiblingOf(expected)
expect(actual).toBePreviousSiblingOf(expected)
expect(actual).toBeEmpty()

the difference between .toBeEmpty() matcher and .toHaveChildren() matcher is that .toBeEmpty() matcher checks if the actual [HTML Element] Object contains both [HTML Element] and [HTML Text] Objects when .toHaveChildren() matcher checks if the actual [HTML Element] Object contains only [HTML Element] Objects.

expect(actual).toHaveAnyAttribute()
expect(actual).toHaveAttribute(name,value)

with appropriate /regular expression/ passed as value parameter it is possible to check whether class attribute contains expected class, but it is easier to achieve with .toHaveClass() custom matcher

expect(actual).toHaveClass(class)

to check whether [HTML Element] Object has got a class attribute defined regardless its values, use .toHaveAttribute('class') matcher

expect(actual).toHaveComputedStyle(prop,value)

in order to deal with the differences between browsers of returning computed style values, use regular expression .toHaveComputedStyle('propertyName',/(ms-value|moz-value|webkit-value)/)

expect(actual).toHaveComputedColor(prop,value)

If the browser return rgb(255, 255, 0) 2px 2px 2px as computed 'box-shadow' style, the expected value rgb(255, 255, 0), rgba(255, 255, 0, 1), #ff0, #FFFF00, hsl(60, 100%, 50%) and hsla(60, 100%, 50%, 1) will return truthy result.

If the browser return rgba(255, 255, 0, .4) as computed 'color' style, the expected value rgba(255, 255, 0, .4) and hsla(60, 100%, 50%, .4) will return truthy result.

If the browser return rgba(255, 255, 0, .4) as computed 'color' style, the expected value rgb(255, 255, 0), #ff0, #FFFF00 and hsl(60, 100%, 50%) will return faulty result because the alpha parameter does not match.

Because of the differences between browsers, the alpha parameter of hsla() and rgba() formats is rounded to two digits after decimal point. The same result will be aimed with rgba(100, 100, 100, 0.23), rgba(100, 100, 100, 0.230445), rgba(100, 100, 100, 0.2349999999)

expect(actual).toHaveEvent(event)

Tips

Each matcher demands accurate type of parameter value. The matcher check the actual and expected parameters' types before implementing the comparison and return false when value type is incorrect regardless the comparison result.

Compared [HTML Element] and [HTML Text] Objects do not have to be appended to the DOM tree in order to use DOM custom matcher. The comparison can be implemented for dynamically created elements in beforeEach(), beforeAll() or it() scope. Exception is .toBeDocumentNode() matcher which check if actual Object is appended to the DOM and .toHaveComputedStyle() .toHaveComputedColor() matchers which require the [HTML Element] Object to be appended to the DOM in order to return the expected computed style

In order to check whether two HTML Objects are the same objects use the native matcher expect(objectA).toBe(objectB)

In order to check whether two HTML Objects are equal objects use the native matcher expect(objectA).toEqual(objectB)

Implementation

with NodeJS

npm install jasmine-dom-custom-matchers --save-dev

var matchers = require('jasmine-dom-custom-matchers'); //get the module

describe("The new DIV element", function() {
    beforeAll(function() {
        jasmine.addMatchers(matchers);  //set custom matchers for jasmine
        this.newDiv = document.createElement('DIV');
    });
    it("should be empty.", function() {
        expect(this.newDiv).toBeEmpty();    //do the magic with new DOM matchers
    });
}

You can use karma-html module to test your .html files in the karma browser runner [git] [npm]

with Karma

Use karma-jasmine-dom package [link] that adapts the jasmine-dom-custom-matchers package for karma.

with Browser

1. Load dom-matchers.js in html file
<head>
  <link rel="shortcut icon" type="image/png" href="https://github.com/devrafalko/jasmine-dom-custom-matchers/blob/master/jasmine/lib/jasmine-core/jasmine_favicon.png">
  <link rel="stylesheet" type="text/css" href="https://github.com/devrafalko/jasmine-dom-custom-matchers/blob/master/jasmine/lib/jasmine-core/jasmine.css">
  <script type="text/javascript" src="https://github.com/devrafalko/jasmine-dom-custom-matchers/raw/master/jasmine/lib/jasmine-core/jasmine.js"></script>
  <script type="text/javascript" src="https://github.com/devrafalko/jasmine-dom-custom-matchers/raw/master/jasmine/lib/jasmine-core/jasmine-html.js"></script>
  <script type="text/javascript" src="https://github.com/devrafalko/jasmine-dom-custom-matchers/raw/master/jasmine/lib/jasmine-core/boot.js"></script>

  <script type="text/javascript" src="https://github.com/devrafalko/jasmine-dom-custom-matchers/raw/master/dom-matchers.js"></script>
</head>

Any outer libraries needed. It is a fully JavaScript library.

2. Load custom matchers with jasmine.addMatchers in your tests files
describe("The new DIV element", function() {
  beforeAll(function() {
    //DOMCustomMatchers is the global window object got from dom-matchers.js
    jasmine.addMatchers(DOMCustomMatchers);    
    this.newDiv = document.createElement('DIV');
  });
  it("should be empty.", function() {
    expect(this.newDiv).toBeEmpty();    //do the magic with new DOM matchers
  });
}

How to use

See also

License

Released under the MIT license.

Copyright (c) 2017 Paweł Rafałko dev.rafalko@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.