Skip to content

Commit 43cf13e

Browse files
Merge pull request #34 from amanshuraikwar/js-tests
Js tests
2 parents 12a1d62 + bb53546 commit 43cf13e

10 files changed

Lines changed: 57 additions & 67 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Tests :shared JS Browser
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
branches:
7+
- trunk
8+
9+
jobs:
10+
web-build-html-js:
11+
runs-on: macos-11
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2.3.1
15+
16+
- name: Set up JDK 11
17+
uses: actions/setup-java@v1
18+
with:
19+
java-version: 11
20+
21+
- name: Tests :shared
22+
run: |
23+
cd project-files/Portfolio
24+
./gradlew :shared:cleanJsBrowserTest :shared:jsBrowserTest --stacktrace

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
![](https://github.com/amanshuraikwar/amanshuraikwar.github.io/actions/workflows/build-web-html-js.yml/badge.svg)
88
       
99
<a href="https://amanshuraikwar.github.io/tests/shared/android-jvm/">![](https://github.com/amanshuraikwar/amanshuraikwar.github.io/actions/workflows/shared-tests-android-jvm.yml/badge.svg)</a>
10+
&nbsp; &nbsp; &nbsp; &nbsp;
11+
<a href="https://amanshuraikwar.github.io/tests/shared/js-browser/">![](https://github.com/amanshuraikwar/amanshuraikwar.github.io/actions/workflows/shared-tests-js-browser.yml/badge.svg)</a>
1012

1113
</p>
1214

project-files/Portfolio/buildWebsite.kts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,25 @@ File(".") exec "cp -R build/home/ build/"
5151
println("Deleting build/home...")
5252
File(".") exec "rm -rf build/home"
5353

54-
println("Running theme data tests in :shared for Android/JVM target...")
55-
File(".") exec "./gradlew :shared:cleanTestDebugUnitTest :shared:testDebugUnitTest"
5654
println("Creating directory build/tests...")
5755
File(".") exec "mkdir build/tests"
5856
println("Creating directory build/tests/shared...")
5957
File(".") exec "mkdir build/tests/shared"
58+
59+
println("Running theme data tests in :shared for Android/JVM target...")
60+
File(".") exec "./gradlew :shared:cleanTestDebugUnitTest :shared:testDebugUnitTest"
6061
println("Creating directory build/tests/shared/android-jvm...")
6162
File(".") exec "mkdir build/tests/shared/android-jvm"
6263
println("Copying contents from shared/build/reports/tests/testDebugUnitTest/ to build/tests/shared/android-jvm/...")
6364
File(".") exec "cp -R shared/build/reports/tests/testDebugUnitTest/ build/tests/shared/android-jvm/"
6465

66+
println("Running theme data tests in :shared for Js Browser...")
67+
File(".") exec "./gradlew :shared:cleanJsBrowserTest :shared:jsBrowserTest --stacktrace"
68+
println("Creating directory build/tests/shared/js-browser...")
69+
File(".") exec "mkdir build/tests/shared/js-browser"
70+
println("Copying contents from shared/build/reports/tests/jsBrowserTest/ to build/tests/shared/js-browser/...")
71+
File(".") exec "cp -R shared/build/reports/tests/jsBrowserTest/ build/tests/shared/js-browser/"
72+
6573
data class BlogListEntry(
6674
val title: String,
6775
val date: String,

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/commonMain/kotlin/io/github/amanshuraikwar/portfolio/BlogListDataStore.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ class BlogListDataStore {
66

77
fun getBlogListData(): List<BlogListDataItem> {
88
return listOf(
9+
BlogListDataItem(
10+
title = "My Portfolio",
11+
date = "December 25, 2021",
12+
firstParagraph = "This is my portfolio. It is a KMM project which runs on Web, Android, iOS. The business logic is written in Kotlin. The Web app is written in Kotlin, built using Jetpack Compose for Web. The Android app is written in Kotlin, built using Jetpack Compose. The iOS app UI is written in Swift, built using Swift UI and it uses the KMM shared module for business logic.",
13+
link = "https://amanshuraikwar.github.io/portfolio",
14+
),
915
BlogListDataItem(
1016
title = "How to use highlight.js in your Jetpack Compose Web project",
1117
date = "December 14, 2021",
@@ -18,12 +24,6 @@ class BlogListDataStore {
1824
firstParagraph = "If you are writing unit tests for your Kotlin project and need to mock certain objects, there is high chance that you are using MockK. Here are a couple of issues that I faced while using MockK in my KMM project. The project has Android and iOS apps with business logic written in shared KMM modules, I was writing unit tests to test this business logic.",
1925
link = "https://amanshuraikwar.github.io/mockk-kmm-issues",
2026
),
21-
BlogListDataItem(
22-
title = "My Portfolio",
23-
date = "December 11, 2021",
24-
firstParagraph = "This is my portfolio. It is a KMM project which runs on Web, Android, iOS. The business logic is written in Kotlin. The Web app is written in Kotlin, built using Jetpack Compose for Web. The Android app is written in Kotlin, built using Jetpack Compose. The iOS app UI is written in Swift, built using Swift UI and it uses the KMM shared module for business logic.",
25-
link = "https://amanshuraikwar.github.io/portfolio",
26-
),
2727
BlogListDataItem(
2828
title = "Introducing Next Bus SG",
2929
date = "December 8, 2021",

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)