Skip to content

Commit 8c2d599

Browse files
added js tests
1 parent 12a1d62 commit 8c2d599

6 files changed

Lines changed: 15 additions & 59 deletions

File tree

project-files/Portfolio/shared/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,12 @@ android {
9898
minSdk = 23
9999
targetSdk = 30
100100
}
101+
}
102+
103+
// a temporary workaround for a bug in jsRun invocation - see https://youtrack.jetbrains.com/issue/KT-48273
104+
afterEvaluate {
105+
rootProject.extensions.configure<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension> {
106+
versions.webpackDevServer.version = "4.0.0"
107+
versions.webpackCli.version = "4.9.0"
108+
}
101109
}

project-files/Portfolio/shared/src/androidTest/kotlin/io/github/amanshuraikwar/portfolio/TestFun.kt

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,4 @@ actual fun runTest(block: suspend CoroutineScope.() -> Unit) = runBlocking { blo
77

88
actual annotation class JsName actual constructor(
99
actual val name: String
10-
)
11-
12-
/** Read the given resource as binary data. */
13-
actual fun readBinaryResource(
14-
resourceName: String
15-
): ByteArray {
16-
return ClassLoader
17-
.getSystemResourceAsStream(resourceName)!!
18-
.readBytes()
19-
}
10+
)

project-files/Portfolio/shared/src/commonTest/kotlin/io.github.amanshuraikwar.portfolio/TestFun.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@ package io.github.amanshuraikwar.portfolio
33
import kotlinx.coroutines.CoroutineScope
44

55
expect fun runTest(block: suspend CoroutineScope.() -> Unit)
6-
expect annotation class JsName constructor(val name: String)
76

8-
/** Read the given resource as binary data. */
9-
expect fun readBinaryResource(
10-
resourceName: String
11-
): ByteArray
7+
expect annotation class JsName constructor(val name: String)

project-files/Portfolio/shared/src/commonTest/kotlin/io.github.amanshuraikwar.portfolio/ThemeDataTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class ThemeDataTest {
151151
}
152152

153153
@Test
154+
@JsName("defaultSelectedThemeColorsNameAndTheThemeExists")
154155
fun `default selected theme colors name and the theme exists`() {
155156
val portfolioRepository = PortfolioRepository(
156157
settings = MockSettings(),
@@ -167,6 +168,7 @@ class ThemeDataTest {
167168
}
168169

169170
@Test
171+
@JsName("defaultSelectedThemeColorsNameIsFirstFromTheDefaultThemeDataWhenTheThemeDoesNotExist")
170172
fun `default selected theme colors name is first from the default theme data when the theme does not exist`() {
171173
val portfolioRepository = PortfolioRepository(
172174
settings = MockSettings(),
@@ -187,6 +189,7 @@ class ThemeDataTest {
187189
}
188190

189191
@Test
192+
@JsName("setSelectedThemeColorsNameFlowEmitIsCorrect")
190193
fun `set selected theme colors name flow emit is correct`() {
191194
val portfolioRepository = PortfolioRepository(
192195
settings = MockSettings(),
@@ -211,6 +214,7 @@ class ThemeDataTest {
211214
}
212215

213216
@Test
217+
@JsName("setSelectedThemeColorsNameCorrespondingThemeDoesNotExist")
214218
fun `set selected theme colors name corresponding theme does not exist`() {
215219
val portfolioRepository = PortfolioRepository(
216220
settings = MockSettings(),
Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
package io.github.amanshuraikwar.portfolio
22

3-
import kotlinx.cinterop.addressOf
4-
import kotlinx.cinterop.usePinned
53
import kotlinx.coroutines.CoroutineScope
64
import kotlinx.coroutines.runBlocking
7-
import platform.Foundation.NSBundle
8-
import platform.Foundation.NSData
9-
import platform.Foundation.dataWithContentsOfFile
10-
import platform.posix.memcpy
115

126
actual fun runTest(block: suspend CoroutineScope.() -> Unit) = runBlocking { block() }
137

148
actual annotation class JsName actual constructor(
159
actual val name: String
16-
)
17-
18-
/** Read the given resource as binary data. */
19-
actual fun readBinaryResource(
20-
resourceName: String
21-
): ByteArray {
22-
// split based on "." and "/". We want to strip the leading ./ and
23-
// split the extension
24-
val pathParts = resourceName.split("[.|/]".toRegex())
25-
// pathParts looks like
26-
// [, , test_case_input_one, bin]
27-
val path = NSBundle.mainBundle
28-
.pathForResource("resources/${pathParts[2]}", pathParts[3])
29-
val data = NSData.dataWithContentsOfFile(path!!)
30-
return data!!.toByteArray()
31-
}
32-
33-
internal fun NSData.toByteArray(): ByteArray {
34-
return ByteArray(length.toInt()).apply {
35-
usePinned {
36-
memcpy(it.addressOf(0), bytes, length)
37-
}
38-
}
39-
}
10+
)
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.amanshuraikwar.portfolio
22

3-
import io.ktor.utils.io.core.toByteArray
43
import kotlinx.coroutines.CoroutineScope
54
import kotlinx.coroutines.GlobalScope
65
import kotlinx.coroutines.await
@@ -19,16 +18,3 @@ actual fun runTest(block: suspend CoroutineScope.() -> Unit): dynamic {
1918
actual typealias JsName = kotlin.js.JsName
2019

2120
private external fun require(module: String): dynamic
22-
23-
@Suppress("ObjectPropertyName")
24-
external val __dirname: dynamic
25-
26-
/** Read the given resource as binary data. */
27-
actual fun readBinaryResource(resourceName: String): ByteArray {
28-
if (resourceName.endsWith(".json")) {
29-
println(JSON.stringify(require("${__dirname}/$resourceName")))
30-
return JSON.stringify(require("${__dirname}/$resourceName")).toByteArray()
31-
} else {
32-
throw IllegalArgumentException("Only .json files are supported")
33-
}
34-
}

0 commit comments

Comments
 (0)