Skip to content

Commit

Permalink
Merge pull request #246 from dwilhelmi/support-Element-type-for-exists
Browse files Browse the repository at this point in the history
Make `findTargets` support a `this.target` of type `Element`
  • Loading branch information
Turbo87 authored Feb 2, 2020
2 parents 36441fe + b117d1e commit 2bdb294
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 24 deletions.
4 changes: 0 additions & 4 deletions lib/__tests__/does-not-exist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ describe('assert.dom(...).doesNotExist()', () => {
});

test('throws for unexpected parameter types', () => {
expect(() => assert.dom(document.body).doesNotExist()).toThrow(
'Unexpected Parameter: [object HTMLBodyElement]'
);

//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).doesNotExist()).toThrow('Unexpected Parameter: 5');
//@ts-ignore
Expand Down
39 changes: 34 additions & 5 deletions lib/__tests__/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('assert.dom(...).exists()', () => {
assert = new TestAssertions();
});

describe('selector only', () => {
describe('with selector', () => {
test('succeeds if element exists', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';

Expand Down Expand Up @@ -41,6 +41,39 @@ describe('assert.dom(...).exists()', () => {
});
});

describe('with Element', () => {
test('succeeds if element exists', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';

const headingElement = document.querySelector('h1');
assert.dom(headingElement).exists();

expect(assert.results).toEqual([
{
actual: 'Element h1.baz exists',
expected: 'Element h1.baz exists',
message: 'Element h1.baz exists',
result: true,
},
]);
});

test('fails if element is missing', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';

assert.dom(null).isVisible();

expect(assert.results).toEqual([
{
actual: 'Element <not found> is not visible',
expected: 'Element <not found> is visible',
message: 'Element <not found> is visible',
result: false,
},
]);
});
});

describe('custom messages', () => {
test('without options', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';
Expand Down Expand Up @@ -136,10 +169,6 @@ describe('assert.dom(...).exists()', () => {
});

test('throws for unexpected parameter types', () => {
expect(() => assert.dom(document.body).exists()).toThrow(
'Unexpected Parameter: [object HTMLBodyElement]'
);

//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).exists()).toThrow('Unexpected Parameter: 5');
//@ts-ignore
Expand Down
34 changes: 33 additions & 1 deletion lib/__tests__/is-visible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,22 @@ describe('assert.dom(...).isVisible()', () => {
assert = new TestAssertions();
});

describe('selector only', () => {
describe('with selector', () => {
test('fails if element is missing', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';

assert.dom('h2').isVisible();

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

test('fails if element is missing', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';

Expand Down Expand Up @@ -51,6 +66,23 @@ describe('assert.dom(...).isVisible()', () => {
});
});

describe('with Element', () => {
test('fails for missing element', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';

assert.dom(null).isVisible();

expect(assert.results).toEqual([
{
actual: 'Element <not found> is not visible',
expected: 'Element <not found> is visible',
message: 'Element <not found> is visible',
result: false,
},
]);
});
});

describe('with count option', () => {
test('fails if element is missing', () => {
document.body.innerHTML = '<div></div>'.repeat(3);
Expand Down
8 changes: 5 additions & 3 deletions lib/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,16 +1196,18 @@ export default class DOMAssertions {
}

/**
* Finds a collection of HTMLElement instances from target using querySelectorAll
* Finds a collection of Element instances from target using querySelectorAll
* @private
* @returns (HTMLElement[]) an array of HTMLElement instances
* @returns (Element[]) an array of Element instances
* @throws TypeError will be thrown if target is an unrecognized type
*/
private findElements(): HTMLElement[] {
private findElements(): Element[] {
if (this.target === null) {
return [];
} else if (typeof this.target === 'string') {
return toArray(this.rootElement.querySelectorAll(this.target));
} else if (this.target instanceof Element) {
return [this.target];
} else {
throw new TypeError(`Unexpected Parameter: ${this.target}`);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/assertions/exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export default function exists(options?: ExistsOptions | string, message?: strin
expectedCount = options.count;
}

let elements = this.findElements(this.target);
let elements = this.findElements();

if (expectedCount === null) {
let result = elements.length > 0;
let expected = format(this.target);
let actual = result ? expected : format(this.target, 0);
let expected = format(this.targetDescription);
let actual = result ? expected : format(this.targetDescription, 0);

if (!message) {
message = expected;
Expand All @@ -23,8 +23,8 @@ export default function exists(options?: ExistsOptions | string, message?: strin
this.pushResult({ result, actual, expected, message });
} else if (typeof expectedCount === 'number') {
let result = elements.length === expectedCount;
let actual = format(this.target, elements.length);
let expected = format(this.target, expectedCount);
let actual = format(this.targetDescription, elements.length);
let expected = format(this.targetDescription, expectedCount);

if (!message) {
message = expected;
Expand Down
10 changes: 5 additions & 5 deletions lib/assertions/is-visible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export default function isVisible(options?: string | ExistsOptions, message?: st
expectedCount = options.count;
}

let elements = this.findElements(this.target).filter(visible);
let elements = this.findElements().filter(visible);

if (expectedCount === null) {
let result = elements.length > 0;
let expected = format(this.target);
let actual = result ? expected : format(this.target, 0);
let expected = format(this.targetDescription);
let actual = result ? expected : format(this.targetDescription, 0);

if (!message) {
message = expected;
Expand All @@ -24,8 +24,8 @@ export default function isVisible(options?: string | ExistsOptions, message?: st
this.pushResult({ result, actual, expected, message });
} else if (typeof expectedCount === 'number') {
let result = elements.length === expectedCount;
let actual = format(this.target, elements.length);
let expected = format(this.target, expectedCount);
let actual = format(this.targetDescription, elements.length);
let expected = format(this.targetDescription, expectedCount);

if (!message) {
message = expected;
Expand Down
4 changes: 3 additions & 1 deletion lib/helpers/element-to-string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// imported from https://github.com/nathanboktae/chai-dom

export default function elementToString(el: Element | NodeList | string): string {
export default function elementToString(el: Element | NodeList | string | null): string {
if (!el) return '<not found>';

let desc: string;
if (el instanceof NodeList) {
if (el.length === 0) {
Expand Down

0 comments on commit 2bdb294

Please sign in to comment.