summaryrefslogtreecommitdiffstatshomepage
path: root/test/attributes/hx-confirm.js
blob: 31e915d143c583bf62b40402e71d0993d9094dcb (plain) (blame)
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
112
113
114
115
116
117
118
119
120
121
122
123
describe('hx-confirm attribute', function() {
  var confirm
  beforeEach(function() {
    this.server = makeServer()
    confirm = sinon.stub(window, 'confirm')
    clearWorkArea()
  })
  afterEach(function() {
    this.server.restore()
    confirm.restore()
    clearWorkArea()
  })

  it('prompts using window.confirm when hx-confirm is set', function() {
    this.server.respondWith('GET', '/test', 'Clicked!')
    confirm.returns(true)
    var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>')
    btn.click()
    confirm.calledOnce.should.equal(true)
    this.server.respond()
    btn.innerHTML.should.equal('Clicked!')
  })

  it('stops the request if confirm is cancelled', function() {
    this.server.respondWith('GET', '/test', 'Clicked!')
    confirm.returns(false)
    var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>')
    btn.click()
    confirm.calledOnce.should.equal(true)
    this.server.respond()
    btn.innerHTML.should.equal('Click Me!')
  })

  it('uses the value of hx-confirm as the prompt', function() {
    this.server.respondWith('GET', '/test', 'Clicked!')
    confirm.returns(false)
    var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>')
    btn.click()
    confirm.firstCall.args[0].should.equal('Sure?')
    this.server.respond()
    btn.innerHTML.should.equal('Click Me!')
  })

  it('should prompt when htmx:confirm handler calls issueRequest', function() {
    try {
      var btn = make('<button hx-get="/test" hx-confirm="Surely?">Click Me!</button>')
      var handler = htmx.on('htmx:confirm', function(evt) {
        evt.preventDefault()
        evt.detail.issueRequest()
      })
      btn.click()
      confirm.calledOnce.should.equal(true)
    } finally {
      htmx.off('htmx:confirm', handler)
    }
  })

  it('should include the question in htmx:confirm event', function() {
    var stub = sinon.stub()
    try {
      var btn = make('<button hx-get="/test" hx-confirm="Surely?">Click Me!</button>')
      var handler = htmx.on('htmx:confirm', stub)
      btn.click()
      stub.calledOnce.should.equal(true)
      stub.firstCall.args[0].detail.should.have.property('question', 'Surely?')
    } finally {
      htmx.off('htmx:confirm', handler)
    }
  })

  it('should allow skipping built-in window.confirm when using issueRequest', function() {
    this.server.respondWith('GET', '/test', 'Clicked!')
    try {
      var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>')
      var handler = htmx.on('htmx:confirm', function(evt) {
        evt.detail.question.should.equal('Sure?')
        evt.preventDefault()
        evt.detail.issueRequest(true)
      })
      btn.click()
      confirm.called.should.equal(false)
      this.server.respond()
      btn.innerHTML.should.equal('Clicked!')
    } finally {
      htmx.off('htmx:confirm', handler)
    }
  })
  it('should allow skipping built-in window.confirm when using issueRequest', function() {
    this.server.respondWith('GET', '/test', 'Clicked!')
    try {
      var btn = make('<button hx-get="/test" hx-confirm="Sure?">Click Me!</button>')
      var handler = htmx.on('htmx:confirm', function(evt) {
        evt.detail.question.should.equal('Sure?')
        evt.preventDefault()
        evt.detail.issueRequest(true)
      })
      btn.click()
      confirm.called.should.equal(false)
      this.server.respond()
      btn.innerHTML.should.equal('Clicked!')
    } finally {
      htmx.off('htmx:confirm', handler)
    }
  })

  it('should allow htmx:confirm even when no hx-confirm is set', function() {
    this.server.respondWith('GET', '/test', 'Clicked!')
    try {
      var btn = make('<button hx-get="/test">Click Me!</button>')
      var handler = htmx.on('htmx:confirm', function(evt) {
        evt.detail.should.have.property('question', null)
        evt.preventDefault()
        evt.detail.issueRequest()
      })
      btn.click()
      confirm.called.should.equal(false) // no hx-confirm means no window.confirm
      this.server.respond()
      btn.innerHTML.should.equal('Clicked!')
    } finally {
      htmx.off('htmx:confirm', handler)
    }
  })
})