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

Tighter null checks #2107

Merged
merged 2 commits into from
Jun 17, 2024
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
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