Skip to content

Commit 9cdb42c

Browse files
committed
refactor(test): move React-specific JSX tests into their own file
I want to eventually introduce Preact support which will interfere with React support. Move React-specific test cases into test-parse-jsx-react.cpp.
1 parent fae101a commit 9cdb42c

File tree

3 files changed

+113
-97
lines changed

3 files changed

+113
-97
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ quick_lint_js_add_executable(
139139
test-parse-expression-typescript.cpp
140140
test-parse-expression.cpp
141141
test-parse-function.cpp
142+
test-parse-jsx-react.cpp
142143
test-parse-jsx.cpp
143144
test-parse-loop.cpp
144145
test-parse-module.cpp

test/test-parse-jsx-react.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (C) 2020 Matthew "strager" Glazar
2+
// See end of file for extended copyright information.
3+
4+
#include <gmock/gmock.h>
5+
#include <gtest/gtest.h>
6+
#include <quick-lint-js/diag-matcher.h>
7+
#include <quick-lint-js/parse-support.h>
8+
#include <quick-lint-js/port/char8.h>
9+
#include <string_view>
10+
11+
namespace quick_lint_js {
12+
namespace {
13+
class Test_Parse_JSX_React : public Test_Parse_Expression {};
14+
15+
TEST_F(Test_Parse_JSX_React, correctly_capitalized_attribute) {
16+
test_parse_and_visit_module(u8R"(c = <td colSpan="2" />;)"_sv, no_diags,
17+
jsx_options);
18+
19+
test_parse_and_visit_module(u8R"(c = <div onClick={handler} />;)"_sv,
20+
no_diags, jsx_options);
21+
}
22+
23+
TEST_F(Test_Parse_JSX_React, event_attributes_should_be_camel_case) {
24+
test_parse_and_visit_module(
25+
u8"c = <div onclick={handler} />;"_sv, //
26+
u8" ^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag
27+
u8"{.expected_attribute_name=onClick}"_diag, //
28+
jsx_options);
29+
30+
// TODO(strager): Should we also report that the handler's value is missing?
31+
test_parse_and_visit_module(
32+
u8"c = <div onclick />;"_sv, //
33+
u8" ^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag
34+
u8"{.expected_attribute_name=onClick}"_diag, //
35+
jsx_options);
36+
37+
test_parse_and_visit_module(
38+
u8"c = <div onmouseenter={handler} />;"_sv, //
39+
u8" ^^^^^^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag
40+
u8"{.expected_attribute_name=onMouseEnter}"_diag, //
41+
jsx_options);
42+
43+
test_parse_and_visit_module(
44+
u8"c = <div oncustomevent={handler} />;"_sv, //
45+
u8" ^^^^^^^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag
46+
u8"{.expected_attribute_name=onCustomevent}"_diag, //
47+
jsx_options);
48+
}
49+
50+
TEST_F(Test_Parse_JSX_React, miscapitalized_attribute) {
51+
test_parse_and_visit_module(
52+
u8"c = <td colspan=\"2\" />;"_sv, //
53+
u8" ^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag
54+
u8"{.expected_attribute_name=colSpan}"_diag, //
55+
jsx_options);
56+
57+
test_parse_and_visit_module(
58+
u8"c = <div onMouseenter={handler} />;"_sv, //
59+
u8" ^^^^^^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag
60+
u8"{.expected_attribute_name=onMouseEnter}"_diag, //
61+
jsx_options);
62+
63+
test_parse_and_visit_module(
64+
u8"c = <div onmouseENTER={handler} />;"_sv, //
65+
u8" ^^^^^^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag
66+
u8"{.expected_attribute_name=onMouseEnter}"_diag, //
67+
jsx_options);
68+
}
69+
70+
TEST_F(Test_Parse_JSX_React, commonly_misspelled_attribute) {
71+
test_parse_and_visit_module(
72+
u8"c = <span class=\"item\"></span>;"_sv, //
73+
u8" ^^^^^ Diag_JSX_Attribute_Renamed_By_React.attribute_name"_diag
74+
u8"{.react_attribute_name=className}"_diag, //
75+
jsx_options);
76+
}
77+
78+
TEST_F(Test_Parse_JSX_React, attribute_checking_ignores_namespaced_attributes) {
79+
test_parse_and_visit_module(u8R"(c = <div ns:onmouseenter={handler} />;)"_sv,
80+
no_diags, jsx_options);
81+
test_parse_and_visit_module(
82+
u8R"(c = <div onmouseenter:onmouseenter={handler} />;)"_sv, no_diags,
83+
jsx_options);
84+
test_parse_and_visit_module(u8R"(c = <div class:class="my-css-class" />;)"_sv,
85+
no_diags, jsx_options);
86+
}
87+
88+
TEST_F(Test_Parse_JSX_React, attribute_checking_ignores_namespaced_elements) {
89+
test_parse_and_visit_module(u8R"(c = <svg:g onmouseenter={handler} />;)"_sv,
90+
no_diags, jsx_options);
91+
test_parse_and_visit_module(u8R"(c = <svg:g class="red" />;)"_sv, no_diags,
92+
jsx_options);
93+
}
94+
95+
TEST_F(Test_Parse_JSX_React, attribute_checking_ignores_user_components) {
96+
test_parse_and_visit_module(
97+
u8R"(c = <MyComponent onmouseenter={handler} />;)"_sv, no_diags,
98+
jsx_options);
99+
100+
test_parse_and_visit_module(u8R"(c = <MyComponent class="red" />;)"_sv,
101+
no_diags, jsx_options);
102+
103+
test_parse_and_visit_module(
104+
u8R"(c = <mymodule.mycomponent onmouseenter={handler} />;)"_sv, no_diags,
105+
jsx_options);
106+
107+
test_parse_and_visit_module(
108+
u8R"(c = <mymodule.mycomponent class="red" />;)"_sv, no_diags,
109+
jsx_options);
110+
}
111+
}
112+
}

test/test-parse-jsx.cpp

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -448,103 +448,6 @@ TEST_F(Test_Parse_JSX, adjacent_tags_without_outer_fragment) {
448448
}
449449
}
450450

451-
TEST_F(Test_Parse_JSX, correctly_capitalized_attribute) {
452-
test_parse_and_visit_module(u8R"(c = <td colSpan="2" />;)"_sv, no_diags,
453-
jsx_options);
454-
455-
test_parse_and_visit_module(u8R"(c = <div onClick={handler} />;)"_sv,
456-
no_diags, jsx_options);
457-
}
458-
459-
TEST_F(Test_Parse_JSX, event_attributes_should_be_camel_case) {
460-
test_parse_and_visit_module(
461-
u8"c = <div onclick={handler} />;"_sv, //
462-
u8" ^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag
463-
u8"{.expected_attribute_name=onClick}"_diag, //
464-
jsx_options);
465-
466-
// TODO(strager): Should we also report that the handler's value is missing?
467-
test_parse_and_visit_module(
468-
u8"c = <div onclick />;"_sv, //
469-
u8" ^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag
470-
u8"{.expected_attribute_name=onClick}"_diag, //
471-
jsx_options);
472-
473-
test_parse_and_visit_module(
474-
u8"c = <div onmouseenter={handler} />;"_sv, //
475-
u8" ^^^^^^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag
476-
u8"{.expected_attribute_name=onMouseEnter}"_diag, //
477-
jsx_options);
478-
479-
test_parse_and_visit_module(
480-
u8"c = <div oncustomevent={handler} />;"_sv, //
481-
u8" ^^^^^^^^^^^^^ Diag_JSX_Event_Attribute_Should_Be_Camel_Case.attribute_name"_diag
482-
u8"{.expected_attribute_name=onCustomevent}"_diag, //
483-
jsx_options);
484-
}
485-
486-
TEST_F(Test_Parse_JSX, miscapitalized_attribute) {
487-
test_parse_and_visit_module(
488-
u8"c = <td colspan=\"2\" />;"_sv, //
489-
u8" ^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag
490-
u8"{.expected_attribute_name=colSpan}"_diag, //
491-
jsx_options);
492-
493-
test_parse_and_visit_module(
494-
u8"c = <div onMouseenter={handler} />;"_sv, //
495-
u8" ^^^^^^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag
496-
u8"{.expected_attribute_name=onMouseEnter}"_diag, //
497-
jsx_options);
498-
499-
test_parse_and_visit_module(
500-
u8"c = <div onmouseENTER={handler} />;"_sv, //
501-
u8" ^^^^^^^^^^^^ Diag_JSX_Attribute_Has_Wrong_Capitalization.attribute_name"_diag
502-
u8"{.expected_attribute_name=onMouseEnter}"_diag, //
503-
jsx_options);
504-
}
505-
506-
TEST_F(Test_Parse_JSX, commonly_misspelled_attribute) {
507-
test_parse_and_visit_module(
508-
u8"c = <span class=\"item\"></span>;"_sv, //
509-
u8" ^^^^^ Diag_JSX_Attribute_Renamed_By_React.attribute_name"_diag
510-
u8"{.react_attribute_name=className}"_diag, //
511-
jsx_options);
512-
}
513-
514-
TEST_F(Test_Parse_JSX, attribute_checking_ignores_namespaced_attributes) {
515-
test_parse_and_visit_module(u8R"(c = <div ns:onmouseenter={handler} />;)"_sv,
516-
no_diags, jsx_options);
517-
test_parse_and_visit_module(
518-
u8R"(c = <div onmouseenter:onmouseenter={handler} />;)"_sv, no_diags,
519-
jsx_options);
520-
test_parse_and_visit_module(u8R"(c = <div class:class="my-css-class" />;)"_sv,
521-
no_diags, jsx_options);
522-
}
523-
524-
TEST_F(Test_Parse_JSX, attribute_checking_ignores_namespaced_elements) {
525-
test_parse_and_visit_module(u8R"(c = <svg:g onmouseenter={handler} />;)"_sv,
526-
no_diags, jsx_options);
527-
test_parse_and_visit_module(u8R"(c = <svg:g class="red" />;)"_sv, no_diags,
528-
jsx_options);
529-
}
530-
531-
TEST_F(Test_Parse_JSX, attribute_checking_ignores_user_components) {
532-
test_parse_and_visit_module(
533-
u8R"(c = <MyComponent onmouseenter={handler} />;)"_sv, no_diags,
534-
jsx_options);
535-
536-
test_parse_and_visit_module(u8R"(c = <MyComponent class="red" />;)"_sv,
537-
no_diags, jsx_options);
538-
539-
test_parse_and_visit_module(
540-
u8R"(c = <mymodule.mycomponent onmouseenter={handler} />;)"_sv, no_diags,
541-
jsx_options);
542-
543-
test_parse_and_visit_module(
544-
u8R"(c = <mymodule.mycomponent class="red" />;)"_sv, no_diags,
545-
jsx_options);
546-
}
547-
548451
TEST_F(Test_Parse_JSX, prop_needs_an_expression) {
549452
test_parse_and_visit_module(
550453
u8"c = <MyComponent custom={}></MyComponent>;"_sv, //

0 commit comments

Comments
 (0)