1
//! A double click on a create form sends one request and stores one record.
2

            
3
use anyhow::{Result, ensure};
4

            
5
use super::harness::{Harness, with_harness};
6
use super::page::{login, open, set_text, wait_for};
7
use super::rows::{Split, fill_splits};
8
use crate::common::tenant::tenant_connection;
9

            
10
2
async fn stored_transactions(h: &Harness) -> Result<i64> {
11
2
    let mut conn = tenant_connection(h.tenant.id).await;
12
2
    Ok(sqlx::query_scalar("SELECT count(*) FROM transactions")
13
2
        .fetch_one(&mut *conn)
14
2
        .await?)
15
2
}
16

            
17
3
async fn double_click(h: &Harness) -> Result<()> {
18
3
    h.driver
19
3
        .run(
20
3
            "window.__posts = 0;\
21
3
             const send = XMLHttpRequest.prototype.send;\
22
3
             XMLHttpRequest.prototype.send = function (...args) { window.__posts += 1; return send.apply(this, args); };\
23
3
             const button = document.querySelector('form.nomi-form button[type=\"submit\"]');\
24
3
             button.click(); button.click();",
25
3
            vec![],
26
3
        )
27
3
        .await
28
3
        .map(drop)
29
3
}
30

            
31
1
async fn creates_once(h: &Harness) -> Result<()> {
32
1
    login(h).await?;
33
1
    open(h, "/transaction/new").await?;
34
1
    fill_splits(h, &[Split::plain("Suica", "JR", "199")]).await?;
35
1
    let before = stored_transactions(h).await?;
36
1
    double_click(h).await?;
37
1
    let reply = wait_for(
38
1
        &h.driver,
39
1
        "const box = document.querySelector('form.nomi-form .result-box');\
40
1
         return box && box.textContent.includes('created with ID') ? {posts: window.__posts} : null;",
41
1
        vec![],
42
1
        "the create reply",
43
1
    )
44
1
    .await?;
45
1
    let posts = &reply["posts"];
46
1
    ensure!(posts == 1, "requests sent: {posts}");
47
1
    let stored = stored_transactions(h).await? - before;
48
1
    ensure!(stored == 1, "transactions stored: {stored}");
49
1
    Ok(())
50
1
}
51

            
52
#[supp_macro::shared_runtime_test]
53
1
async fn a_double_click_on_create_stores_one_transaction() -> Result<()> {
54
1
    with_harness(|h| Box::pin(creates_once(h))).await
55
}
56

            
57
1
async fn creates_tag_once(h: &Harness) -> Result<()> {
58
1
    login(h).await?;
59
1
    open(h, "/tag/create").await?;
60
1
    set_text(h, "name", "double").await?;
61
1
    set_text(h, "value", "click").await?;
62
1
    double_click(h).await?;
63
1
    let reply = wait_for(
64
1
        &h.driver,
65
1
        "const box = document.querySelector('form.nomi-form .result-box');\
66
1
         return box && box.textContent.includes('New tag id') ? {posts: window.__posts} : null;",
67
1
        vec![],
68
1
        "the tag create reply",
69
1
    )
70
1
    .await?;
71
1
    let posts = &reply["posts"];
72
1
    ensure!(posts == 1, "requests sent: {posts}");
73
1
    Ok(())
74
1
}
75

            
76
#[supp_macro::shared_runtime_test]
77
1
async fn a_double_click_on_tag_create_sends_one_request() -> Result<()> {
78
1
    with_harness(|h| Box::pin(creates_tag_once(h))).await
79
}
80

            
81
2
async fn stored_artifacts(h: &Harness) -> Result<i64> {
82
2
    let mut conn = tenant_connection(h.tenant.id).await;
83
2
    Ok(sqlx::query_scalar("SELECT count(*) FROM artifacts")
84
2
        .fetch_one(&mut *conn)
85
2
        .await?)
86
2
}
87

            
88
1
async fn creates_script_once(h: &Harness) -> Result<()> {
89
1
    login(h).await?;
90
1
    open(h, "/script/create").await?;
91
1
    let before = stored_artifacts(h).await?;
92
1
    set_text(h, "source", "(+ 1 2)").await?;
93
1
    double_click(h).await?;
94
1
    let reply = wait_for(
95
1
        &h.driver,
96
1
        "const box = document.querySelector('form.nomi-form .result-box');\
97
1
         return box && box.textContent.includes('Script created') ? {posts: window.__posts} : null;",
98
1
        vec![],
99
1
        "the script create reply",
100
1
    )
101
1
    .await?;
102
1
    let posts = &reply["posts"];
103
1
    ensure!(posts == 1, "requests sent: {posts}");
104
1
    let stored = stored_artifacts(h).await? - before;
105
1
    ensure!(stored == 1, "scripts stored: {stored}");
106
1
    Ok(())
107
1
}
108

            
109
#[supp_macro::shared_runtime_test]
110
1
async fn a_double_click_on_script_create_stores_one_script() -> Result<()> {
111
1
    with_harness(|h| Box::pin(creates_script_once(h))).await
112
}