FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/tests/test_ref.py at v0.41.0 · pythonnative/pythonnative · GitHub
pythonnative
pythonnative
Repository navigation
Code
Issues
11
(11)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
tests
/
test_ref.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
111 lines (85 loc) · 3.52 KB
Breadcrumbs
pythonnative
/
tests
/
test_ref.py
Copy path
File metadata and controls
111 lines (85 loc) · 3.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Tests for the reconciler's ``ref`` prop support."""
from
__future__
import
annotations
from
typing
import
Any
,
Dict
from
pythonnative
.
component
import
component
from
pythonnative
.
element
import
Element
from
pythonnative
.
hooks
import
Ref
,
use_ref
from
pythonnative
.
reconciler
import
Reconciler
from
pythonnative
.
testing
import
FakeBackend
as
MockBackend
from
pythonnative
.
testing
import
FakeView
as
MockView
def
test_ref_populated_on_mount
()
->
None
:
ref
:
Ref
[
Any
]
=
Ref
()
el
=
Element
(
"Text"
, {
"text"
:
"hi"
,
"ref"
:
ref
}, [])
backend
=
MockBackend
()
Reconciler
(
backend
).
mount
(
el
)
assert
ref
.
current
is
not
None
assert
isinstance
(
ref
.
current
,
MockView
)
assert
ref
.
current
.
type_name
==
"Text"
assert
ref
.
_pn_tag
is
not
None
def
test_ref_not_passed_to_backend
()
->
None
:
ref
:
Ref
[
Any
]
=
Ref
()
el
=
Element
(
"Text"
, {
"text"
:
"hi"
,
"ref"
:
ref
}, [])
backend
=
MockBackend
()
Reconciler
(
backend
).
mount
(
el
)
assert
"ref"
not
in
backend
.
last_create_props
def
test_ref_cleared_on_unmount
()
->
None
:
ref
:
Ref
[
Any
]
=
Ref
()
el
=
Element
(
"Text"
, {
"text"
:
"hi"
,
"ref"
:
ref
}, [])
backend
=
MockBackend
()
rec
=
Reconciler
(
backend
)
rec
.
mount
(
el
)
assert
ref
.
current
is
not
None
# Replace with a different element type; destroys the old tree.
rec
.
reconcile
(
Element
(
"Button"
, {
"title"
:
"ok"
}, []))
assert
ref
.
current
is
None
def
test_ref_repointed_when_ref_swapped
()
->
None
:
"""Swapping the ref on update should populate the new and clear the old."""
old_ref
:
Ref
[
Any
]
=
Ref
()
new_ref
:
Ref
[
Any
]
=
Ref
()
backend
=
MockBackend
()
rec
=
Reconciler
(
backend
)
rec
.
mount
(
Element
(
"Text"
, {
"text"
:
"a"
,
"ref"
:
old_ref
}, []))
assert
old_ref
.
current
is
not
None
first_view
=
old_ref
.
current
rec
.
reconcile
(
Element
(
"Text"
, {
"text"
:
"a"
,
"ref"
:
new_ref
}, []))
assert
old_ref
.
current
is
None
assert
new_ref
.
current
is
first_view
def
test_ref_ignored_when_not_a_ref
()
->
None
:
"""Non-``Ref`` values under ``ref`` are silently ignored, no crashes."""
el
=
Element
(
"Text"
, {
"text"
:
"hi"
,
"ref"
:
"not-a-ref"
}, [])
backend
=
MockBackend
()
Reconciler
(
backend
).
mount
(
el
)
def
test_ref_diff_does_not_trigger_native_update
()
->
None
:
"""Changing only the ref identity should NOT call update_view."""
backend
=
MockBackend
()
rec
=
Reconciler
(
backend
)
ref_a
:
Ref
[
Any
]
=
Ref
()
ref_b
:
Ref
[
Any
]
=
Ref
()
rec
.
mount
(
Element
(
"Text"
, {
"text"
:
"x"
,
"ref"
:
ref_a
}, []))
backend
.
last_update_changes
=
{}
rec
.
reconcile
(
Element
(
"Text"
, {
"text"
:
"x"
,
"ref"
:
ref_b
}, []))
assert
backend
.
last_update_changes
==
{}
assert
ref_a
.
current
is
None
assert
ref_b
.
current
is
not
None
def
test_use_ref_in_component_populated_after_mount
()
->
None
:
captured
:
Dict
[
str
,
Any
]
=
{}
@
component
def
Comp
()
->
Element
:
ref
=
use_ref
(
None
)
captured
[
"ref"
]
=
ref
return
Element
(
"Text"
, {
"text"
:
"hi"
,
"ref"
:
ref
}, [])
Reconciler
(
MockBackend
()).
mount
(
Comp
())
ref
=
captured
[
"ref"
]
assert
ref
.
current
is
not
None
assert
isinstance
(
ref
.
current
,
MockView
)
def
test_ref_frame_mirrored_after_layout
()
->
None
:
"""The layout pass mirrors the committed frame onto ``ref._pn_frame``."""
ref
:
Ref
[
Any
]
=
Ref
()
backend
=
MockBackend
()
rec
=
Reconciler
(
backend
)
child
=
Element
(
"View"
, {
"ref"
:
ref
,
"height"
:
40
}, [])
rec
.
mount
(
Element
(
"View"
, {}, [
child
]))
rec
.
set_viewport_size
(
200.0
,
100.0
)
assert
ref
.
_pn_frame
is
not
None
_x
,
_y
,
w
,
h
=
ref
.
_pn_frame
assert
(
w
,
h
)
==
(
200.0
,
40.0
)
Back
|
FazBrowse Home
|
New Git URL