11import $ from "jquery" ;
2- import pattern from "./markdown" ;
2+ import events from "../../core/events" ;
3+ import Pattern from "./markdown" ;
34import utils from "../../core/utils" ;
45import { jest } from "@jest/globals" ;
56
@@ -19,77 +20,85 @@ describe("pat-markdown", function () {
1920 it ( "Replaces the DOM element with the rendered Markdown content." , async function ( ) {
2021 var $el = $ ( '<p class="pat-markdown"></p>' ) ;
2122 $el . appendTo ( "#lab" ) ;
22- jest . spyOn ( pattern . prototype , "render" ) . mockImplementation ( ( ) => {
23+ jest . spyOn ( Pattern . prototype , "render" ) . mockImplementation ( ( ) => {
2324 return $ ( "<p>Rendering</p>" ) ;
2425 } ) ;
25- pattern . init ( $el ) ;
26- await utils . timeout ( 1 ) ; // wait a tick for async to settle.
26+
27+ const instance = new Pattern ( $el ) ;
28+ await events . await_pattern_init ( instance ) ;
29+
2730 expect ( $ ( "#lab" ) . html ( ) ) . toBe ( "<p>Rendering</p>" ) ;
2831 } ) ;
2932
30- it ( "It does not render when the DOM element doesn't have the pattern trigger" , function ( ) {
33+ it ( "It does not render when the DOM element doesn't have the pattern trigger" , async function ( ) {
3134 var $el = $ ( "<p></p>" ) ;
3235 $el . appendTo ( "#lab" ) ;
33- jest . spyOn ( pattern . prototype , "render" ) . mockImplementation ( ( ) => {
36+ jest . spyOn ( Pattern . prototype , "render" ) . mockImplementation ( ( ) => {
3437 return $ ( "<p>Rendering</p>" ) ;
3538 } ) ;
36- pattern . init ( $el ) ;
39+ const instance = new Pattern ( $el ) ;
40+ await events . await_pattern_init ( instance ) ;
41+
3742 expect ( $ ( "#lab" ) . html ( ) ) . toBe ( "<p></p>" ) ;
3843 } ) ;
3944
40- it ( "uses content for non-input elements" , function ( ) {
45+ it ( "uses content for non-input elements" , async function ( ) {
4146 var $el = $ ( '<p class="pat-markdown"/>' ) . text ( "This is markdown" ) ;
4247 $el . appendTo ( "#lab" ) ;
4348 const spy_render = jest
44- . spyOn ( pattern . prototype , "render" )
49+ . spyOn ( Pattern . prototype , "render" )
4550 . mockImplementation ( ( ) => {
4651 return $ ( "<p/>" ) ;
4752 } ) ;
48- pattern . init ( $el ) ;
53+ const instance = new Pattern ( $el ) ;
54+ await events . await_pattern_init ( instance ) ;
55+
4956 expect ( spy_render ) . toHaveBeenCalledWith ( "This is markdown" ) ;
5057 } ) ;
5158
52- it ( "uses value for input elements" , function ( ) {
59+ it ( "uses value for input elements" , async function ( ) {
5360 var $el = $ ( '<textarea class="pat-markdown"/>' ) . val ( "This is markdown" ) ;
5461 $el . appendTo ( "#lab" ) ;
5562 const spy_render = jest
56- . spyOn ( pattern . prototype , "render" )
63+ . spyOn ( Pattern . prototype , "render" )
5764 . mockImplementation ( ( ) => {
5865 return $ ( "<p/>" ) ;
5966 } ) ;
60- pattern . init ( $el ) ;
67+ const instance = new Pattern ( $el ) ;
68+ await events . await_pattern_init ( instance ) ;
69+
6170 expect ( spy_render ) . toHaveBeenCalledWith ( "This is markdown" ) ;
6271 } ) ;
6372 } ) ;
6473
6574 describe ( "when rendering" , function ( ) {
6675 it ( "wraps rendering in a div" , async function ( ) {
67- const $rendering = await pattern . prototype . render ( "*This is markdown*" ) ;
76+ const $rendering = await Pattern . prototype . render ( "*This is markdown*" ) ;
6877 expect ( $rendering [ 0 ] . tagName ) . toBe ( "DIV" ) ;
6978 } ) ;
7079
7180 it ( "converts markdown into HTML" , async function ( ) {
72- const $rendering = await pattern . prototype . render ( "*This is markdown*" ) ;
81+ const $rendering = await Pattern . prototype . render ( "*This is markdown*" ) ;
7382 expect ( $rendering . html ( ) ) . toBe ( `<p><em>This is markdown</em></p>\n` ) ;
7483 } ) ;
7584 } ) ;
7685
7786 describe ( "Session extraction" , function ( ) {
7887 it ( "Unknown section" , function ( ) {
7988 expect (
80- pattern . prototype . extractSection ( "## My title\n\nContent" , "Other title" )
89+ Pattern . prototype . extractSection ( "## My title\n\nContent" , "Other title" )
8190 ) . toBe ( null ) ;
8291 } ) ;
8392
8493 it ( "Last hash-section" , function ( ) {
8594 expect (
86- pattern . prototype . extractSection ( "## My title\n\nContent" , "My title" )
95+ Pattern . prototype . extractSection ( "## My title\n\nContent" , "My title" )
8796 ) . toBe ( "## My title\n\nContent" ) ;
8897 } ) ;
8998
9099 it ( "Hash-section with following section at same level " , function ( ) {
91100 expect (
92- pattern . prototype . extractSection (
101+ Pattern . prototype . extractSection (
93102 "## My title\n\nContent\n## Next section\n" ,
94103 "My title"
95104 )
@@ -98,7 +107,7 @@ describe("pat-markdown", function () {
98107
99108 it ( "Hash-section with following section at lower level " , function ( ) {
100109 expect (
101- pattern . prototype . extractSection (
110+ Pattern . prototype . extractSection (
102111 "## My title\n\nContent\n### Next section\n" ,
103112 "My title"
104113 )
@@ -107,7 +116,7 @@ describe("pat-markdown", function () {
107116
108117 it ( "Double underscore section" , function ( ) {
109118 expect (
110- pattern . prototype . extractSection (
119+ Pattern . prototype . extractSection (
111120 "My title\n=======\nContent" ,
112121 "My title"
113122 )
@@ -116,7 +125,7 @@ describe("pat-markdown", function () {
116125
117126 it ( "Double underscore section with following section at same level" , function ( ) {
118127 expect (
119- pattern . prototype . extractSection (
128+ Pattern . prototype . extractSection (
120129 "My title\n=======\nContent\n\nNext\n====\n" ,
121130 "My title"
122131 )
@@ -125,7 +134,7 @@ describe("pat-markdown", function () {
125134
126135 it ( "Double underscore section with following section at lower level" , function ( ) {
127136 expect (
128- pattern . prototype . extractSection (
137+ Pattern . prototype . extractSection (
129138 "My title\n=======\nContent\n\nNext\n----\n" ,
130139 "My title"
131140 )
@@ -134,7 +143,7 @@ describe("pat-markdown", function () {
134143
135144 it ( "Single underscore section" , function ( ) {
136145 expect (
137- pattern . prototype . extractSection (
146+ Pattern . prototype . extractSection (
138147 "My title\n-------\nContent" ,
139148 "My title"
140149 )
@@ -143,7 +152,7 @@ describe("pat-markdown", function () {
143152
144153 it ( "Single underscore section with following section at same level" , function ( ) {
145154 expect (
146- pattern . prototype . extractSection (
155+ Pattern . prototype . extractSection (
147156 "My title\n-------\nContent\n\nNext\n----\n" ,
148157 "My title"
149158 )
@@ -152,7 +161,7 @@ describe("pat-markdown", function () {
152161
153162 it ( "Single underscore section with following section at higher level" , function ( ) {
154163 expect (
155- pattern . prototype . extractSection (
164+ Pattern . prototype . extractSection (
156165 "My title\n-------\nContent\n\nNext\n====\n" ,
157166 "My title"
158167 )
@@ -177,8 +186,8 @@ some content
177186 </main>
178187 ` ;
179188
180- new pattern ( document . querySelector ( ".pat-markdown" ) ) ;
181- await utils . timeout ( 1 ) ; // wait a tick for async to settle.
189+ const instance = new Pattern ( document . querySelector ( ".pat-markdown" ) ) ;
190+ await events . await_pattern_init ( instance ) ;
182191 await utils . timeout ( 1 ) ; // wait a tick for async to settle.
183192
184193 expect ( document . body . querySelector ( "main > div > h1" ) . textContent ) . toBe ( "Title" ) ; // prettier-ignore
0 commit comments