;; AUTO-GENERATED from spec/tests/hyperscript-upstream-tests.json
;; DO NOT EDIT — regenerate with:
;;   python3 tests/playwright/generate-sx-tests.py --emit-pages

(defcomp ()
  (~docs/page :title "Hyperscript: fetch (23 tests — 17 runnable)"
    (p :style "color:#57534e;margin-bottom:1rem" "Live cards for the upstream fetch tests. 17 of 23 are reproducible in-browser; the remainder show their source for reference.")
    (p :style "color:#78716c;font-size:0.875rem;margin-bottom:1rem"
      "Theme: " (a :href "/sx/(applications.(hyperscript.gallery-events))"
        :style "color:#7c3aed" "events"))
    (div :style "display:flex;flex-direction:column"
      (~hyperscript/hs-test-card
  :name "can do a simple fetch"
  :html "<div _='on click fetch \"/test\" then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch \\\"/test\\\" then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple fetch w/ a naked URL"
  :html "<div _='on click fetch /test then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple fetch w/ html"
  :html "<div _='on click fetch /test as html then log it then put it into my.innerHTML put its childElementCount into my @data-count'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"[object DocumentFragment]\") && div.dataset.count.should.equal(\"1\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test as html then log it then put it into my.innerHTML put its childElementCount into my @data-count\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"[object DocumentFragment]\")
    ;; SKIP check: skip div.dataset.count.should.equal(\"1\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple fetch w/ json"
  :html "<div _='on click fetch /test as json then get result as JSON then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal('{\"foo\":1}')"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test as json then get result as JSON then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"'{\\\"foo\\\":1}'\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple fetch w/ json using Object syntax"
  :html "<div _='on click fetch /test as Object then get result as JSON then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal('{\"foo\":1}')"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test as Object then get result as JSON then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"'{\\\"foo\\\":1}'\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple fetch w/ json using Object syntax and an 'an' prefix"
  :html "<div _='on click fetch /test as an Object then get result as JSON then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal('{\"foo\":1}')"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test as an Object then get result as JSON then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"'{\\\"foo\\\":1}'\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple fetch with a response object"
  :html "<div _='on click fetch /test as response then if its.ok put \"yep\" into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yep\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test as response then if its.ok put \\\"yep\\\" into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yep\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple fetch w/ a custom conversion"
  :html "<div _='on click fetch /test as Number then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"1.2\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test as Number then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"1.2\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple post"
  :html "<div _='on click fetch /test {method:\"POST\"} then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test {method:\\\"POST\\\"} then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple post alt syntax without curlies"
  :html "<div _='on click fetch /test with method:\"POST\" then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test with method:\\\"POST\\\" then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple post alt syntax w/ curlies"
  :html "<div _='on click fetch /test with {method:\"POST\"} then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test with {method:\\\"POST\\\"} then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "can put response conversion after with"
  :html "<div _='on click fetch /test with {method:\"POST\"} as text then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test with {method:\\\"POST\\\"} as text then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "can put response conversion before with"
  :html "<div _='on click fetch /test as text with {method:\"POST\"} then put it into my.innerHTML'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test as text with {method:\\\"POST\\\"} then put it into my.innerHTML\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "triggers an event just before fetching"
  :html "<div _='on click fetch \"/test\" then put it into my.innerHTML end'></div>"
  :action "div.click()"
  :check "div.classList.contains(\"foo-set\").should.equal(false) && div.classList.contains(\"foo-set\").should.equal(true) && div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch \\\"/test\\\" then put it into my.innerHTML end\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert (dom-has-class? _el-div \"foo-set\"))
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "submits the fetch parameters to the event handler"
  :html "<div _='on click fetch \"/test\" {headers: {\"X-CustomHeader\": \"foo\"}} then put it into my.innerHTML end'></div>"
  :action "div.click()"
  :check "event.detail.headers.should.have.property('X-CustomHeader', 'foo') && div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch \\\"/test\\\" {headers: {\\\"X-CustomHeader\\\": \\\"foo\\\"}} then put it into my.innerHTML end\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    ;; SKIP check: skip event.detail.headers.should.have.property('X-CustomHeader', 
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "allows the event handler to change the fetch parameters"
  :html "<div _='on click fetch \"/test\" then put it into my.innerHTML end'></div>"
  :action "div.click()"
  :check "arguments[1].should.have.property('headers') && arguments[1].headers.should.have.property('X-CustomHeader', 'foo') && div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch \\\"/test\\\" then put it into my.innerHTML end\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    ;; SKIP check: skip arguments[1].should.have.property('headers')
    ;; SKIP check: skip arguments[1].headers.should.have.property('X-CustomHeader', 
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "can catch an error that occurs when using fetch"
  :html "<div _='on click fetch /test catch e log e put \"yay\" into me'></div>"
  :action "div.click()"
  :check "div.innerHTML.should.equal(\"yay\")"
  :run-src "(fn (sandbox)
  (let ((_el-div (dom-create-element \"div\")))
    (dom-set-attr _el-div \"_\" \"on click fetch /test catch e log e put \\\"yay\\\" into me\")
    (dom-append sandbox _el-div)
    (hs-activate! _el-div)
    (dom-dispatch _el-div \"click\" nil)
    (assert= (dom-inner-html _el-div) \"yay\")
  ))")
(~hyperscript/hs-test-card
  :name "can do a simple fetch w/ json using JSON syntax"
  :html "<div _='on click fetch /test as JSON then get result as JSONString then put it into my.innerHTML'></div>"
  :action "find('div').dispatchEvent('click')"
  :check "toHaveText('{\"foo\":1}')"
  :run-src "(fn (sandbox) (error \"not yet runnable in gallery — see test suite\"))")
(~hyperscript/hs-test-card
  :name "throws on non-2xx response by default"
  :html "<div _='on click fetch /test catch e put \\\"caught\\\" into me'></div>"
  :action "find('div').dispatchEvent('click')"
  :check "toHaveText(\"caught\")"
  :run-src "(fn (sandbox) (error \"not yet runnable in gallery — see test suite\"))")
(~hyperscript/hs-test-card
  :name "do not throw passes through 404 response"
  :html "<div _='on click fetch /test do not throw then put it into me'></div>"
  :action "find('div').dispatchEvent('click')"
  :check "toHaveText(\"the body\")"
  :run-src "(fn (sandbox) (error \"not yet runnable in gallery — see test suite\"))")
(~hyperscript/hs-test-card
  :name "don't throw passes through 404 response"
  :html "<div _=\"on click fetch /test don\\'t throw then put it into me\"></div>"
  :action "find('div').dispatchEvent('click')"
  :check "toHaveText(\"the body\")"
  :run-src "(fn (sandbox) (error \"not yet runnable in gallery — see test suite\"))")
(~hyperscript/hs-test-card
  :name "as response does not throw on 404"
  :html "<div _='on click fetch /test as response then put it.status into me'></div>"
  :action "find('div').dispatchEvent('click')"
  :check "toHaveText(\"404\")"
  :run-src "(fn (sandbox) (error \"not yet runnable in gallery — see test suite\"))")
(~hyperscript/hs-test-card
  :name "Response can be converted to JSON via as JSON"
  :html "<div _=\\\"on click fetch /test as Response then put (it as JSON).name into me\\\"></div>"
  :action "find('div').dispatchEvent('click')"
  :check "toHaveText(\"Joe\")"
  :run-src "(fn (sandbox) (error \"not yet runnable in gallery — see test suite\"))"))))
