Skip to content

Commit 9be598f

Browse files
author
Thomas Scharke
committed
test: Add unit tests for HTML and TSX
1 parent d9861f9 commit 9be598f

4 files changed

Lines changed: 182 additions & 0 deletions

File tree

build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2+
import org.jetbrains.intellij.platform.gradle.TestFrameworkType
23

34
fun properties(key: String) = providers.gradleProperty(key)
45

@@ -24,7 +25,12 @@ dependencies {
2425
val type = properties("platformType").get()
2526
val version = properties("platformVersion").get()
2627
create(type, version)
28+
29+
testFramework(TestFrameworkType.Platform)
30+
bundledPlugin("JavaScript")
2731
}
32+
33+
testImplementation(kotlin("test"))
2834
}
2935

3036
tasks {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package dev.zbinski.htmlattributefolder
2+
3+
private const val HTML_SNIPPET =
4+
"""<div class="a b" className="c d" style="background-color: red;" data-foo="foo-data" />"""
5+
6+
class AttributeFolderHTMLTest : BaseAttributeFolderTest() {
7+
fun testFoldingHtmlAttributesCollapsed() {
8+
assertContains(document.text, """class="a b"""")
9+
assertContains(document.text, """className="c d"""")
10+
assertContains(document.text, """style="background-color: red;"""")
11+
assertContains(document.text, """data-foo="foo-data"""")
12+
13+
configureAttributeFolder(collapseByDefault = true)
14+
15+
val visualText = applyPluginFoldingAndRender()
16+
assertContains(visualText, """class="__PLACEHOLDER__"""")
17+
assertContains(visualText, """className="__PLACEHOLDER__"""")
18+
assertContains(visualText, """style="background-color: red;"""")
19+
assertContains(visualText, """data-foo="foo-data"""")
20+
}
21+
22+
fun testFoldingHtmlAttributesUncollapsed() {
23+
assertContains(document.text, """class="a b"""")
24+
assertContains(document.text, """className="c d"""")
25+
assertContains(document.text, """style="background-color: red;"""")
26+
assertContains(document.text, """data-foo="foo-data"""")
27+
28+
configureAttributeFolder(collapseByDefault = false)
29+
30+
val visualText = applyPluginFoldingAndRender()
31+
assertContains(visualText, """class="a b"""")
32+
assertContains(visualText, """className="c d"""")
33+
assertContains(visualText, """style="background-color: red;"""")
34+
assertContains(visualText, """data-foo="foo-data"""")
35+
}
36+
37+
override fun setUp() {
38+
super.setUp()
39+
setupDocument("test.html", HTML_SNIPPET)
40+
}
41+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dev.zbinski.htmlattributefolder
2+
3+
private const val TSX_SNIPPET = """
4+
export const Component = () => (
5+
<div class="a b" className="c d" style={{ backgroundColor: "red" }} data-foo="foo-data" />
6+
)
7+
"""
8+
9+
class AttributeFolderTSXTest : BaseAttributeFolderTest() {
10+
fun testFoldingTSXAttributesCollapsed() {
11+
assertContains(document.text, """class="a b"""")
12+
assertContains(document.text, """className="c d"""")
13+
assertContains(document.text, """style={{ backgroundColor: "red" }}""")
14+
assertContains(document.text, """data-foo="foo-data"""")
15+
16+
configureAttributeFolder(collapseByDefault = true)
17+
18+
val visualText = applyPluginFoldingAndRender()
19+
assertContains(visualText, """class="__PLACEHOLDER__"""")
20+
assertContains(visualText, """className="__PLACEHOLDER__"""")
21+
assertContains(visualText, """style={{ backgroundColor: "red" }}""")
22+
assertContains(visualText, """data-foo="foo-data"""")
23+
}
24+
25+
fun testFoldingTSXAttributesUncollapsed() {
26+
assertContains(document.text, """class="a b"""")
27+
assertContains(document.text, """className="c d"""")
28+
assertContains(document.text, """style={{ backgroundColor: "red" }}""")
29+
assertContains(document.text, """data-foo="foo-data"""")
30+
31+
configureAttributeFolder(collapseByDefault = false)
32+
33+
val visualText = applyPluginFoldingAndRender()
34+
assertContains(visualText, """class="a b"""")
35+
assertContains(visualText, """className="c d"""")
36+
assertContains(visualText, """style={{ backgroundColor: "red" }}""")
37+
assertContains(visualText, """data-foo="foo-data"""")
38+
}
39+
40+
override fun setUp() {
41+
super.setUp()
42+
setupDocument("test.tsx", TSX_SNIPPET)
43+
skipTestIfJSXIsNotSupported()
44+
}
45+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package dev.zbinski.htmlattributefolder
2+
3+
import com.intellij.openapi.editor.Document
4+
import com.intellij.openapi.editor.FoldRegion
5+
import com.intellij.psi.PsiFile
6+
import com.intellij.testFramework.fixtures.BasePlatformTestCase
7+
import org.junit.Assume
8+
9+
abstract class BaseAttributeFolderTest : BasePlatformTestCase() {
10+
protected lateinit var file: PsiFile
11+
protected lateinit var document: Document
12+
13+
protected fun setupDocument(fileName: String, text: String) {
14+
file = myFixture.configureByText(fileName, text)
15+
document = myFixture.getDocument(file)
16+
}
17+
18+
protected fun configureAttributeFolder(collapseByDefault: Boolean, listOfAttributes: ArrayList<String> = arrayListOf("class", "className")) {
19+
val state = AttributeFolderState.instance
20+
state.attributes = listOfAttributes
21+
state.foldingMethod = 0
22+
state.collapseByDefault = collapseByDefault
23+
state.placeholder = "__PLACEHOLDER__"
24+
}
25+
26+
protected fun applyPluginFoldingAndRender(): String {
27+
val builder = AttributeFolder()
28+
val descriptors = builder.buildFoldRegions(file, document, false)
29+
val editor = myFixture.editor
30+
31+
editor.foldingModel.runBatchFoldingOperation {
32+
for (d in descriptors) {
33+
val region = editor.foldingModel.addFoldRegion(
34+
d.range.startOffset,
35+
d.range.endOffset,
36+
builder.getPlaceholderText(d.element)
37+
)
38+
if (region != null) {
39+
region.isExpanded = !builder.isCollapsedByDefault(d.element)
40+
}
41+
}
42+
}
43+
44+
return renderVisualText(document.text, editor.foldingModel.allFoldRegions.toList())
45+
}
46+
47+
protected fun renderVisualText(documentText: String, regions: List<FoldRegion>): String {
48+
val collapsed = regions.filter { !it.isExpanded }
49+
.sortedBy { it.startOffset }
50+
51+
val sb = StringBuilder()
52+
var i = 0
53+
54+
for (r in collapsed) {
55+
if (r.startOffset < i) continue
56+
sb.append(documentText.substring(i, r.startOffset))
57+
sb.append(r.placeholderText ?: "")
58+
i = r.endOffset
59+
}
60+
61+
sb.append(documentText.substring(i))
62+
return sb.toString()
63+
}
64+
65+
protected fun assertContains(actual: String, expectedSubstring: String, context: String = "") {
66+
kotlin.test.assertTrue(
67+
actual.contains(expectedSubstring),
68+
message = buildString {
69+
appendLine("Expected substring not found:")
70+
appendLine(" expected: $expectedSubstring")
71+
appendLine()
72+
appendLine(" actual:")
73+
appendLine(actual)
74+
if (context.isNotBlank()) {
75+
appendLine()
76+
appendLine(" context:")
77+
appendLine(context)
78+
}
79+
}
80+
)
81+
}
82+
83+
protected fun skipTestIfJSXIsNotSupported() {
84+
val languageId = file.language.id
85+
Assume.assumeTrue(
86+
"Skipping test: JSX/TSX not supported in this test runtime (languageId=$languageId)",
87+
languageId.contains("TypeScript", ignoreCase = true) && languageId.contains("JSX", ignoreCase = true)
88+
)
89+
}
90+
}

0 commit comments

Comments
 (0)