-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #579 from nfdi4plants/react_tests
init react component tests 🎉
- Loading branch information
Showing
26 changed files
with
2,997 additions
and
1,116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,6 @@ release.sh | |
/.db/neo4j | ||
/src/Client/output | ||
/tests/Client/output | ||
/tests/Components/output | ||
/.paket | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Components | ||
|
||
open Fable.Core | ||
open Feliz | ||
|
||
|
||
/// These 2 Attributes will remove the type from the generated JS code | ||
/// ⚠️ Due to [<Mangle(false)>] you can never use overloads in the same file! | ||
[<Erase; Mangle(false)>] | ||
type TestInput = | ||
|
||
/// [<ExportDefault>] can only be used on a single member in a module | ||
/// [<NamedParams>] is used for correct type hinting in typescript | ||
/// with: export function TestInput({ children, number }: {children?: ReactElement, number?: int32 }): any { | ||
/// without: export function TestInput(children?: ReactElement, number?: int32): any { | ||
/// ⚠️ ... fails because react requires object as input | ||
[<ExportDefault; NamedParams>] | ||
static member TestInput(?children: ReactElement, ?number: int) : JSX.Element = | ||
let state, useState = React.useState (number |> Option.defaultValue 0) | ||
Html.div [ | ||
prop.children [ | ||
if children.IsSome then children.Value | ||
Html.div [ | ||
prop.textf "Number: %d" state | ||
] | ||
Html.input [ | ||
prop.type'.number | ||
prop.onChange(fun (number:int) -> useState number) | ||
] | ||
] | ||
] | ||
// 👀 This is used to remove type hint to: ReactElement in typescript. Which would trigger warning when using in .tsx file. | ||
|> unbox<JSX.Element> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Components | ||
|
||
open Fable.Core | ||
open Feliz | ||
|
||
/// https://fable.io/blog/2022/2022-10-12-react-jsx.html | ||
[<AutoOpen>] | ||
module Util = | ||
|
||
let inline toJsx (el: ReactElement) : JSX.Element = unbox el | ||
let inline toReact (el: JSX.Element) : ReactElement = unbox el | ||
|
||
/// Enables use of Feliz styles within a JSX hole | ||
let inline toStyle (styles: IStyleAttribute list) : obj = JsInterop.createObj (unbox styles) | ||
let toClass (classes: (string * bool) list) : string = | ||
classes | ||
|> List.choose (fun (c, b) -> | ||
match c.Trim(), b with | ||
| "", _ | ||
| _, false -> None | ||
| c, true -> Some c) | ||
|> String.concat " " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { describe, test, expect } from 'vitest'; | ||
|
||
describe('Native DOM', () => { | ||
test('creates an element in the DOM', () => { | ||
const div = document.createElement('div'); | ||
expect(div).toBeDefined(); | ||
}); | ||
|
||
|
||
test('environment should be jsdom', () => { | ||
expect(globalThis.navigator).toBeDefined(); | ||
expect(globalThis.navigator.userAgent).toContain('jsdom'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
// Assuming TestInput is the transpiled component from Fable | ||
import Example from './output/Example'; // Update the import path as needed | ||
|
||
describe('TestInput Component', () => { | ||
it('renders with initial number and children', () => { | ||
render( | ||
<Example number={42}> | ||
<span>Child Element</span> | ||
</Example> | ||
); | ||
|
||
// Check for child element | ||
expect(screen.getByText('Child Element')).toBeInTheDocument(); | ||
|
||
// Check for initial number | ||
expect(screen.getByText('Number: 42')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders with default number (0) when no number prop is provided', () => { | ||
render( | ||
<Example> | ||
<span>Default Number</span> | ||
</Example> | ||
); | ||
|
||
// Check for default number | ||
expect(screen.getByText('Number: 0')).toBeInTheDocument(); | ||
}); | ||
|
||
it('updates number when input value changes', async () => { | ||
render(<Example number={10} />); | ||
|
||
// Get the number input | ||
const input = screen.getByRole('spinbutton'); // Default ARIA role for number inputs | ||
|
||
// Simulate changing the input value | ||
fireEvent.change(input, { target: { value: '25' } }); | ||
|
||
// Check if the number is updated | ||
expect(screen.getByText('Number: 25')).toBeInTheDocument(); | ||
}); | ||
|
||
it('handles non-numeric input gracefully', async () => { | ||
render(<Example />); | ||
|
||
// Get the number input | ||
const input = screen.getByRole('spinbutton'); | ||
|
||
// Simulate entering non-numeric value | ||
fireEvent.change(input, { target: { value: 'not-a-number' } }); | ||
|
||
// Check if the number remains 0 (default state) | ||
expect(screen.getByText('Number: 0')).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.