|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at https://opensource.org/license/UPL. |
| 5 | + */ |
| 6 | + |
| 7 | +package com.example; |
| 8 | + |
| 9 | +import io.micronaut.http.client.HttpClient; |
| 10 | +import io.micronaut.http.client.annotation.Client; |
| 11 | +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; |
| 12 | +import jakarta.inject.Inject; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +@MicronautTest |
| 19 | +public class ReactViewTest { |
| 20 | + @Inject |
| 21 | + @Client("/") |
| 22 | + HttpClient client; |
| 23 | + |
| 24 | + @Test |
| 25 | + void rootViewRendersServerSide() { |
| 26 | + var html = client.toBlocking().exchange("/", String.class).body(); |
| 27 | + |
| 28 | + // Check that title is correctly rendered |
| 29 | + assertTrue(html.contains("<title>Recharts Examples</title>"), "Title not set correctly"); |
| 30 | + |
| 31 | + // Check the embedded rootProps and rootComponent in the script tag |
| 32 | + assertTrue(html.contains("\"rootProps\":{\"width\":500,\"title\":\"Recharts Examples\",\"height\":300,\"url\":\"/\"}"), "rootProps not set correctly"); |
| 33 | + assertTrue(html.contains("\"rootComponent\":\"App\""), "rootComponent not set correctly"); |
| 34 | + |
| 35 | + // Check SVG elements |
| 36 | + assertEquals(11, countOccurrences(html, "<svg"), "Number of SVG elements incorrect"); |
| 37 | + } |
| 38 | + |
| 39 | + private static int countOccurrences(String str, String subStr) { |
| 40 | + int count = 0; |
| 41 | + int index = 0; |
| 42 | + |
| 43 | + while ((index = str.indexOf(subStr, index)) != -1) { |
| 44 | + count++; |
| 45 | + index += subStr.length(); // Move past the last found substring |
| 46 | + } |
| 47 | + |
| 48 | + return count; |
| 49 | + } |
| 50 | +} |
0 commit comments