-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from pitch-io/simplified-matchers
Simplify matcher setup and fix bug related to negated matchers
- Loading branch information
Showing
14 changed files
with
220 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
{:setup-ns cljest.internal-test-setup-ns} | ||
{:aliases ["test"] | ||
:setup-ns cljest.internal-test-setup-ns} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
(ns cljest.matchers) | ||
|
||
(defmacro defmatcher | ||
"A macro for defining a Jest matcher. Creates a function with metadata that will allow | ||
`cljest.core/is` to treat this symbol as a Jest matcher, rather than a regular symbol. | ||
This allows the compiler to generate simpler code, making one expect call for the matcher, | ||
rather than two (the `is` and the underlying matcher). | ||
When the function defined by `defmatcher` is called, it will throw as it is replaced when | ||
compiled in `is`." | ||
[sym matcher-name] | ||
`(defn ~(with-meta sym {:jest-matcher matcher-name}) [& _#] | ||
(throw (ex-info (str "You must call " ~(str sym) " inside of `cljest.core/is`.") {:matcher-name ~matcher-name})))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,32 @@ | ||
(ns cljest.matchers | ||
(:require [applied-science.js-interop :as j] | ||
[cljest.core])) | ||
|
||
(defn make-matcher | ||
"Most matchers accept either 0, 1, or 2 arguments. This handles those cases. | ||
Used for matchers that follow these patterns: | ||
``` | ||
expect(actual).matcherName() | ||
expect(actual).matcherName(expected) | ||
expect(actual).matcherName(expected, secondExpected) | ||
``` | ||
" | ||
([name actual] | ||
(make-matcher name actual js/undefined)) | ||
([name actual expected] | ||
(make-matcher name actual expected js/undefined)) | ||
([name actual expected second-expected] | ||
(if-not cljest.core/*inside-is?* | ||
(throw (ex-info (str "You must call " name " inside of `cljest.core/is`.") {})) | ||
(let [raw-expect-call (js/expect actual) | ||
expect-call (if cljest.core/*is-body-negated?* | ||
(j/get raw-expect-call :not) | ||
raw-expect-call)] | ||
(j/call expect-call name expected second-expected) | ||
|
||
;; So that `is` will pass. | ||
true)))) | ||
|
||
(defn make-optional-matcher | ||
"Some matchers, like `.toHaveAttribute`, optionally accept an argument (or second argument) to the matcher, | ||
and can't take js/undefined -- the argument must be unprovided. | ||
Used for matchers that follow these patterns: | ||
``` | ||
expect(actual).matcherName() | ||
expect(actual).matcherName(maybeValue) | ||
expect(actual).matcherName(value) | ||
expect(actual).matcherName(value, maybeExtraValue) | ||
```" | ||
([name actual maybe-value] | ||
(make-optional-matcher name actual maybe-value nil)) | ||
|
||
([name actual value maybe-extra-value] | ||
(if-not cljest.core/*inside-is?* | ||
(throw (ex-info (str "You must call " name " inside of `cljest.core/is`.") {})) | ||
(let [raw-expect-call (js/expect actual) | ||
expect-call (if cljest.core/*is-body-negated?* | ||
(j/get raw-expect-call :not) | ||
raw-expect-call)] | ||
(cond | ||
(nil? value) | ||
(j/call expect-call name) | ||
|
||
(nil? maybe-extra-value) | ||
(j/call expect-call name value) | ||
|
||
:else | ||
(j/call expect-call name value maybe-extra-value)) | ||
true)))) | ||
(:require-macros [cljest.matchers :refer [defmatcher]])) | ||
|
||
; jest.fn | ||
(defn called? [spy] (make-matcher "toHaveBeenCalled" spy)) | ||
(defn called-times? [spy n] (make-matcher "toHaveBeenCalledTimes" spy n)) | ||
(defn called-with? [spy & args] (make-matcher "customCalledWith" spy args)) | ||
(defmatcher called? "toHaveBeenCalled") | ||
(defmatcher called-times? "toHaveBeenCalledTimes") | ||
(defmatcher called-with? "customCalledWith") | ||
|
||
; jest-dom | ||
(defn disabled? [element] (make-matcher "toBeDisabled" element)) | ||
(defn enabled? [element] (make-matcher "toBeEnabled" element)) | ||
(defn empty-dom-element? [element] (make-matcher "toBeEmptyDOMElement" element)) | ||
(defn in-the-document? [element] (make-matcher "toBeInTheDocument" element)) | ||
(defn invalid? [element] (make-matcher "toBeInvalid" element)) | ||
(defn required? [element] (make-matcher "toBeRequired" element)) | ||
(defn valid? [element] (make-matcher "toBeValid" element)) | ||
(defn visible? [element] (make-matcher "toBeVisible" element)) | ||
(defn contains-element? [element descendent] (make-matcher "toContainElement" element descendent)) | ||
(defn contains-html? [expected actual] (make-matcher "toContainHTML" actual expected)) | ||
(defn has-attribute? [element attribute value] (make-matcher "toHaveAttribute" element attribute value)) | ||
(defn has-class? [element class & [options]] (make-optional-matcher "toHaveClass" element class options)) | ||
(defn has-focus? [element] (make-matcher "toHaveFocus" element)) | ||
(defn has-style? [element css] (make-matcher "toHaveStyle" element css)) | ||
(defn has-text-content? [element text] (make-matcher "toHaveTextContent" element text)) | ||
(defn has-value? [element value] (make-matcher "toHaveValue" element value)) | ||
(defn has-display-value? [element value] (make-matcher "toHaveDisplayValue" element value)) | ||
(defn checked? [element] (make-matcher "toBeChecked" element)) | ||
(defn partially-checked? [element] (make-matcher "toBePartiallyChecked" element)) | ||
(defn has-error-msg? [element message] (make-matcher "toHaveErrorMessage" element message)) | ||
(defn has-accessible-description? [element & [expected-desc]] (make-optional-matcher "toHaveAccessibleDescription" element expected-desc)) | ||
(defn has-accessible-name? [element & [expected-name]] (make-optional-matcher "toHaveAccessibleName" element expected-name)) | ||
(defn has-attr? [element attribute & [value]] (make-optional-matcher "toHaveAttribute" element attribute value)) | ||
(defmatcher disabled? "toBeDisabled") | ||
(defmatcher enabled? "toBeEnabled") | ||
(defmatcher empty-dom-element? "toBeEmptyDOMElement") | ||
(defmatcher in-the-document? "toBeInTheDocument") | ||
(defmatcher invalid? "toBeInvalid") | ||
(defmatcher required? "toBeRequired") | ||
(defmatcher valid? "toBeValid") | ||
(defmatcher visible? "toBeVisible") | ||
(defmatcher contains-element? "toContainElement") | ||
(defmatcher contains-html? "toContainHTML") | ||
(defmatcher has-attribute? "toHaveAttribute") | ||
(defmatcher has-class? "toHaveClass") | ||
(defmatcher has-focus? "toHaveFocus") | ||
(defmatcher has-style? "toHaveStyle") | ||
(defmatcher has-text-content? "toHaveTextContent") | ||
(defmatcher has-value? "toHaveValue") | ||
(defmatcher has-display-value? "toHaveDisplayValue") | ||
(defmatcher checked? "toBeChecked") | ||
(defmatcher partially-checked? "toBePartiallyChecked") | ||
(defmatcher has-error-msg? "toHaveErrorMessage") | ||
(defmatcher has-accessible-description? "toHaveAccessibleDescription") | ||
(defmatcher has-accessible-name? "toHaveAccessibleName") | ||
(defmatcher has-attr? "toHaveAttribute") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.