summaryrefslogtreecommitdiffstatshomepage
path: root/test/attributes/hx-get.js
blob: f9fdc5c84e51fc28729ff907996c850110619a4d (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
124
125
126
describe('hx-get attribute', function() {
  beforeEach(function() {
    this.server = makeServer()
    clearWorkArea()
  })
  afterEach(function() {
    this.server.restore()
    clearWorkArea()
  })

  it('issues a GET request on click and swaps content', function() {
    this.server.respondWith('GET', '/test', 'Clicked!')

    var btn = make('<button hx-get="/test">Click Me!</button>')
    btn.click()
    this.server.respond()
    btn.innerHTML.should.equal('Clicked!')
  })

  it('GET does not include surrounding data by default', function() {
    this.server.respondWith('GET', '/test', function(xhr) {
      should.equal(getParameters(xhr).i1, undefined)
      xhr.respond(200, {}, 'Clicked!')
    })
    make('<form><input name="i1" value="value"/><button id="b1" hx-get="/test">Click Me!</button></form>')
    var btn = byId('b1')
    btn.click()
    this.server.respond()
    btn.innerHTML.should.equal('Clicked!')
  })

  it('GET on form includes its own data by default', function() {
    this.server.respondWith('GET', /\/test.*/, function(xhr) {
      getParameters(xhr).i1.should.equal('value')
      xhr.respond(200, {}, 'Clicked!')
    })
    var form = make('<form hx-trigger="click" hx-get="/test"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>')
    form.click()
    this.server.respond()
    form.innerHTML.should.equal('Clicked!')
  })

  it('GET on form with existing parameters works properly', function() {
    this.server.respondWith('GET', /\/test.*/, function(xhr) {
      getParameters(xhr).foo.should.equal('bar')
      getParameters(xhr).i1.should.equal('value')
      xhr.respond(200, {}, 'Clicked!')
    })
    var form = make('<form hx-trigger="click" hx-get="/test?foo=bar"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>')
    form.click()
    this.server.respond()
    form.innerHTML.should.equal('Clicked!')
  })

  it('GET on form with anchor works properly', function() {
    this.server.respondWith('GET', /\/test.*/, function(xhr) {
      getParameters(xhr).foo.should.equal('bar')
      getParameters(xhr).i1.should.equal('value')
      xhr.respond(200, {}, 'Clicked!')
    })
    var form = make('<form hx-trigger="click" hx-get="/test?foo=bar#foo"><input name="i1" value="value"/><button id="b1">Click Me!</button></form>')
    form.click()
    this.server.respond()
    form.innerHTML.should.equal('Clicked!')
  })

  it('issues a GET request on click and swaps content w/ data-* prefix', function() {
    this.server.respondWith('GET', '/test', 'Clicked!')

    var btn = make('<button data-hx-get="/test">Click Me!</button>')
    btn.click()
    this.server.respond()
    btn.innerHTML.should.equal('Clicked!')
  })

  it('does not include a cache-busting parameter when not enabled', function() {
    this.server.respondWith('GET', /\/test.*/, function(xhr) {
      should.not.exist(getParameters(xhr)['org.htmx.cache-buster'])
      xhr.respond(200, {}, 'Clicked!')
    })

    try {
      htmx.config.getCacheBusterParam = false
      var btn = make('<button hx-get="/test">Click Me!</button>')
      btn.click()
      this.server.respond()
      btn.innerHTML.should.equal('Clicked!')
    } finally {
      htmx.config.getCacheBusterParam = false
    }
  })

  it('includes a cache-busting parameter when enabled w/ value "true" if no id on target', function() {
    this.server.respondWith('GET', /\/test.*/, function(xhr) {
      getParameters(xhr)['org.htmx.cache-buster'].should.equal('true')
      xhr.respond(200, {}, 'Clicked!')
    })

    try {
      htmx.config.getCacheBusterParam = true
      var btn = make('<button hx-get="/test">Click Me!</button>')
      btn.click()
      this.server.respond()
      btn.innerHTML.should.equal('Clicked!')
    } finally {
      htmx.config.getCacheBusterParam = false
    }
  })

  it('includes a cache-busting parameter when enabled w/ the id of the target if there is one', function() {
    this.server.respondWith('GET', /\/test.*/, function(xhr) {
      getParameters(xhr)['org.htmx.cache-buster'].should.equal('foo')
      xhr.respond(200, {}, 'Clicked!')
    })

    try {
      htmx.config.getCacheBusterParam = true
      var btn = make('<button hx-get="/test" id="foo">Click Me!</button>')
      btn.click()
      this.server.respond()
      btn.innerHTML.should.equal('Clicked!')
    } finally {
      htmx.config.getCacheBusterParam = false
    }
  })
})