Skip to content

Commit

Permalink
Merge pull request #62 from omochi/add-brtest
Browse files Browse the repository at this point in the history
ブラウザでのテストを追加する
  • Loading branch information
omochi authored Apr 19, 2024
2 parents cfab4df + fee0d9e commit 16b518e
Show file tree
Hide file tree
Showing 24 changed files with 252 additions and 54 deletions.
5 changes: 4 additions & 1 deletion BrowserTests/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ addPage(name: "QSComponents")
addPage(name: "QSMarkup")
addPage(name: "QSDisplayingData")
addPage(name: "QSConditional")
addPage(name: "QSRenderingLists")
addPage(name: "QSLists")
addPage(name: "QSEvents")
addPage(name: "QSUpdating")
addPage(name: "QSSharing")

let package = Package(
name: "BrowserTests",
Expand Down
3 changes: 3 additions & 0 deletions BrowserTests/Sources/BRTSupport/RenderRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import React
import JavaScriptKit
import SRTDOM

public var root: ReactRoot? = nil

public func renderRoot(component: some Component) throws {
let body = try JSWindow.global.document.body.unwrap("body")
let root = ReactRoot(element: body)
BRTSupport.root = root
root.render(node: component)
}
55 changes: 55 additions & 0 deletions BrowserTests/Sources/BRTSupport/common.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
* {
box-sizing: border-box;
}

body {
font-family: sans-serif;
margin: 20px;
padding: 0;
}

h1 {
margin-top: 0;
font-size: 22px;
}

h2 {
margin-top: 0;
font-size: 20px;
}

h3 {
margin-top: 0;
font-size: 18px;
}

h4 {
margin-top: 0;
font-size: 16px;
}

h5 {
margin-top: 0;
font-size: 14px;
}

h6 {
margin-top: 0;
font-size: 12px;
}

code {
font-size: 1.2em;
}

ul {
padding-inline-start: 20px;
}

.avatar {
border-radius: 50%;
}

.large {
border: 4px solid gold;
}
1 change: 1 addition & 0 deletions BrowserTests/Sources/GenPagesModule/GenPages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public struct GenPages {
<head>
<meta charset="utf-8">
<title>\(page)</title>
<link rel="stylesheet" type="text/css" href="/Sources/BRTSupport/common.css">
<script type="module">
import { load } from "/src/loader/load.ts";
load("/.build/wasm32-unknown-wasi/debug/\(page).wasm");
Expand Down
48 changes: 0 additions & 48 deletions BrowserTests/Sources/QSDisplayingData/styles.css
Original file line number Diff line number Diff line change
@@ -1,51 +1,3 @@
* {
box-sizing: border-box;
}

body {
font-family: sans-serif;
margin: 20px;
padding: 0;
}

h1 {
margin-top: 0;
font-size: 22px;
}

h2 {
margin-top: 0;
font-size: 20px;
}

h3 {
margin-top: 0;
font-size: 18px;
}

h4 {
margin-top: 0;
font-size: 16px;
}

h5 {
margin-top: 0;
font-size: 14px;
}

h6 {
margin-top: 0;
font-size: 12px;
}

code {
font-size: 1.2em;
}

ul {
padding-inline-start: 20px;
}

.avatar {
border-radius: 50%;
}
Expand Down
30 changes: 30 additions & 0 deletions BrowserTests/Sources/QSEvents/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React
import JavaScriptKit
import SRTDOM
import BRTSupport

// https://ja.react.dev/learn#responding-to-events

struct MyButton: Component {
func render() -> Node {
let handleClick = EventListener { (_) in
do {
try JSWindow.global.alert("You clicked me!")
} catch {
print(error)
}
}

return button(
listeners: [
"click": handleClick
]
) {
"Click me"
}
}
}

try renderRoot(component: MyButton())


File renamed without changes.
42 changes: 42 additions & 0 deletions BrowserTests/Sources/QSSharing/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React
import JavaScriptKit
import SRTDOM
import BRTSupport

// https://ja.react.dev/learn#sharing-data-between-components

struct MyButton: Component {
var count: Int
var onClick: EventListener

func render() -> Node {
return button(
listeners: [
"click": onClick
]
) {
"Clicked \(count) times"
}
}
}

struct MyApp: Component {
@State var count = 0

func render() -> Node {
let handleClick = EventListener { (_) in
count += 1
}

return div {
h1 { "Counters that update together" }
MyButton(count: count, onClick: handleClick)
MyButton(count: count, onClick: handleClick)
}
}
}

try addCSS(path: "/Sources/QSSharing/styles.css")
try renderRoot(component: MyApp())


5 changes: 5 additions & 0 deletions BrowserTests/Sources/QSSharing/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
button {
display: block;
margin-bottom: 5px;
}

39 changes: 39 additions & 0 deletions BrowserTests/Sources/QSUpdating/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React
import JavaScriptKit
import SRTDOM
import BRTSupport

// https://ja.react.dev/learn#updating-the-screen

struct MyButton: Component {
@State var count = 0

func render() -> Node {
let handleClick = EventListener { (_) in
count += 1
}

return button(
listeners: [
"click": handleClick
]
) {
"Clicked \(count) times"
}
}
}

struct MyApp: Component {
func render() -> Node {
div {
h1 { "Counters that update separately" }
MyButton()
MyButton()
}
}
}

try addCSS(path: "/Sources/QSUpdating/styles.css")
try renderRoot(component: MyApp())


5 changes: 5 additions & 0 deletions BrowserTests/Sources/QSUpdating/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
button {
display: block;
margin-bottom: 5px;
}

11 changes: 10 additions & 1 deletion BrowserTests/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BrowserTests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "browsertests",
"version": "1.0.0",
"description": "",
"type": "module",
"main": "index.js",
"scripts": {
"dev": "vite",
Expand Down
1 change: 1 addition & 0 deletions BrowserTests/pages/QSComponents.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BrowserTests/pages/QSConditional.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BrowserTests/pages/QSDisplayingData.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions BrowserTests/pages/QSEvents.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BrowserTests/pages/QSMarkup.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions BrowserTests/pages/QSSharing.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions BrowserTests/pages/QSUpdating.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion BrowserTests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "CommonJS",
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"target": "ESNext",
"outDir": "js",
Expand Down
Loading

0 comments on commit 16b518e

Please sign in to comment.