Skip to content

Commit 0354949

Browse files
authored
feat(offline-templates): enhance offline templates and add tests (#142)
* feat(offline-templates): enhance offline templates and add tests - Refactor main.js to include setMessage function for dynamic content - Create jsconfig.json for test configuration - Add main.test.js to test setMessage functionality - Update Main.fs to implement a Counter type with increment/decrement methods - Create App.Tests.fsproj for testing setup - Implement Main.Test.fs for Counter unit tests * feat(offline-templates): add utility functions and tests - Implement createMessage and add functions in main.js - Add jsconfig.json for test configuration - Create unit tests for createMessage and add functions - Update perla.json to include testing configuration - Implement multiply and createGreeting functions in main.jsx - Add jsconfig.json for empty-jsx tests - Create unit tests for multiply and createGreeting functions - Update perla.json for fable-feliz to include testing configuration - Add add and formatName functions in Components.fs - Create unit tests for add and formatName functions
1 parent db265c1 commit 0354949

File tree

18 files changed

+353
-84
lines changed

18 files changed

+353
-84
lines changed

src/Perla/offline-templates/basic/src/main.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
console.log("Welcome to Perla!");
22

3-
const appRoot = document.querySelector("app-root");
4-
const element = document.createElement("section");
5-
element.innerHTML = `
3+
export function setMessage(element) {
4+
element.innerHTML = `
65
<h1>Perla Offline Template</h1>
76
<p>This is a basic offline template for Perla.</p>
87
<p>It is designed to work without any server-side processing.</p>
98
<p>Feel free to modify it as per your requirements.</p>
10-
`;
9+
`;
10+
}
1111

1212
document.addEventListener("DOMContentLoaded", () => {
13+
const appRoot = document.querySelector("app-root");
1314
if (appRoot) {
15+
const element = document.createElement("section");
16+
setMessage(element);
1417
appRoot.appendChild(element);
1518
} else {
1619
console.error("App root element not found.");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../jsconfig.json",
3+
"include": ["../src/**/*.js", "./**/*.js"],
4+
"typeAcquisition": {
5+
"enable": true,
6+
"include": ["qunit"]
7+
}
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { setMessage } from "../src/main.js";
2+
3+
QUnit.module("Main Module Tests");
4+
5+
QUnit.test("setMessage sets correct HTML content", (assert) => {
6+
const fixture = document.querySelector("#qunit-fixture");
7+
8+
setMessage(fixture);
9+
assert.equal(
10+
fixture.innerHTML,
11+
`
12+
<h1>Perla Offline Template</h1>
13+
<p>This is a basic offline template for Perla.</p>
14+
<p>It is designed to work without any server-side processing.</p>
15+
<p>Feel free to modify it as per your requirements.</p>
16+
`
17+
);
18+
});

src/Perla/offline-templates/blocal/src/main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
console.log("Welcome to Perla!");
22

3+
export function createMessage(title, description) {
4+
return `${title}: ${description}`;
5+
}
6+
7+
export function add(a, b) {
8+
return a + b;
9+
}
10+
311
const appRoot = document.querySelector("app-root");
412
const element = document.createElement("section");
513
element.innerHTML = `
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../jsconfig.json",
3+
"include": ["../src/**/*.js", "./**/*.js"],
4+
"typeAcquisition": {
5+
"enable": true,
6+
"include": ["qunit"]
7+
}
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { createMessage, add } from "../src/main.js";
2+
3+
QUnit.module("Blocal Module Tests");
4+
5+
QUnit.test("createMessage formats message correctly", (assert) => {
6+
const result = createMessage("Hello", "World");
7+
assert.equal(result, "Hello: World", "Should format message correctly");
8+
9+
const result2 = createMessage("Perla", "Offline Template");
10+
assert.equal(
11+
result2,
12+
"Perla: Offline Template",
13+
"Should handle different inputs"
14+
);
15+
});
16+
17+
QUnit.test("add function works correctly", (assert) => {
18+
assert.equal(add(2, 3), 5, "Should add two positive numbers");
19+
assert.equal(add(-1, 1), 0, "Should handle negative numbers");
20+
assert.equal(add(0, 0), 0, "Should handle zero");
21+
});

src/Perla/offline-templates/empty-fable/.config/dotnet-tools.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
"version": 1,
33
"isRoot": true,
44
"tools": {
5-
"perla": {
6-
"version": "1.0.0-beta-028",
7-
"commands": ["perla"],
8-
"rollForward": false
9-
},
105
"fable": {
116
"version": "4.25.0",
127
"commands": ["fable"],

src/Perla/offline-templates/empty-fable/perla.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
"project": "./src/App.fsproj",
66
"sourceMaps": true
77
},
8+
"testing": {
9+
"fable": {
10+
"project": "./tests/App.Tests.fsproj",
11+
"sourceMaps": true
12+
}
13+
},
814
"dependencies": {}
915
}
Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
11
module Main
22

33
open Fable.Core
4-
open Browser
4+
open Browser.Dom
5+
open Browser.Types
56

6-
let element = document.createElement("section")
7+
type Counter(initialValue: int) =
8+
let mutable count = initialValue
79

8-
element.innerHTML <-
9-
"""
10-
<h1>Welcome to the Fable Feliz App!</h1>
11-
<p>This is a basic template using Feliz for Fable applications.</p>
12-
<p>Enjoy building your app!</p>
13-
"""
10+
member this.Increment() =
11+
count <- count + 1
12+
count
13+
14+
member this.Decrement() =
15+
count <- count - 1
16+
count
17+
18+
member this.GetValue() = count
19+
20+
21+
let createCounter initialValue =
22+
let counter = Counter initialValue
23+
let element = document.createElement("div")
24+
25+
element.innerHTML <- sprintf "<p>Counter: %d</p>" (counter.GetValue())
26+
27+
document.addEventListener(
28+
"click",
29+
fun _ ->
30+
counter.Increment() |> ignore
31+
element.innerHTML <- sprintf "<p>Counter: %d</p>" (counter.GetValue())
32+
)
33+
34+
element
1435

1536
document.addEventListener(
1637
"DOMContentLoaded",
1738
fun _ ->
1839
let root = document.getElementById "feliz-app"
40+
let element = document.createElement("section")
41+
42+
element.innerHTML <-
43+
"""<h1>Welcome to the Fable Feliz App!</h1>
44+
<p>This is a basic template using Feliz for Fable applications.</p>
45+
<p>Enjoy building your app!</p>"""
1946

2047
if not(isNull root) then
2148
root.appendChild element |> ignore
49+
element.appendChild(createCounter 0) |> ignore
2250
else
2351
console.error("App root element not found.")
2452
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
</PropertyGroup>
5+
<ItemGroup>
6+
<Compile Include="Main.Test.fs" />
7+
</ItemGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Fable.Browser.Dom" Version="2.20.0" />
10+
<PackageReference Include="Fable.Core" Version="4.5.0" />
11+
<PackageReference Include="Perla.Fable.QUnit" Version="1.0.0-beta-034" />
12+
</ItemGroup>
13+
<ItemGroup>
14+
<ProjectReference Include="..\src\App.fsproj" />
15+
</ItemGroup>
16+
</Project>

0 commit comments

Comments
 (0)