Kotlin wrapper for React Test Renderer, which can be used to unit test React components in a Kotlin/JS project.

Overview

Kotlin API for React Test Renderer

Kotlin wrapper for React Test Renderer, which can be used to unit test React components in a Kotlin/JS project.

How to Use

Installation

With Gradle (using Kotlin DSL):

implementation("io.github.mysticfall:kotlin-react-test:1.0")

Alternatively, using Groovy DSL:

implementation "io.github.mysticfall:kotlin-react-test:1.0"

Code Example

The most straightforward way of using the library is to make your test class implement ReactTestSupport, as shown below:

import mysticfall.kotlin.react.test.ReactTestSupport

class ComponentTest : ReactTestSupport {

    @Test
    fun testHeaderTitle() {
        val renderer = render {
            HeaderTitle {
                attrs {
                    title = "Kotlin/JS"
                }
            }
        }

        val title = renderer.root.findByType(HeaderTitle)

        assertEquals("Kotlin/JS", title.props.title)
    }
}

The project itself has quite an extensive set of test cases, which can serve as examples that show how various features of React Test Renderer can be used in Kotlin.

LICENSE

This project is provided under the terms of MIT License.

Comments
  • Issues with installation

    Issues with installation

    Hi!

    When adding the dependency to my project (I use kotlin("js") plugin):

    dependencies {
        testImplementation("io.github.mysticfall:kotlin-react-test:1.1.0")
    }
    

    I'm getting this:

    Module <managed-automation-ui:frontend-react> has reference public react.dom/HTMLAttributes|null[0], unfortunately neither itself nor its dependencies [
    	<org.jetbrains.kotlin-wrappers:kotlin-styled>
    	<org.jetbrains.kotlin-wrappers:kotlin-react-dom>
    	<org.jetbrains.kotlin-wrappers:kotlin-react-router-dom>
    	<org.jetbrains.kotlin-wrappers:kotlin-react>
    	<io.ktor:ktor-client-js>
    	<org.jetbrains.kotlin-wrappers:kotlin-csstype>
    	<org.jetbrains.kotlin-wrappers:kotlin-extensions>
    	<org.jetbrains.kotlinx:kotlinx-html>
    	<io.ktor:ktor-client-serialization>
    	<io.ktor:ktor-client-json>
    	<io.ktor:ktor-client-core>
    	<io.ktor:ktor-http-cio>
    	<io.ktor:ktor-http>
    	<io.ktor:ktor-utils>
    	<io.ktor:ktor-io>
    	<org.jetbrains.kotlinx:kotlinx-coroutines-core>
    	<org.jetbrains.kotlin-wrappers:kotlin-css>
    	<org.jetbrains.kotlinx:atomicfu>
    	<org.jetbrains.kotlinx:kotlinx-serialization-json>
    	<org.jetbrains.kotlinx:kotlinx-serialization-core>
    	<kotlin>
    ] contain this declaration
    Please check that project configuration is correct and has required dependencies.
    

    Could you advise and maybe create an example project that consumes this library?

    Thanks!

    PS: I created https://youtrack.jetbrains.com/issue/KT-49610 to track the topic of unit testing in React.

    opened by krzema12 5
  • IR JavaScript compiler chokes on a method signature

    IR JavaScript compiler chokes on a method signature

    This happens with kotlin("js") version "1.7.20" in build.gradle.kts:

    java.lang.IllegalStateException: P is not found when encode the signature of public open fun mysticfall.kotlin.react.test.ReactTestSupport.render(mockFactory: (react.ReactElement<P>) -> kotlin.Any, block: react.RBuilder.() -> kotlin.Unit): mysticfall.kotlin.react.test.TestRenderer defined in mysticfall.kotlin.react.test.ReactTestSupport[DeserializedSimpleFunctionDescriptor@7291ba6f]. at org.jetbrains.kotlin.js.naming.EncodeSignatureKt$encodeSignature$typeParameterNamer$1.invoke(encodeSignature.kt:37) at org.jetbrains.kotlin.js.naming.EncodeSignatureKt$encodeSignature$typeParameterNamer$1.invoke(encodeSignature.kt:34) at org.jetbrains.kotlin.js.naming.EncodeSignatureKt.encodeForSignature(encodeSignature.kt:90) at org.jetbrains.kotlin.js.naming.EncodeSignatureKt.encodeForSignature(encodeSignature.kt:132) at org.jetbrains.kotlin.js.naming.EncodeSignatureKt.encodeForSignature(encodeSignature.kt:106) at org.jetbrains.kotlin.js.naming.EncodeSignatureKt.encodeForSignature(encodeSignature.kt:132) at org.jetbrains.kotlin.js.naming.EncodeSignatureKt.encodeForSignature(encodeSignature.kt:106) at org.jetbrains.kotlin.js.naming.EncodeSignatureKt.encodeSignature(encodeSignature.kt:60) at org.jetbrains.kotlin.js.naming.NameSuggestion$Companion.mangleRegularNameIfNecessary$mangledAndStable(NameSuggestion.kt:282) at org.jetbrains.kotlin.js.naming.NameSuggestion$Companion.mangleRegularNameIfNecessary(NameSuggestion.kt:302) at org.jetbrains.kotlin.js.naming.NameSuggestion$Companion.mangleNameIfNecessary(NameSuggestion.kt:262) at org.jetbrains.kotlin.js.naming.NameSuggestion$Companion.access$mangleNameIfNecessary(NameSuggestion.kt:225) at org.jetbrains.kotlin.js.naming.NameSuggestion.generateDefault(NameSuggestion.kt:204) at org.jetbrains.kotlin.js.naming.NameSuggestion.generate(NameSuggestion.kt:147) at org.jetbrains.kotlin.js.naming.NameSuggestion.suggest(NameSuggestion.kt:69) at org.jetbrains.kotlin.js.resolve.diagnostics.JsNameClashChecker.suggestAllPossibleNames(JsNameClashChecker.kt:135) at org.jetbrains.kotlin.js.resolve.diagnostics.JsNameClashChecker.collect(JsNameClashChecker.kt:196) at org.jetbrains.kotlin.js.resolve.diagnostics.JsNameClashChecker.collect(JsNameClashChecker.kt:184) at org.jetbrains.kotlin.js.resolve.diagnostics.JsNameClashChecker.getScope(JsNameClashChecker.kt:177) at org.jetbrains.kotlin.js.resolve.diagnostics.JsNameClashChecker.checkDescriptor(JsNameClashChecker.kt:94) at org.jetbrains.kotlin.js.resolve.diagnostics.JsNameClashChecker.check(JsNameClashChecker.kt:57) at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.runDeclarationCheckers(ModifiersChecker.java:285) at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifierListCommon(ModifiersChecker.java:224) at org.jetbrains.kotlin.resolve.ModifiersChecker$ModifiersCheckingProcedure.checkModifiersForDeclaration(ModifiersChecker.java:183) at org.jetbrains.kotlin.resolve.DeclarationsChecker.process(DeclarationsChecker.kt:95) at org.jetbrains.kotlin.resolve.BodyResolver.resolveBodies(BodyResolver.java:258) at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations(LazyTopDownAnalyzer.kt:227) at org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer.analyzeDeclarations$default(LazyTopDownAnalyzer.kt:58) at org.jetbrains.kotlin.js.analyze.AbstractTopDownAnalyzerFacadeForJS.analyzeFilesWithGivenTrace(TopDownAnalyzerFacadeForJS.kt:140) at org.jetbrains.kotlin.js.analyze.AbstractTopDownAnalyzerFacadeForJS.analyzeFiles(TopDownAnalyzerFacadeForJS.kt:92) at org.jetbrains.kotlin.ir.backend.js.ModulesStructure$runAnalysis$1.invoke(klib.kt:649) at org.jetbrains.kotlin.ir.backend.js.ModulesStructure$runAnalysis$1.invoke(klib.kt:648) at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:115) at org.jetbrains.kotlin.ir.backend.js.ModulesStructure.runAnalysis(klib.kt:648) at org.jetbrains.kotlin.ir.backend.js.KlibKt.prepareAnalyzedSourceModule(klib.kt:507) at org.jetbrains.kotlin.ir.backend.js.KlibKt.prepareAnalyzedSourceModule$default(klib.kt:495) at org.jetbrains.kotlin.cli.js.K2JsIrCompiler.doExecute(K2JsIrCompiler.kt:259) at org.jetbrains.kotlin.cli.js.K2JSCompiler.doExecute(K2JSCompiler.java:183) at org.jetbrains.kotlin.cli.js.K2JSCompiler.doExecute(K2JSCompiler.java:72) at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:99) at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:47) at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101) at org.jetbrains.kotlin.incremental.IncrementalJsCompilerRunner.runCompiler(IncrementalJsCompilerRunner.kt:208) at org.jetbrains.kotlin.incremental.IncrementalJsCompilerRunner.runCompiler(IncrementalJsCompilerRunner.kt:84) at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:373) at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally$default(IncrementalCompilerRunner.kt:318) at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.rebuild(IncrementalCompilerRunner.kt:114) at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:207) at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:79) at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile$default(IncrementalCompilerRunner.kt:69) at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execJsIncrementalCompiler(CompileServiceImpl.kt:566) at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execJsIncrementalCompiler(CompileServiceImpl.kt:101) at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1837) at jdk.internal.reflect.GeneratedMethodAccessor28.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360) at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200) at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197) at java.base/java.security.AccessController.doPrivileged(AccessController.java:712) at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196) at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:587) at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828) at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:705) at java.base/java.security.AccessController.doPrivileged(AccessController.java:399) at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:704) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:833)

    opened by vpeurala 1
  • updating kotlin-wrappers, including react-legacy

    updating kotlin-wrappers, including react-legacy

    Updated for compatibility with the latest builds of kotlin-wrappers. I assume the empty style blocks were non-important, but LMK if there's anything else to tweak.

    No attempt here to migrate to the new childrenbuilder system.

    opened by robertfmurdock 0
  • RBuilderSingle is not a constructor

    RBuilderSingle is not a constructor

    A simple test I've written could not be executed due to TypeError: RBuilderSingle is not a constructor.

    dependencies {
        ...
        implementation("io.github.mysticfall:kotlin-react-test:1.2.0")
    }
    
    import kotlin.test.Test
    import mysticfall.kotlin.react.test.ReactTestSupport
    import react.dom.html.ReactHTML.div
    
    class Test : ReactTestSupport {
    
        @Test
        fun test() {
            val renderer = render {
                div {
                    +"Test"
                }
            }
        }
    }
    
    TypeError: RBuilderSingle is not a constructor
    TypeError: RBuilderSingle is not a constructor
    	at Test.ReactTestSupport.render_or9nxs$$default(C:\Users\Sander\Documents\myproject\src\main\kotlin\ReactTestSupport.kt:18)
    	at Test.ReactTestSupport.render(C:\Users\user\Documents\myproject\src\main\kotlin\ReactTestSupport.kt:14)
    	at Test.ReactTestSupport.render(C:\Users\user\Documents\myproject\src\main\kotlin\ReactTestSupport.kt:12)
    	at Test.test(C:\Users\user\Documents\myproject\src\test\kotlin\Test.kt:9)
    	at <global>.fn(kotlin\my-project-test.js:55)
    	at Context.<anonymous>(C:\Users\user\Documents\myproject\build\js\packages_imported\kotlin-test-js-runner\src\KotlinTestTeamCityConsoleAdapter.ts:72)
    	at <global>.processImmediate(internal/timers.js:462)
    

    Any idea what i'm doing wrong here?

    opened by skrabbenborg 0
  • Installation Issue

    Installation Issue

    I'm having some trouble setting up this package in my new Kotlin/ JS project. I'm using the LEGACY js compiler with kotlin JS 1.5.10 (which comes as the default setting when creating a new project from IntelliJ). The project compiles and builds fine, but when trying to write tests, I get a number of errors such as not being able to render basic dom elements like a "div" tag, and the project not building when I try and use the renderer object to find elements on the page. I've attached a few images of the problems I'm having. Any feedback would be appreciated! domElementsFailure reactPropsFailure

    opened by rd-test-devops 0
Releases(v1.2.0)
Owner
Xavier Cho
Xavier Cho
PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.

Writing unit tests can be hard and sometimes good design has to be sacrificed for the sole purpose of testability. Often testability corresponds to go

PowerMock 3.9k Jan 2, 2023
A simple project to help developers in writing their unit tests in Android Platform.

AndroidUnitTesting A simple project to help developers in writing their unit tests in Android Platform. This is not a multi-module project, but has th

Bruno Gabriel dos Santos 4 Nov 10, 2021
Fixtures for Kotlin providing generated values for unit testing

A tool to generate well-defined, but essentially random, input following the idea of constrained non-determinism.

Appmattus Limited 191 Dec 21, 2022
Android Unit Testing Framework

Robolectric is the industry-standard unit testing framework for Android. With Robolectric, your tests run in a simulated Android environment inside a

Robolectric 5.6k Jan 3, 2023
3 types of Tests in Android (Unit - instrumentation - UI)

UnitTestingPractice 3 types of Tests in Android Unit instrumentation (Integration) UI Unit Testing benefits confirm code work like a charm simulate Ap

Ahmed Tawfiq 8 Mar 23, 2022
Most popular Mocking framework for unit tests written in Java

Most popular mocking framework for Java Current version is 3.x Still on Mockito 1.x? See what's new in Mockito 2! Mockito 3 does not introduce any bre

mockito 13.6k Jan 4, 2023
Toster - Small test dsl based on adb commands that allows you to test the mobile application close to user actions

toster Small test dsl based on adb commands that allows you to test the mobile a

Alexander Kulikovskiy 31 Sep 1, 2022
Lbc-test-app - Test Android Senior Leboncoin

Test Android Senior Leboncoin ?? Mathieu EDET Overview Min API version : 24 This

null 0 Feb 7, 2022
Android background tint test project

Android Background Tint References https://developer.android.com/reference/android/view/View#attr_android:background https://developer.android.com/ref

Ashwin Dinesh 0 Nov 4, 2021
Snapshot/Screenshot test example project

Snapshot Snapshot/Screenshot test example code using Showkase (https://github.com/airbnb/Showkase) Paparazzi (https://github.com/cashapp/paparazzi) Te

Anders Ullnæss 3 Nov 25, 2022
Simple, lightweight, modular components to help conjure your app architecture

Magic Simple, lightweight, modular components and utilities to help conjure your app architecture. Built with Kotlin coroutines to provide flexible as

TeraThought 0 Jan 9, 2022
Powerful, elegant and flexible test framework for Kotlin with additional assertions, property testing and data driven testing

Kotest is a flexible and comprehensive testing tool for Kotlin with multiplatform support. To learn more about Kotest, visit kotest.io or see our quic

Kotest 3.8k Jan 3, 2023
Strikt is an assertion library for Kotlin intended for use with a test runner such as JUnit, Minutest, Spek, or KotlinTest.

Strikt is an assertion library for Kotlin intended for use with a test runner such as JUnit, Minutest, Spek, or KotlinTest.

Rob Fletcher 447 Dec 26, 2022
Proyecto de Kotlin y JPA sobre Hibernate, con algunos test usando JUnit 5 y Mockito.

Contactos Kotlin JPA Ejemplos de una aplicación de manejo de contactos con Kotlin y JPA. Usando para testear la aplicación JUnit 5 y Mockito. Almacena

José Luis González Sánchez 3 Sep 13, 2022
null 866 Dec 27, 2022
A powerful test framework for Android

Cafe A powerful test framework for Android named Case Automated Framework for Everyone. Home Page http://baiduqa.github.com/Cafe/ How to make Cafe dow

Baidu 367 Nov 22, 2022
TestObserver to easily test LiveData and make assertions on them.

JCenter Update LiveData Testing is currently published on JCenter - it will serve packages until February 1st, 2022. LiveData Testing packages will be

Josef Raska 395 Dec 8, 2022
A custom instrumentation test runner for Android that generates XML reports for integration with other tools.

Android JUnit Report Test Runner Introduction The Android JUnit report test runner is a custom instrumentation test runner for Android that creates XM

Jason Sankey 148 Nov 25, 2022
A powerful test framework for Android

Cafe A powerful test framework for Android named Case Automated Framework for Everyone. Home Page http://baiduqa.github.com/Cafe/ How to make Cafe dow

Baidu 367 Nov 22, 2022