Skip to content

Commit

Permalink
Merge pull request #2107 from bendemboski/null-checks
Browse files Browse the repository at this point in the history
Tighter null checks
  • Loading branch information
BobrImperator authored Jun 17, 2024
2 parents 4b86b35 + 4731a3c commit a420f27
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 21 deletions.
15 changes: 15 additions & 0 deletions packages/qunit-dom/lib/__tests__/does-not-exist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ describe('assert.dom(...).doesNotExist()', () => {
},
]);
});

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

assert.dom(null).doesNotExist();

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

test('custom message', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/does-not-have-attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ describe('assert.dom(...).doesNotHaveAttribute()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).doesNotHaveAttribute('disabled');

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).doesNotHaveAttribute('disabled')).toThrow('Unexpected Parameter: 5');
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/does-not-have-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ describe('assert.dom(...).doesNotHaveClass()', () => {
]);
});

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

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).doesNotHaveClass('foo')).toThrow('Unexpected Parameter: 5');
Expand Down
12 changes: 12 additions & 0 deletions packages/qunit-dom/lib/__tests__/does-not-have-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ describe('assert.dom(...).doesNotHaveStyle()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).doesNotHaveStyle({
opacity: 0,
});
expect(assert.results).toEqual([
{
message: 'Element <unknown> should exist',
result: false,
},
]);
});

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).doesNotHaveStyle({ opacity: 1 })).toThrow('Unexpected Parameter: 5');
Expand Down
15 changes: 15 additions & 0 deletions packages/qunit-dom/lib/__tests__/does-not-match-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ describe('assert.dom(...).doesNotMatchSelector()', () => {
]);
});

test('null passed', () => {
assert.dom(null).doesNotMatchSelector('div>p:last-child');

expect(assert.results).toEqual([
{
actual: '0 elements, selected by null, did not also match the selector div>p:last-child.',
expected:
'0 elements, selected by null, did not also match the selector div>p:last-child.',
message:
'0 elements, selected by null, did not also match the selector div>p:last-child.',
result: true,
},
]);
});

test('multiple elements, some matching compareSelector', () => {
assert.dom('p').doesNotMatchSelector('div>p');

Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-any-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ describe('assert.dom(...).hasAnyText()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasAnyText();

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasAnyText()).toThrow('Unexpected Parameter: 5');
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-any-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ describe('assert.dom(...).hasAnyValue()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasAnyValue();

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasAnyValue()).toThrow('Unexpected Parameter: 5');
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ describe('assert.dom(...).hasAttribute()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasAttribute('foo');

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasAttribute('foo')).toThrow('Unexpected Parameter: 5');
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ describe('assert.dom(...).hasClass()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasClass('foo');

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasClass('foo')).toThrow('Unexpected Parameter: 5');
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-no-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ describe('assert.dom(...).hasNoText()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasNoText();

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasNoText()).toThrow('Unexpected Parameter: 5');
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-no-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ describe('assert.dom(...).hasNoValue()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasNoValue();

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasNoValue()).toThrow('Unexpected Parameter: 5');
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ describe('assert.dom(...).hasProperty()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasProperty('foo', 'bar');

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasProperty('foo', 'bar')).toThrow('Unexpected Parameter: 5');
Expand Down
12 changes: 12 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ describe('assert.dom(...).hasStyle()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasStyle({
opacity: 0,
});
expect(assert.results).toEqual([
{
message: 'Element <unknown> should exist',
result: false,
},
]);
});

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasStyle({ opacity: 1 })).toThrow('Unexpected Parameter: 5');
Expand Down
11 changes: 11 additions & 0 deletions packages/qunit-dom/lib/__tests__/has-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ describe('assert.dom(...).hasValue()', () => {
]);
});

test('fails for null', () => {
assert.dom(null).hasValue('foo');

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

test('throws for unexpected parameter types', () => {
//@ts-ignore -- These assertions are for JavaScript users who don't have type checking
expect(() => assert.dom(5).hasValue('foo')).toThrow('Unexpected Parameter: 5');
Expand Down
17 changes: 17 additions & 0 deletions packages/qunit-dom/lib/__tests__/is-not-visible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ describe('assert.dom(...).isNotVisible()', () => {
});
});

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

assert.dom(null).isNotVisible();

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

describe('custom messages', () => {
test('shows custom messages', () => {
document.body.innerHTML = '<h1 class="baz">foo</h1>bar';
Expand Down
13 changes: 13 additions & 0 deletions packages/qunit-dom/lib/__tests__/matches-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ describe('assert.dom(...).matchesSelector()', () => {
]);
});

test('null passed', () => {
assert.dom(null).matchesSelector('div>p:last-child');

expect(assert.results).toEqual([
{
actual: '0 elements, selected by null, also match the selector div>p:last-child.',
expected: '0 elements, selected by null, also match the selector div>p:last-child.',
message: '0 elements, selected by null, also match the selector div>p:last-child.',
result: true,
},
]);
});

test('multiple elements not all matching compareSelector', () => {
assert.dom('p').matchesSelector('p + p');

Expand Down
Loading

0 comments on commit a420f27

Please sign in to comment.