14
14
* limitations under the License.
15
15
*/
16
16
17
- import { describe , it , expect , vi , beforeEach } from "vitest" ;
18
- import { render , screen } from "@testing-library/react" ;
17
+ import { describe , it , expect , vi , beforeEach , afterEach } from "vitest" ;
18
+ import { render , fireEvent , cleanup } from "@testing-library/react" ;
19
19
import { Policies } from "./policies" ;
20
20
import { FirebaseUIProvider } from "~/context" ;
21
21
import { createMockUI } from "~/tests/utils" ;
@@ -25,8 +25,12 @@ describe("<Policies />", () => {
25
25
vi . clearAllMocks ( ) ;
26
26
} ) ;
27
27
28
- it ( "renders component with terms and privacy links" , ( ) => {
29
- render (
28
+ afterEach ( ( ) => {
29
+ cleanup ( ) ;
30
+ } ) ;
31
+
32
+ it ( "renders component with terms and privacy links using anchor tags" , ( ) => {
33
+ const { container } = render (
30
34
< FirebaseUIProvider
31
35
ui = { createMockUI ( ) }
32
36
policies = { {
@@ -39,27 +43,117 @@ describe("<Policies />", () => {
39
43
) ;
40
44
41
45
// Check that the text and links are rendered
42
- expect ( screen . getByText ( / B y c o n t i n u i n g , y o u a g r e e t o o u r / ) ) . toBeInTheDocument ( ) ;
46
+ expect ( container . querySelector ( ".fui-policies" ) ) . toBeInTheDocument ( ) ;
43
47
44
- const tosLink = screen . getByText ( "Terms of Service" ) ;
48
+ const tosLink = container . querySelector ( 'a[href="https://example.com/terms"]' ) ;
45
49
expect ( tosLink ) . toBeInTheDocument ( ) ;
46
- expect ( tosLink . tagName ) . toBe ( "A" ) ;
50
+ expect ( tosLink ? .tagName ) . toBe ( "A" ) ;
47
51
expect ( tosLink ) . toHaveAttribute ( "target" , "_blank" ) ;
48
52
expect ( tosLink ) . toHaveAttribute ( "rel" , "noopener noreferrer" ) ;
53
+ expect ( tosLink ) . toHaveTextContent ( "Terms of Service" ) ;
49
54
50
- const privacyLink = screen . getByText ( "Privacy Policy" ) ;
55
+ const privacyLink = container . querySelector ( 'a[href="https://example.com/privacy"]' ) ;
51
56
expect ( privacyLink ) . toBeInTheDocument ( ) ;
52
- expect ( privacyLink . tagName ) . toBe ( "A" ) ;
57
+ expect ( privacyLink ? .tagName ) . toBe ( "A" ) ;
53
58
expect ( privacyLink ) . toHaveAttribute ( "target" , "_blank" ) ;
54
59
expect ( privacyLink ) . toHaveAttribute ( "rel" , "noopener noreferrer" ) ;
60
+ expect ( privacyLink ) . toHaveTextContent ( "Privacy Policy" ) ;
61
+ } ) ;
62
+
63
+ it ( "renders component with custom navigation handler using buttons" , ( ) => {
64
+ const mockNavigate = vi . fn ( ) ;
65
+ const { container } = render (
66
+ < FirebaseUIProvider
67
+ ui = { createMockUI ( ) }
68
+ policies = { {
69
+ termsOfServiceUrl : "https://example.com/terms" ,
70
+ privacyPolicyUrl : "https://example.com/privacy" ,
71
+ onNavigate : mockNavigate ,
72
+ } }
73
+ >
74
+ < Policies />
75
+ </ FirebaseUIProvider >
76
+ ) ;
77
+
78
+ // Check that the text and buttons are rendered
79
+ expect ( container . querySelector ( ".fui-policies" ) ) . toBeInTheDocument ( ) ;
80
+
81
+ const tosButton = container . querySelector ( "button" ) ;
82
+ expect ( tosButton ) . toBeInTheDocument ( ) ;
83
+ expect ( tosButton ?. tagName ) . toBe ( "BUTTON" ) ;
84
+ expect ( tosButton ) . not . toHaveAttribute ( "href" ) ;
85
+ expect ( tosButton ) . not . toHaveAttribute ( "target" ) ;
86
+ expect ( tosButton ) . toHaveTextContent ( "Terms of Service" ) ;
87
+
88
+ const privacyButton = container . querySelectorAll ( "button" ) [ 1 ] ;
89
+ expect ( privacyButton ) . toBeInTheDocument ( ) ;
90
+ expect ( privacyButton ?. tagName ) . toBe ( "BUTTON" ) ;
91
+ expect ( privacyButton ) . not . toHaveAttribute ( "href" ) ;
92
+ expect ( privacyButton ) . not . toHaveAttribute ( "target" ) ;
93
+ expect ( privacyButton ) . toHaveTextContent ( "Privacy Policy" ) ;
94
+
95
+ fireEvent . click ( tosButton ! ) ;
96
+ expect ( mockNavigate ) . toHaveBeenCalledWith ( "https://example.com/terms" ) ;
97
+
98
+ fireEvent . click ( privacyButton ! ) ;
99
+ expect ( mockNavigate ) . toHaveBeenCalledWith ( "https://example.com/privacy" ) ;
100
+ } ) ;
101
+
102
+ it ( "handles URL objects correctly" , ( ) => {
103
+ const termsUrl = new URL ( "https://example.com/terms" ) ;
104
+ const privacyUrl = new URL ( "https://example.com/privacy" ) ;
105
+ const { container } = render (
106
+ < FirebaseUIProvider
107
+ ui = { createMockUI ( ) }
108
+ policies = { {
109
+ termsOfServiceUrl : termsUrl ,
110
+ privacyPolicyUrl : privacyUrl ,
111
+ } }
112
+ >
113
+ < Policies />
114
+ </ FirebaseUIProvider >
115
+ ) ;
116
+
117
+ const tosLink = container . querySelector ( 'a[href="https://example.com/terms"]' ) ;
118
+ expect ( tosLink ) . toHaveAttribute ( "href" , "https://example.com/terms" ) ;
119
+
120
+ const privacyLink = container . querySelector ( 'a[href="https://example.com/privacy"]' ) ;
121
+ expect ( privacyLink ) . toHaveAttribute ( "href" , "https://example.com/privacy" ) ;
55
122
} ) ;
56
123
57
- it ( "returns null when both tosUrl and privacyPolicyUrl are not provided" , ( ) => {
124
+ it ( "returns null when policies are not provided" , ( ) => {
58
125
const { container } = render (
59
126
< FirebaseUIProvider ui = { createMockUI ( ) } policies = { undefined } >
60
127
< Policies />
61
128
</ FirebaseUIProvider >
62
129
) ;
63
130
expect ( container ) . toBeEmptyDOMElement ( ) ;
64
131
} ) ;
132
+
133
+ it ( "handles custom navigation with URL objects" , ( ) => {
134
+ const mockNavigate = vi . fn ( ) ;
135
+ const termsUrl = new URL ( "https://example.com/terms" ) ;
136
+ const privacyUrl = new URL ( "https://example.com/privacy" ) ;
137
+ const { container } = render (
138
+ < FirebaseUIProvider
139
+ ui = { createMockUI ( ) }
140
+ policies = { {
141
+ termsOfServiceUrl : termsUrl ,
142
+ privacyPolicyUrl : privacyUrl ,
143
+ onNavigate : mockNavigate ,
144
+ } }
145
+ >
146
+ < Policies />
147
+ </ FirebaseUIProvider >
148
+ ) ;
149
+
150
+ const tosButton = container . querySelector ( "button" ) ;
151
+ const privacyButton = container . querySelectorAll ( "button" ) [ 1 ] ;
152
+
153
+ fireEvent . click ( tosButton ! ) ;
154
+ expect ( mockNavigate ) . toHaveBeenCalledWith ( termsUrl ) ;
155
+
156
+ fireEvent . click ( privacyButton ! ) ;
157
+ expect ( mockNavigate ) . toHaveBeenCalledWith ( privacyUrl ) ;
158
+ } ) ;
65
159
} ) ;
0 commit comments