Skip to content

Commit

Permalink
Merge pull request #87 from Turbo87/disabled
Browse files Browse the repository at this point in the history
 Merge `isDisabled` and `isNotDisabled` helpers
  • Loading branch information
Turbo87 authored Apr 19, 2018
2 parents 8d614b1 + 074ec4a commit c8f3608
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 76 deletions.
126 changes: 96 additions & 30 deletions lib/__tests__/is-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,130 @@ describe('assert.dom(...).isDisabled()', () => {
assert = new TestAssertions();
});

describe('input tags', () => {
test('succeeds when input is disabled', () => {
document.body.innerHTML = '<input disabled>';
test('with custom message', () => {
document.body.innerHTML = '<input type="text">';

assert.dom('input').isDisabled('custom message');

expect(assert.results).toEqual([{
actual: 'Element input is not disabled',
expected: 'Element input is disabled',
message: 'custom message',
result: false,
}]);
});

describe('with HTMLElement', () => {
let element;

beforeEach(() => {
document.body.innerHTML = '<input type="text" disabled>';
element = document.querySelector('input');
});

test('succeeds if element is disabled', () => {
assert.dom(element).isDisabled();

expect(assert.results).toEqual([{
actual: 'Element input[type="text"][disabled] is disabled',
expected: 'Element input[type="text"][disabled] is disabled',
message: 'Element input[type="text"][disabled] is disabled',
result: true,
}]);
});

test('fails if element is not disabled', () => {
element.disabled = false;
assert.dom(element).isDisabled();

expect(assert.results).toEqual([{
actual: 'Element input[type="text"] is not disabled',
expected: 'Element input[type="text"] is disabled',
message: 'Element input[type="text"] is disabled',
result: false,
}]);
});

test('fails for missing element', () => {
assert.dom(null).isDisabled();

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

test('succeeds if element is disabled with text', () => {
document.body.innerHTML = '<input type="text" disabled="false">';
element = document.querySelector('input');

assert.dom('input').isDisabled();
assert.dom(document.querySelector('input')).isDisabled();

expect(assert.results).toEqual([{
actual: 'Element input is disabled',
expected: 'Element input is disabled',
message: 'Element input is disabled',
result: true,
}, {
actual: 'Element input[disabled] is disabled',
expected: 'Element input[disabled] is disabled',
message: 'Element input[disabled] is disabled',
result: true,
}]);
});
});

test('fails when input is not disabled', () => {
document.body.innerHTML = '<input>';
describe('with selector', () => {
beforeEach(() => {
document.body.innerHTML = '<input type="text" disabled>';
});

test('succeeds if element is disabled', () => {
assert.dom('input').isDisabled();
assert.dom(document.querySelector('input')).isDisabled();

expect(assert.results).toEqual([{
actual: 'Element input is not disabled',
actual: 'Element input is disabled',
expected: 'Element input is disabled',
message: 'Element input is disabled',
result: false,
}, {
result: true,
}]);
});

test('fails if element is not disabled', () => {
document.querySelector('input').disabled = false;
assert.dom('input').isDisabled();

expect(assert.results).toEqual([{
actual: 'Element input is not disabled',
expected: 'Element input is disabled',
message: 'Element input is disabled',
result: false,
}]);
});
});

describe('non-disablable elements', () => {
test('fails when element does not support disabled', () => {
document.body.innerHTML = '<div id="dis" disabled><div>';

assert.dom('div#dis').isDisabled();
assert.dom(document.querySelector('div#dis')).isDisabled();
test('fails for missing element', () => {
assert.dom('input[type="password"]').isDisabled();

expect(assert.results).toEqual([{
actual: 'Element div#dis does not support disabled',
expected: 'Element div#dis is disabled',
message: 'Element div#dis is disabled',
result: false,
}, {
actual: 'Element div#dis[disabled] does not support disabled',
expected: 'Element div#dis[disabled] is disabled',
message: 'Element div#dis[disabled] is disabled',
message: 'Element input[type="password"] exists',
result: false,
}]);
});

test('succeeds if element is disabled with text', () => {
document.body.innerHTML = '<input type="text" disabled="false">';
assert.dom('input').isDisabled();

expect(assert.results).toEqual([{
actual: 'Element input is disabled',
expected: 'Element input is disabled',
message: 'Element input is disabled',
result: true,
}]);
});
});

test('throws for unexpected parameter types', () => {
expect(() => assert.dom(5).isDisabled()).toThrow('Unexpected Parameter: 5');
expect(() => assert.dom(true).isDisabled()).toThrow('Unexpected Parameter: true');
expect(() => assert.dom(undefined).isDisabled()).toThrow('Unexpected Parameter: undefined');
expect(() => assert.dom({}).isDisabled()).toThrow('Unexpected Parameter: [object Object]');
expect(() => assert.dom(document).isDisabled()).toThrow('Unexpected Parameter: [object HTMLDocument]');
expect(() => assert.dom(document.createElement('div')).isDisabled()).toThrow('Unexpected Element Type: [object HTMLDivElement]');
});
});
23 changes: 3 additions & 20 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import isRequired from './assertions/is-required';
import isNotRequired from './assertions/is-not-required';
import isVisible from './assertions/is-visible';
import isNotVisible from './assertions/is-not-visible';
import isNotDisabled from './assertions/is-not-disabled';
import isDisabled from './assertions/is-disabled';

import elementToString from './helpers/element-to-string';
import collapseWhitespace from './helpers/collapse-whitespace';
Expand Down Expand Up @@ -318,24 +318,7 @@ export default class DOMAssertions {
* @see {@link #isNotDisabled}
*/
isDisabled(message) {
let element = this.findTargetElement();

let expected = `Element ${this.targetDescription} is disabled`;
let actual = `Element ${this.targetDescription} is disabled`;
let result = element.disabled;

if (result === false) {
actual = `Element ${this.targetDescription} is not disabled`;
} else if(result === undefined) {
actual = `Element ${this.targetDescription} does not support disabled`;
result = false;
}

if (!message) {
message = expected;
}

this.pushResult({ result, actual, expected, message });
isDisabled.call(this, message);
}

/**
Expand All @@ -351,7 +334,7 @@ export default class DOMAssertions {
* @see {@link #isDisabled}
*/
isNotDisabled(message) {
isNotDisabled.call(this, message);
isDisabled.call(this, message, { inverted: true });
}

/**
Expand Down
34 changes: 34 additions & 0 deletions lib/assertions/is-disabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export default function isDisabled(message, options = {}) {
let { inverted } = options;

let element = this.findTargetElement();
if (!element) return;

if (!(
element instanceof HTMLInputElement ||
element instanceof HTMLTextAreaElement ||
element instanceof HTMLSelectElement ||
element instanceof HTMLButtonElement ||
element instanceof HTMLOptGroupElement ||
element instanceof HTMLOptionElement ||
element instanceof HTMLFieldSetElement
)) {
throw new TypeError(`Unexpected Element Type: ${element.toString()}`);
}

let result = element.disabled === !inverted;

let actual = element.disabled === false
? `Element ${this.targetDescription} is not disabled`
: `Element ${this.targetDescription} is disabled`;

let expected = inverted
? `Element ${this.targetDescription} is not disabled`
: `Element ${this.targetDescription} is disabled`;

if (!message) {
message = expected;
}

this.pushResult({ result, actual, expected, message });
}
26 changes: 0 additions & 26 deletions lib/assertions/is-not-disabled.js

This file was deleted.

0 comments on commit c8f3608

Please sign in to comment.