npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

qunit-assert-classes

v1.0.2

Published

A set of assertions for checking thats an element has ( or does not have ) any number of classes. The check is order independent and gives a diff of the expected classes.

Downloads

46

Readme

Build Status NPM version

QUnit Classes assertion plugin

A set of assertions for checking thats an element has ( or does not have ) any number of classes. The check is order independent and gives a diff of the expected classes. Accepts either a single Dom node or a jQuery object containing exactly one element.

NOTE: While this accepts jQuery objects it has no dependency on jQuery

Usage

assert.hasClasses( element, classes [, message] );
assert.lacksClasses( element, classes [, message] );

assert.hasClassesStrict( element, classes [, message] );

assert.hasClassRegex( element, RexExp [, message] );
assert.lacksClassRegex( element, RexExp [, message] );

assert.hasClassStart( element, start [, message] );
assert.lacksClassStart( element, start [, message] );

assert.hasClassPartial( element, partial [, message] );
assert.lacksClassPartial( element, partial [, message] );

assert.lacksAllClasses( element [, message] );
assert.hasSomeClass( element [, message] );

Where:

  • element: a jQuery object or DOM Element ( must be a single element, collections not allowed )
  • classes: a space seperated string of classes
  • message: a custom message to replace default message of "Element must (not) have classes"
  • RegExp: a valid regular expression to be used to match against individual class names
  • start: a string that should match the begining of any class
  • partial: a string of a partial class name that should be matched against any of the classes

Examples

Example 1: hasClasses with DOM Element

<div class="class1 class2"></div>
QUnit.test('Example hasClasses unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.hasClasses( element, "class1 class2" ); // Passes
  assert.hasClasses( element, "class3", "custom message" ); // Fails
});

Example 2: lacksClasses with jQuery object

<div class="class1 class2"></div>
QUnit.test('Example lacksClasses unit test', function( assert ) {
  var element = $( "#test" );

  assert.lacksClasses( element, "missing-class" ); // Passes
  assert.lacksClasses( element, "class1 missing-class-2", "custom message" ); // Fails
});

Example 3: hasClassesStrict

<div class="class1 class2"></div>
QUnit.test('Example hasClassesStrict unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.hasClassesStrict( element, "class1 class2" ); // Passes
  assert.hasClassesStrict( element, "class1", "custom message" ); // Fails
});

Example 4: hasClassRegex

<div class="class1 class2"></div>
QUnit.test('Example hasClassesRegex unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.hasClassRegex( element, /^(clas)([a-z]){1}([0-9])$/ ); // Passes

  // Fails this only matches against individual classes
  assert.hasClassRegex( element, /^(([a-z]){5}([0-9]|[0-9]\s)){2}$/, "custom message" );
});

Example 5: lacksClassRegex

<div class="class1 class2"></div>
QUnit.test('Example hasClassesRegex unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.lacksClassRegex( element, /boom/ ); // Passes
  assert.lacksClassRegex( element, /^(class)/, "custom message" ); // Fails
});

Example 6: hasClassStart

<div class="class1 class2"></div>
QUnit.test('Example hasClassStart unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.hasClassStart( element, "cla" ); // Passes
  assert.hasClassStart( element, "lass", "custom message" ); // Fails
});

Example 7: lacksClassStart

<div class="class1 class2"></div>
QUnit.test('Example lacksClassStart unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.lacksClassStart( element, "foo" ); // Passes
  assert.lacksClassStart( element, "cla", "custom message" ); // Fails
});

Example 8: hasClassPartial

<div class="class1 class2"></div>
QUnit.test('Example hasClassPartial unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.hasClassPartial( element, "lass" ); // Passes
  assert.hasClassPartial( element, "foo", "custom message" ); // Fails
});

Example 9: lacksClassPartial

<div class="class1 class2"></div>
QUnit.test('Example lacksClassStart unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.lacksClassPartial( element, "foo" ); // Passes
  assert.lacksClassPartial( element, "lass", "custom message" ); // Fails
});

Example 10: lacksAllClasses - checks to make sure an element contains no classes

<div class=""></div>
QUnit.test('Example lacksAllClasses unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.lacksAllClasses( element ); // Passes
  assert.lacksAllClasses( element, "custom message" ); // Fails
});

Example 10: hasSomeClass - checks to make sure an element has some class does not matter what

<div class="class1 class2"></div>
QUnit.test('Example hasSomeClass unit test', function( assert ) {
  var element = document.getElementById( "test" );

  assert.hasSomeClass( element ); // Passes
  assert.hasSomeClass( element, "custom message" ); // Fails
});