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

isChecked: Add support for aria-checked="true/false" #535

Merged
merged 1 commit into from
Nov 12, 2019
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
60 changes: 60 additions & 0 deletions lib/__tests__/is-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,66 @@ describe('assert.dom(...).isChecked()', () => {
});
});

describe('aria-checked', () => {
test('succeeds if element is checked', () => {
document.body.innerHTML = '<button role="checkbox" aria-checked="true">';

assert.dom('button').isChecked();

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

test('fails if element is not checked', () => {
document.body.innerHTML = '<button role="checkbox" aria-checked="false">';

assert.dom('button').isChecked();

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

test('fails if element does not have `aria-checked` attribute', () => {
document.body.innerHTML = '<button role="checkbox">';

assert.dom('button').isChecked();

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

test('fails for missing element', () => {
document.body.innerHTML = '';

assert.dom('button').isChecked();

expect(assert.results).toEqual([
{
message: 'Element button should exist',
result: false,
},
]);
});
});

test('throws for unexpected parameter types', () => {
expect(() => assert.dom(5).isChecked()).toThrow('Unexpected Parameter: 5');
expect(() => assert.dom(true).isChecked()).toThrow('Unexpected Parameter: true');
Expand Down
60 changes: 60 additions & 0 deletions lib/__tests__/is-not-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,66 @@ describe('assert.dom(...).isNotChecked()', () => {
});
});

describe('aria-checked', () => {
test('succeeds if element is not checked', () => {
document.body.innerHTML = '<button role="checkbox" aria-checked="false">';

assert.dom('button').isNotChecked();

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

test('fails if element is checked', () => {
document.body.innerHTML = '<button role="checkbox" aria-checked="true">';

assert.dom('button').isNotChecked();

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

test('succeeds if element does not have `aria-checked` attribute', () => {
document.body.innerHTML = '<button role="checkbox">';

assert.dom('button').isNotChecked();

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

test('fails for missing element', () => {
document.body.innerHTML = '';

assert.dom('button').isNotChecked();

expect(assert.results).toEqual([
{
message: 'Element button should exist',
result: false,
},
]);
});
});

test('throws for unexpected parameter types', () => {
expect(() => assert.dom(5).isNotChecked()).toThrow('Unexpected Parameter: 5');
expect(() => assert.dom(true).isNotChecked()).toThrow('Unexpected Parameter: true');
Expand Down
4 changes: 4 additions & 0 deletions lib/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ export default class DOMAssertions {
* Assert that the {@link HTMLElement} or an {@link HTMLElement} matching the
* `selector` is currently checked.
*
* Note: This also supports `aria-checked="true/false"`.
*
* @param {string?} message
*
* @example
Expand All @@ -95,6 +97,8 @@ export default class DOMAssertions {
* Assert that the {@link HTMLElement} or an {@link HTMLElement} matching the
* `selector` is currently unchecked.
*
* Note: This also supports `aria-checked="true/false"`.
*
* @param {string?} message
*
* @example
Expand Down
16 changes: 14 additions & 2 deletions lib/assertions/is-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ export default function checked(message) {
let element = this.findTargetElement();
if (!element) return;

let result = element.checked === true;
let actual = element.checked === true ? 'checked' : 'not checked';
let isChecked = element.checked === true;
let isNotChecked = element.checked === false;

let result = isChecked;

let hasCheckedProp = isChecked || isNotChecked;
if (!hasCheckedProp) {
let ariaChecked = element.getAttribute('aria-checked');
if (ariaChecked !== null) {
result = ariaChecked === 'true';
}
}

let actual = result ? 'checked' : 'not checked';
let expected = 'checked';

if (!message) {
Expand Down
16 changes: 14 additions & 2 deletions lib/assertions/is-not-checked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ export default function notChecked(message) {
let element = this.findTargetElement();
if (!element) return;

let result = element.checked === false;
let actual = element.checked === true ? 'checked' : 'not checked';
let isChecked = element.checked === true;
let isNotChecked = element.checked === false;

let result = !isChecked;

let hasCheckedProp = isChecked || isNotChecked;
if (!hasCheckedProp) {
let ariaChecked = element.getAttribute('aria-checked');
if (ariaChecked !== null) {
result = ariaChecked !== 'true';
}
}

let actual = result ? 'not checked' : 'checked';
let expected = 'not checked';

if (!message) {
Expand Down