Skip to content

Commit

Permalink
test: Add test cases for special attribute in DomValidator
Browse files Browse the repository at this point in the history
Add test cases to verify DomValidator accepts attribute:
- ':' prefix for binding attributes (e.g. :class)
- '@' prefix for event handlers (e.g. @scroll.window)
  • Loading branch information
gonft committed Dec 31, 2024
1 parent fe6779e commit a6009af
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions packages/jaspr/test/server/dom_validator_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:jaspr/src/server/markup_render_object.dart';
import 'package:test/test.dart';

void main() {
group('DomValidator', () {
late DomValidator validator;

setUp(() {
validator = DomValidator();
});

group('validateElementName', () {
test('accepts valid element names', () {
expect(() => validator.validateElementName('div'), returnsNormally);
expect(() => validator.validateElementName('custom-element'),
returnsNormally);
expect(() => validator.validateElementName('h1'), returnsNormally);
});

test('throws on invalid element names', () {
expect(
() => validator.validateElementName('1div'), throwsArgumentError);
expect(() => validator.validateElementName('-element'),
throwsArgumentError);
expect(
() => validator.validateElementName('div!'), throwsArgumentError);
});
});

group('validateAttributeName', () {
test('accepts valid attribute names', () {
expect(() => validator.validateAttributeName('id'), returnsNormally);
expect(() => validator.validateAttributeName('data-test'),
returnsNormally);
expect(() => validator.validateAttributeName('aria-label'),
returnsNormally);
expect(
() => validator.validateAttributeName(':class'), returnsNormally);
expect(() => validator.validateAttributeName('@scroll.window'),
returnsNormally);
});

test('throws on invalid attribute names', () {
expect(
() => validator.validateAttributeName('1id'), throwsArgumentError);
expect(() => validator.validateAttributeName('-test'),
throwsArgumentError);
expect(() => validator.validateAttributeName('data!'),
throwsArgumentError);
});
});

group('isSelfClosing', () {
test('returns true for self-closing elements', () {
expect(validator.isSelfClosing('img'), isTrue);
expect(validator.isSelfClosing('br'), isTrue);
expect(validator.isSelfClosing('input'), isTrue);
});

test('returns false for non-self-closing elements', () {
expect(validator.isSelfClosing('div'), isFalse);
expect(validator.isSelfClosing('span'), isFalse);
expect(validator.isSelfClosing('p'), isFalse);
});
});

group('hasStrictWhitespace', () {
test('returns true for elements with strict whitespace', () {
expect(validator.hasStrictWhitespace('p'), isTrue);
expect(validator.hasStrictWhitespace('h1'), isTrue);
expect(validator.hasStrictWhitespace('label'), isTrue);
});

test('returns false for elements without strict whitespace', () {
expect(validator.hasStrictWhitespace('div'), isFalse);
expect(validator.hasStrictWhitespace('span'), isFalse);
});
});

group('hasStrictFormatting', () {
test('returns true for elements with strict formatting', () {
expect(validator.hasStrictFormatting('pre'), isTrue);
expect(validator.hasStrictFormatting('span'), isTrue);
});

test('returns false for elements without strict formatting', () {
expect(validator.hasStrictFormatting('div'), isFalse);
expect(validator.hasStrictFormatting('p'), isFalse);
});
});
});
}

0 comments on commit a6009af

Please sign in to comment.