1
// -- -*- mode: rust -*-
2
//! Tests for fixed behaviors in the WASM frontend
3

            
4
use wasm_bindgen::JsCast;
5
use wasm_bindgen_test::*;
6

            
7
wasm_bindgen_test_configure!(run_in_browser);
8

            
9
/// Test that change events created with `EventInit` bubble up to parent elements.
10
/// This is critical for the currency mismatch detection which uses a document-level
11
/// event listener to catch change events from commodity input fields.
12
#[wasm_bindgen_test]
13
fn test_change_event_bubbles_to_document() {
14
    let window = web_sys::window().unwrap();
15
    let document = window.document().unwrap();
16

            
17
    // Create a container and input
18
    let container = document.create_element("div").unwrap();
19
    let input = document
20
        .create_element("input")
21
        .unwrap()
22
        .dyn_into::<web_sys::HtmlInputElement>()
23
        .unwrap();
24
    container.append_child(&input).unwrap();
25
    document.body().unwrap().append_child(&container).unwrap();
26

            
27
    // Track if the event bubbled to the container
28
    let bubbled = std::rc::Rc::new(std::cell::Cell::new(false));
29
    let bubbled_clone = bubbled.clone();
30

            
31
    let callback = wasm_bindgen::closure::Closure::wrap(Box::new(move |_: web_sys::Event| {
32
        bubbled_clone.set(true);
33
    }) as Box<dyn FnMut(_)>);
34

            
35
    container
36
        .add_event_listener_with_callback("change", callback.as_ref().unchecked_ref())
37
        .unwrap();
38
    callback.forget();
39

            
40
    // Create a bubbling change event (like our autocomplete does)
41
    let init = web_sys::EventInit::new();
42
    init.set_bubbles(true);
43
    let event = web_sys::Event::new_with_event_init_dict("change", &init).unwrap();
44
    input.dispatch_event(&event).unwrap();
45

            
46
    assert!(
47
        bubbled.get(),
48
        "Change event should bubble to parent container"
49
    );
50

            
51
    // Cleanup
52
    container.remove();
53
}
54

            
55
/// Test that non-bubbling events do NOT reach parent elements.
56
/// This demonstrates why we needed to add bubbles: true.
57
#[wasm_bindgen_test]
58
fn test_non_bubbling_event_stays_local() {
59
    let window = web_sys::window().unwrap();
60
    let document = window.document().unwrap();
61

            
62
    let container = document.create_element("div").unwrap();
63
    let input = document
64
        .create_element("input")
65
        .unwrap()
66
        .dyn_into::<web_sys::HtmlInputElement>()
67
        .unwrap();
68
    container.append_child(&input).unwrap();
69
    document.body().unwrap().append_child(&container).unwrap();
70

            
71
    let bubbled = std::rc::Rc::new(std::cell::Cell::new(false));
72
    let bubbled_clone = bubbled.clone();
73

            
74
    let callback = wasm_bindgen::closure::Closure::wrap(Box::new(move |_: web_sys::Event| {
75
        bubbled_clone.set(true);
76
    }) as Box<dyn FnMut(_)>);
77

            
78
    container
79
        .add_event_listener_with_callback("change", callback.as_ref().unchecked_ref())
80
        .unwrap();
81
    callback.forget();
82

            
83
    // Create a NON-bubbling event (default behavior without EventInit)
84
    let event = web_sys::Event::new("change").unwrap();
85
    input.dispatch_event(&event).unwrap();
86

            
87
    assert!(
88
        !bubbled.get(),
89
        "Non-bubbling event should NOT reach parent container"
90
    );
91

            
92
    container.remove();
93
}
94

            
95
/// Test that mousedown with preventDefault stops blur from happening.
96
/// This is critical for mobile Safari autocomplete selection.
97
#[wasm_bindgen_test]
98
fn test_mousedown_prevent_default_stops_blur() {
99
    let window = web_sys::window().unwrap();
100
    let document = window.document().unwrap();
101

            
102
    let input = document
103
        .create_element("input")
104
        .unwrap()
105
        .dyn_into::<web_sys::HtmlInputElement>()
106
        .unwrap();
107
    let dropdown = document.create_element("div").unwrap();
108

            
109
    document.body().unwrap().append_child(&input).unwrap();
110
    document.body().unwrap().append_child(&dropdown).unwrap();
111

            
112
    // Add mousedown handler that prevents default (like our autocomplete)
113
    let mousedown_callback =
114
        wasm_bindgen::closure::Closure::wrap(Box::new(move |e: web_sys::MouseEvent| {
115
            e.prevent_default();
116
        }) as Box<dyn FnMut(_)>);
117

            
118
    dropdown
119
        .add_event_listener_with_callback("mousedown", mousedown_callback.as_ref().unchecked_ref())
120
        .unwrap();
121
    mousedown_callback.forget();
122

            
123
    // Focus the input
124
    input.focus().unwrap();
125

            
126
    // The mousedown with preventDefault should not cause blur
127
    // (In a real browser, clicking the dropdown would blur the input without this)
128
    let init = web_sys::MouseEventInit::new();
129
    init.set_bubbles(true);
130
    init.set_cancelable(true);
131
    let mousedown =
132
        web_sys::MouseEvent::new_with_mouse_event_init_dict("mousedown", &init).unwrap();
133
    let default_prevented = !dropdown.dispatch_event(&mousedown).unwrap();
134

            
135
    assert!(
136
        default_prevented,
137
        "mousedown preventDefault should be called"
138
    );
139

            
140
    input.remove();
141
    dropdown.remove();
142
}
143

            
144
/// Test that input events also bubble (used for autocomplete filtering).
145
#[wasm_bindgen_test]
146
fn test_input_event_bubbles() {
147
    let window = web_sys::window().unwrap();
148
    let document = window.document().unwrap();
149

            
150
    let container = document.create_element("div").unwrap();
151
    let input = document
152
        .create_element("input")
153
        .unwrap()
154
        .dyn_into::<web_sys::HtmlInputElement>()
155
        .unwrap();
156
    container.append_child(&input).unwrap();
157
    document.body().unwrap().append_child(&container).unwrap();
158

            
159
    let bubbled = std::rc::Rc::new(std::cell::Cell::new(false));
160
    let bubbled_clone = bubbled.clone();
161

            
162
    let callback = wasm_bindgen::closure::Closure::wrap(Box::new(move |_: web_sys::Event| {
163
        bubbled_clone.set(true);
164
    }) as Box<dyn FnMut(_)>);
165

            
166
    container
167
        .add_event_listener_with_callback("input", callback.as_ref().unchecked_ref())
168
        .unwrap();
169
    callback.forget();
170

            
171
    let init = web_sys::EventInit::new();
172
    init.set_bubbles(true);
173
    let event = web_sys::Event::new_with_event_init_dict("input", &init).unwrap();
174
    input.dispatch_event(&event).unwrap();
175

            
176
    assert!(bubbled.get(), "Input event should bubble to parent");
177

            
178
    container.remove();
179
}
180

            
181
/// Test that `init_all()` marks a tag-name autocomplete wrapper as initialized
182
/// when `data-display-input` points to a valid input element.
183
#[wasm_bindgen_test]
184
fn test_autocomplete_init_marks_wrapper_initialized() {
185
    let document = web_sys::window().unwrap().document().unwrap();
186

            
187
    let wrapper = document.create_element("div").unwrap();
188
    wrapper
189
        .set_attribute("data-autocomplete", "tag-name")
190
        .unwrap();
191
    wrapper
192
        .set_attribute(
193
            "data-fetch-url",
194
            "/api/forms/suggestions/transaction-tag-names",
195
        )
196
        .unwrap();
197
    wrapper
198
        .set_attribute("data-display-input", "test-ac-input")
199
        .unwrap();
200

            
201
    let input = document.create_element("input").unwrap();
202
    input.set_id("test-ac-input");
203
    input.set_attribute("type", "text").unwrap();
204
    wrapper.append_child(&input).unwrap();
205

            
206
    let results = document.create_element("div").unwrap();
207
    results.set_class_name("autocomplete-results");
208
    wrapper.append_child(&results).unwrap();
209

            
210
    document.body().unwrap().append_child(&wrapper).unwrap();
211

            
212
    nomisync_frontend::autocomplete::init_all();
213

            
214
    assert_eq!(
215
        wrapper.get_attribute("data-initialized").as_deref(),
216
        Some("true"),
217
        "Wrapper with valid data-display-input should be marked initialized"
218
    );
219

            
220
    wrapper.remove();
221
}
222

            
223
/// Test that `init_all()` still marks a wrapper as initialized even when
224
/// data-display-input is missing (attach fails but `init_all` proceeds).
225
#[wasm_bindgen_test]
226
fn test_autocomplete_init_marks_wrapper_without_display_input() {
227
    let document = web_sys::window().unwrap().document().unwrap();
228

            
229
    let wrapper = document.create_element("div").unwrap();
230
    wrapper
231
        .set_attribute("data-autocomplete", "tag-name")
232
        .unwrap();
233
    wrapper
234
        .set_attribute(
235
            "data-fetch-url",
236
            "/api/forms/suggestions/transaction-tag-names",
237
        )
238
        .unwrap();
239
    // deliberately no data-display-input
240

            
241
    let input = document.create_element("input").unwrap();
242
    input.set_attribute("type", "text").unwrap();
243
    wrapper.append_child(&input).unwrap();
244

            
245
    let results = document.create_element("div").unwrap();
246
    results.set_class_name("autocomplete-results");
247
    wrapper.append_child(&results).unwrap();
248

            
249
    document.body().unwrap().append_child(&wrapper).unwrap();
250

            
251
    nomisync_frontend::autocomplete::init_all();
252

            
253
    assert_eq!(
254
        wrapper.get_attribute("data-initialized").as_deref(),
255
        Some("true"),
256
        "Wrapper without data-display-input is still marked initialized (attach fails silently)"
257
    );
258

            
259
    wrapper.remove();
260
}