hsx/tests/element.lisp

65 lines
2.3 KiB
Common Lisp
Raw Normal View History

2024-05-27 07:54:46 +00:00
(defpackage #:hsx-test/element
(:use #:cl
#:fiveam
#:hsx/element))
(in-package #:hsx-test/element)
2024-05-25 03:00:39 +00:00
2024-05-26 03:26:09 +00:00
(def-suite element-test)
(in-suite element-test)
(test element-class
(is (typep (create-element :div nil nil) 'tag))
(is (typep (create-element :html nil nil) 'html-tag))
(is (typep (create-element :<> nil nil) 'fragment))
(is (typep (create-element (lambda ()) nil nil) 'component))
(signals error (create-element "div" nil nil)))
2024-05-26 03:26:09 +00:00
(test flatten-children
(let* ((elm (create-element :p
2024-05-25 10:38:54 +00:00
nil
(list "a"
nil
(list "b" (list nil "c"))
(cons "d" "e")))))
2024-05-29 02:47:48 +00:00
(is (equal (list "a" "b" "c" "d" "e") (element-children elm)))))
2024-05-25 10:38:54 +00:00
(defun comp1 (&key prop children)
(create-element :div
2024-05-26 03:26:09 +00:00
nil
(list prop
children)))
2024-05-26 03:26:09 +00:00
2024-05-28 11:48:39 +00:00
(test component-accepting-keyword-args
(let ((elm (expand-component (create-element #'comp1
'(:prop "value")
(list "child")))))
(is (eq :div (element-type elm)))
(is (equal (list "value" "child") (element-children elm)))))
2024-05-26 03:26:09 +00:00
(defun comp2 (&rest props)
(create-element :div
2024-05-26 03:26:09 +00:00
nil
(list (getf props :prop)
(getf props :children))))
2024-05-26 03:26:09 +00:00
2024-05-28 11:48:39 +00:00
(test component-accepting-property-list
(let ((elm (expand-component (create-element #'comp2
'(:prop "value")
(list "child")))))
(is (eq :div (element-type elm)))
(is (equal (list "value" "child") (element-children elm)))))
2024-05-26 03:26:09 +00:00
(defun comp3 (&rest props &key prop children &allow-other-keys)
(create-element :div
2024-05-26 03:26:09 +00:00
nil
(list prop
children
(getf props :other-key))))
2024-05-26 03:26:09 +00:00
2024-05-28 11:48:39 +00:00
(test component-accepting-keyword-args-and-property-list
(let ((elm (expand-component (create-element #'comp3
'(:prop "value" :other-key "other")
(list "child")))))
(is (eq :div (element-type elm)))
(is (equal (list "value" "child" "other") (element-children elm)))))