Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ktretikoff committed Jul 28, 2021
1 parent 373ad0b commit d15e427
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions kotlin-styled-next/src/jsTest/kotlin/RandomUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import kotlin.random.Random
const val seed = 46271372869541347
val random = Random(seed)

// Functions below are needed for hash to be different even on CSS with the same properties set
fun randomAlign(): Align {
return when (random.nextInt(0, 13)) {
5 -> Align.flexStart
0 -> Align.flexEnd
1 -> Align.baseline
2 -> Align.center
3 -> Align.auto
4 -> Align.end
5 -> Align.flexStart
6 -> Align.inherit
7 -> Align.initial
8 -> Align.selfStart
Expand Down Expand Up @@ -47,7 +48,6 @@ fun randomBorderStyle(): BorderStyle {
0 -> BorderStyle.initial
1 -> BorderStyle.inherit
2 -> BorderStyle.unset

3 -> BorderStyle.none
4 -> BorderStyle.dotted
5 -> BorderStyle.dashed
Expand Down
10 changes: 6 additions & 4 deletions kotlin-styled-next/src/jsTest/kotlin/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlin.js.Promise

typealias Component = ComponentType<RProps>
interface WithId: RProps {
var id: String?
}
typealias Component = ComponentType<WithId>

private val testScope = MainScope()

Expand All @@ -32,8 +35,7 @@ class TestScope : CoroutineScope by testScope {
}

fun renderComponent(component: Component) {
root.clear()
val reactElement = createElement(component, jsObject {})
val reactElement = createElement(component, jsObject { })
render(reactElement, root)
}

Expand All @@ -42,9 +44,9 @@ class TestScope : CoroutineScope by testScope {
return styles.sheet as CSSStyleSheet
}


fun clear() {
unmountComponentAtNode(root)
root.clear()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ class ElementTest {
assertTrue(false, "Could not find rule for class")
}

private suspend fun TestScope.injectComponent(styledComponent: Component): Element {
/**
* Inject [styledComponent] into the DOM and return corresponding [Element]
*/
private suspend fun TestScope.clearAndInject(styledComponent: Component): Element {
clear()
renderComponent(styledComponent)
waitForAnimationFrame()
assertChildrenCount(1)
val styledElement = root.children[0]
assertNotNull(styledElement)
return styledElement
Expand All @@ -81,7 +86,7 @@ class ElementTest {
}
}
}
val styledElement = injectComponent(styledComponent)
val styledElement = clearAndInject(styledComponent)
assertCSS(
styledElement.className, listOf(
"background-color" to Color.blue.toString(),
Expand All @@ -100,7 +105,7 @@ class ElementTest {
}
}
}
val styledElement = injectComponent(styledComponent)
val styledElement = clearAndInject(styledComponent)
assertCSS(
styledElement.className, listOf(
"min-height" to 66.px.toString(),
Expand All @@ -119,7 +124,7 @@ class ElementTest {
}
}
}
val className = injectComponent(styledComponent).className.split(" ").first { "ksc-" !in it }
val className = clearAndInject(styledComponent).className.split(" ").first { "ksc-" !in it }
assertCSS(
className, listOf(
"align-content" to "end",
Expand All @@ -137,7 +142,7 @@ class ElementTest {
}
}
}
val className = injectComponent(styledComponent).className
val className = clearAndInject(styledComponent).className

assertCSS(
".$className.$className", listOf(
Expand All @@ -160,7 +165,7 @@ class ElementTest {
}
}
}
injectComponent(styledComponent).className
clearAndInject(styledComponent).className
assertCSS(
"@media $query", listOf(
"text-transform" to "capitalize",
Expand Down

0 comments on commit d15e427

Please sign in to comment.