|
| 1 | +"""Tests for check_frontend_code_quality.py - frontend code quality checks.""" |
| 2 | + |
| 3 | +from scripts.check_frontend_code_quality import check_source |
| 4 | + |
| 5 | + |
| 6 | +class TestDialogDescriptionNesting: |
| 7 | + """Detect <p> elements nested inside Radix Dialog/AlertDialog Description.""" |
| 8 | + |
| 9 | + def test_flags_p_inside_alert_dialog_description(self) -> None: |
| 10 | + source = ( |
| 11 | + '<AlertDialog.Description className="mt-3 text-sm">\n' |
| 12 | + " <p>Are you sure?</p>\n" |
| 13 | + "</AlertDialog.Description>\n" |
| 14 | + ) |
| 15 | + violations = check_source(source, "src/components/Example.tsx") |
| 16 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 17 | + assert len(nesting_violations) == 1 |
| 18 | + assert nesting_violations[0][0] == 2 # line number of <p> |
| 19 | + |
| 20 | + def test_flags_p_inside_dialog_description(self) -> None: |
| 21 | + source = ( |
| 22 | + '<Dialog.Description className="mt-3 text-sm">\n' |
| 23 | + " <p>Some content</p>\n" |
| 24 | + "</Dialog.Description>\n" |
| 25 | + ) |
| 26 | + violations = check_source(source, "src/components/Example.tsx") |
| 27 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 28 | + assert len(nesting_violations) == 1 |
| 29 | + |
| 30 | + def test_flags_multiple_p_tags(self) -> None: |
| 31 | + source = ( |
| 32 | + '<AlertDialog.Description className="mt-3">\n' |
| 33 | + " <p>First paragraph</p>\n" |
| 34 | + " <p>Second paragraph</p>\n" |
| 35 | + "</AlertDialog.Description>\n" |
| 36 | + ) |
| 37 | + violations = check_source(source, "src/components/Example.tsx") |
| 38 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 39 | + assert len(nesting_violations) == 2 |
| 40 | + |
| 41 | + def test_allows_as_child_with_div_wrapper(self) -> None: |
| 42 | + source = ( |
| 43 | + "<AlertDialog.Description asChild>\n" |
| 44 | + ' <div className="mt-3 text-sm">\n' |
| 45 | + " <p>This is fine because asChild renders as div</p>\n" |
| 46 | + " </div>\n" |
| 47 | + "</AlertDialog.Description>\n" |
| 48 | + ) |
| 49 | + violations = check_source(source, "src/components/Example.tsx") |
| 50 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 51 | + assert len(nesting_violations) == 0 |
| 52 | + |
| 53 | + def test_allows_text_only_content(self) -> None: |
| 54 | + source = ( |
| 55 | + '<AlertDialog.Description className="mt-3 text-sm">\n' |
| 56 | + " This document will be removed from the upload list.\n" |
| 57 | + "</AlertDialog.Description>\n" |
| 58 | + ) |
| 59 | + violations = check_source(source, "src/components/Example.tsx") |
| 60 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 61 | + assert len(nesting_violations) == 0 |
| 62 | + |
| 63 | + def test_allows_span_block_instead_of_p(self) -> None: |
| 64 | + source = ( |
| 65 | + '<AlertDialog.Description className="mt-3 text-sm">\n' |
| 66 | + ' <span className="block mb-2">\n' |
| 67 | + " Are you sure you want to delete this?\n" |
| 68 | + " </span>\n" |
| 69 | + ' <span className="block">\n' |
| 70 | + " This action cannot be undone.\n" |
| 71 | + " </span>\n" |
| 72 | + "</AlertDialog.Description>\n" |
| 73 | + ) |
| 74 | + violations = check_source(source, "src/components/Example.tsx") |
| 75 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 76 | + assert len(nesting_violations) == 0 |
| 77 | + |
| 78 | + def test_handles_as_child_on_separate_line(self) -> None: |
| 79 | + source = ( |
| 80 | + "<AlertDialog.Description\n" |
| 81 | + " asChild\n" |
| 82 | + ' className="mt-3"\n' |
| 83 | + ">\n" |
| 84 | + ' <div className="space-y-2">\n' |
| 85 | + " <p>Safe because asChild is present</p>\n" |
| 86 | + " </div>\n" |
| 87 | + "</AlertDialog.Description>\n" |
| 88 | + ) |
| 89 | + violations = check_source(source, "src/components/Example.tsx") |
| 90 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 91 | + assert len(nesting_violations) == 0 |
| 92 | + |
| 93 | + def test_flags_p_with_multi_line_opening_tag_without_as_child(self) -> None: |
| 94 | + source = ( |
| 95 | + "<AlertDialog.Description\n" |
| 96 | + ' className="mt-3 text-sm text-brand-mid-grey"\n' |
| 97 | + ">\n" |
| 98 | + " <p>This is a violation</p>\n" |
| 99 | + "</AlertDialog.Description>\n" |
| 100 | + ) |
| 101 | + violations = check_source(source, "src/components/Example.tsx") |
| 102 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 103 | + assert len(nesting_violations) == 1 |
| 104 | + assert nesting_violations[0][0] == 4 |
| 105 | + |
| 106 | + def test_handles_self_closing_description(self) -> None: |
| 107 | + source = '<AlertDialog.Description className="sr-only" />\n' |
| 108 | + violations = check_source(source, "src/components/Example.tsx") |
| 109 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 110 | + assert len(nesting_violations) == 0 |
| 111 | + |
| 112 | + def test_handles_multiple_descriptions_in_one_file(self) -> None: |
| 113 | + source = ( |
| 114 | + "// First dialog - safe (text only)\n" |
| 115 | + '<AlertDialog.Description className="mt-2">\n' |
| 116 | + " Are you sure?\n" |
| 117 | + "</AlertDialog.Description>\n" |
| 118 | + "\n" |
| 119 | + "// Second dialog - violation\n" |
| 120 | + '<AlertDialog.Description className="mt-2">\n' |
| 121 | + " <p>Bad nesting</p>\n" |
| 122 | + "</AlertDialog.Description>\n" |
| 123 | + ) |
| 124 | + violations = check_source(source, "src/components/Example.tsx") |
| 125 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 126 | + assert len(nesting_violations) == 1 |
| 127 | + assert nesting_violations[0][0] == 8 |
| 128 | + |
| 129 | + def test_p_with_attributes_is_also_flagged(self) -> None: |
| 130 | + source = ( |
| 131 | + '<AlertDialog.Description className="mt-3">\n' |
| 132 | + ' <p className="text-sm">Content</p>\n' |
| 133 | + "</AlertDialog.Description>\n" |
| 134 | + ) |
| 135 | + violations = check_source(source, "src/components/Example.tsx") |
| 136 | + nesting_violations = [v for v in violations if v[1] == "dialog-p-nesting"] |
| 137 | + assert len(nesting_violations) == 1 |
0 commit comments