Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doesNotHaveClass/hasNoClass() assertion #26

Merged
merged 2 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ assert.dom('input[type="password"]').isNotFocused();

### hasClass

- **See: [#doesNotHaveClass](#doesnothaveclass)**

Assert that the [HTMLElement][] has the `expected` CSS class using
[`classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList).

Expand All @@ -111,6 +113,26 @@ Assert that the [HTMLElement][] has the `expected` CSS class using
assert.dom('input[type="password"]').hasClass('secret-password-input');
```

### doesNotHaveClass

- **See: [#hasClass](#hasclass)**

Assert that the [HTMLElement][] does not have the `expected` CSS class using
[`classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList).

**Aliases:** `hasNoClass`

**Parameters**

- `expected` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `message` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)?**

**Examples**

```javascript
assert.dom('input[type="password"]').doesNotHaveClass('username-input');
```

### hasText

- **See: [#hasTextContaining](#hastextcontaining)**
Expand Down
64 changes: 64 additions & 0 deletions lib/__tests__/does-not-have-class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-env jest */

import TestAssertions from "../helpers/test-assertions";

describe('assert.dom(...).doesNotHaveClass()', () => {
let assert;

beforeEach(() => {
assert = new TestAssertions();

document.body.innerHTML = '<input type="password" class="secret-password-input foo">';
});

test('succeeds for correct content', () => {
assert.dom('input[type="password"]').doesNotHaveClass('username-input');
assert.dom(document.querySelector('input[type="password"]')).doesNotHaveClass('username-input');

expect(assert.results).toEqual([{
actual: 'secret-password-input foo',
expected: 'not: username-input',
message: 'Element input[type="password"] does not have CSS class "username-input"',
result: true,
}, {
actual: 'secret-password-input foo',
expected: 'not: username-input',
message: 'Element input.secret-password-input.foo[type="password"] does not have CSS class "username-input"',
result: true,
}]);
});

test('fails for wrong content', () => {
assert.dom('input[type="password"]').doesNotHaveClass('secret-password-input');
assert.dom(document.querySelector('input[type="password"]')).doesNotHaveClass('secret-password-input');

expect(assert.results).toEqual([{
actual: 'secret-password-input foo',
expected: 'not: secret-password-input',
message: 'Element input[type="password"] does not have CSS class "secret-password-input"',
result: false,
}, {
actual: 'secret-password-input foo',
expected: 'not: secret-password-input',
message: 'Element input.secret-password-input.foo[type="password"] does not have CSS class "secret-password-input"',
result: false,
}]);
});

test('fails for missing element', () => {
assert.dom('#missing').doesNotHaveClass('foo');

expect(assert.results).toEqual([{
message: 'Element #missing exists',
result: false,
}]);
});

test('throws for unexpected parameter types', () => {
expect(() => assert.dom(5).doesNotHaveClass('foo')).toThrow('Unexpected Parameter: 5');
expect(() => assert.dom(true).doesNotHaveClass('foo')).toThrow('Unexpected Parameter: true');
expect(() => assert.dom(undefined).doesNotHaveClass('foo')).toThrow('Unexpected Parameter: undefined');
expect(() => assert.dom({}).doesNotHaveClass('foo')).toThrow('Unexpected Parameter: [object Object]');
expect(() => assert.dom(document).doesNotHaveClass('foo')).toThrow('Unexpected Parameter: [object HTMLDocument]');
});
});
35 changes: 34 additions & 1 deletion lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export default class DOMAssertions {
notFocused.call(this, message);
}


/**
* Assert that the [HTMLElement][] has the `expected` CSS class using
* [`classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList).
Expand All @@ -82,6 +81,8 @@ export default class DOMAssertions {
*
* @example
* assert.dom('input[type="password"]').hasClass('secret-password-input');
*
* @see {@link #doesNotHaveClass}
*/
hasClass(expected, message) {
let element = this.findTargetElement();
Expand All @@ -97,6 +98,38 @@ export default class DOMAssertions {
this.pushResult({ result, actual, expected, message });
}

/**
* Assert that the [HTMLElement][] does not have the `expected` CSS class using
* [`classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList).
*
* **Aliases:** `hasNoClass`
*
* @param {string} expected
* @param {string?} message
*
* @example
* assert.dom('input[type="password"]').doesNotHaveClass('username-input');
*
* @see {@link #hasClass}
*/
doesNotHaveClass(expected, message) {
let element = this.findTargetElement();
if (!element) return;

let result = !element.classList.contains(expected);
let actual = element.classList.toString();

if (!message) {
message = `Element ${this.targetDescription} does not have CSS class "${expected}"`;
}

this.pushResult({ result, actual, expected: `not: ${expected}`, message });
}

hasNoClass(expected, message) {
this.doesNotHaveClass(expected, message);
}

/**
* Assert that the text of the [HTMLElement][] or an [HTMLElement][]
* matching the `selector` matches the `expected` text, using the
Expand Down