|  | 
|  | 1 | +--- | 
|  | 2 | +id: example-intro | 
|  | 3 | +title: Example | 
|  | 4 | +--- | 
|  | 5 | + | 
|  | 6 | +The below examples use | 
|  | 7 | +[`bs-webapi`](https://github.com/reasonml-community/bs-webapi-incubator) to help | 
|  | 8 | +with typings and creating events. | 
|  | 9 | + | 
|  | 10 | +## getByText | 
|  | 11 | + | 
|  | 12 | +```reason | 
|  | 13 | +/* __tests__/example_test.re */ | 
|  | 14 | +open Jest; | 
|  | 15 | +open DomTestingLibrary; | 
|  | 16 | +open Expect; | 
|  | 17 | +
 | 
|  | 18 | +type parser; | 
|  | 19 | +
 | 
|  | 20 | +[@bs.new] | 
|  | 21 | +external domParser : unit => parser = "DOMParser"; | 
|  | 22 | +
 | 
|  | 23 | +[@bs.send.pipe : parser] | 
|  | 24 | +external parseFromString : ( string, [@bs.as "text/html"] _) => Dom.element = ""; | 
|  | 25 | +
 | 
|  | 26 | +[@bs.get] | 
|  | 27 | +external body : Dom.element => Dom.element = ""; | 
|  | 28 | +
 | 
|  | 29 | +[@bs.get] | 
|  | 30 | +external firstChild : Dom.element => Dom.element = ""; | 
|  | 31 | +
 | 
|  | 32 | +let div = domParser() | 
|  | 33 | + |> parseFromString({j| | 
|  | 34 | + <div> | 
|  | 35 | + <b title="greeting">Hello,</b> | 
|  | 36 | + <p data-testid="world"> World!</p> | 
|  | 37 | + <input type="text" placeholder="Enter something" /> | 
|  | 38 | + <input type="text" value="Some value" /> | 
|  | 39 | + <img src="" alt="Alt text" /> | 
|  | 40 | + </div> | 
|  | 41 | + |j}) | 
|  | 42 | + |> body | 
|  | 43 | + |> firstChild; | 
|  | 44 | +
 | 
|  | 45 | +describe("getByText", () => { | 
|  | 46 | + test("works with string matcher", () => { | 
|  | 47 | + let actual = div |> getByText(~matcher=`Str("Hello,")); | 
|  | 48 | +
 | 
|  | 49 | + expect(actual) |> toMatchSnapshot; | 
|  | 50 | + }); | 
|  | 51 | +
 | 
|  | 52 | + test("works with regex matcher", () => { | 
|  | 53 | + let actual = div |> getByText(~matcher=`RegExp([%bs.re "/\\w!/"])); | 
|  | 54 | +
 | 
|  | 55 | + expect(actual) |> toMatchSnapshot; | 
|  | 56 | + }); | 
|  | 57 | +
 | 
|  | 58 | + test("works with function matcher", () => { | 
|  | 59 | + let matcher = ( _text, node ) => (node |> tagName) === "P"; | 
|  | 60 | + let actual = div |> getByText(~matcher=`Func(matcher)); | 
|  | 61 | +
 | 
|  | 62 | + expect(actual) |> toMatchSnapshot; | 
|  | 63 | + }); | 
|  | 64 | +}); | 
|  | 65 | +``` | 
|  | 66 | + | 
|  | 67 | +## FireEvent | 
|  | 68 | + | 
|  | 69 | +```reason | 
|  | 70 | +open Jest; | 
|  | 71 | +open DomTestingLibrary; | 
|  | 72 | +open Expect; | 
|  | 73 | +
 | 
|  | 74 | +describe("FireEvent", () => { | 
|  | 75 | + test("click works", () => { | 
|  | 76 | + open Webapi.Dom; | 
|  | 77 | +
 | 
|  | 78 | + let node = document |> Document.createElement("button"); | 
|  | 79 | + let spy = JestJs.inferred_fn(); | 
|  | 80 | + let fn = spy |> MockJs.fn; | 
|  | 81 | + let clickHandler = _ => [@bs] fn("clicked!") |> ignore; | 
|  | 82 | +
 | 
|  | 83 | + node |> Element.addEventListener("click", clickHandler); | 
|  | 84 | +
 | 
|  | 85 | + FireEvent.click(node); | 
|  | 86 | +
 | 
|  | 87 | + expect(spy |> MockJs.calls) |> toEqual([|"clicked!"|]); | 
|  | 88 | + }); | 
|  | 89 | +
 | 
|  | 90 | + test("change works", () => { | 
|  | 91 | + open Webapi.Dom; | 
|  | 92 | +
 | 
|  | 93 | + let node = document |> Document.createElement("input"); | 
|  | 94 | + let spy = JestJs.inferred_fn(); | 
|  | 95 | + let fn = spy |> MockJs.fn; | 
|  | 96 | + let changeHandler = _ => [@bs] fn("changed!") |> ignore; | 
|  | 97 | + let event = Event.makeWithOptions("change", { "target": { "value": "1" } }); | 
|  | 98 | +
 | 
|  | 99 | + node |> Element.addEventListener("change", changeHandler); | 
|  | 100 | +
 | 
|  | 101 | + FireEvent.change(node, event); | 
|  | 102 | +
 | 
|  | 103 | + expect(spy |> MockJs.calls) |> toEqual([|"changed!"|]); | 
|  | 104 | + }); | 
|  | 105 | +}); | 
|  | 106 | +``` | 
|  | 107 | + | 
|  | 108 | +## More | 
|  | 109 | + | 
|  | 110 | +You can find more bs-dom-testing-library examples at | 
|  | 111 | +[wyze/bs-dom-testing-library/src/\_\_tests\_\_](https://github.com/wyze/bs-dom-testing-library/tree/master/src/__tests__). | 
0 commit comments