1
1
import $ from "jquery" ;
2
- import pattern from "./markdown " ;
3
- import utils from "../../core/utils " ;
2
+ import events from "../../core/events " ;
3
+ import Pattern from "./markdown " ;
4
4
import { jest } from "@jest/globals" ;
5
5
6
6
describe ( "pat-markdown" , function ( ) {
@@ -19,77 +19,85 @@ describe("pat-markdown", function () {
19
19
it ( "Replaces the DOM element with the rendered Markdown content." , async function ( ) {
20
20
var $el = $ ( '<p class="pat-markdown"></p>' ) ;
21
21
$el . appendTo ( "#lab" ) ;
22
- jest . spyOn ( pattern . prototype , "render" ) . mockImplementation ( ( ) => {
22
+ jest . spyOn ( Pattern . prototype , "render" ) . mockImplementation ( ( ) => {
23
23
return $ ( "<p>Rendering</p>" ) ;
24
24
} ) ;
25
- pattern . init ( $el ) ;
26
- await utils . timeout ( 1 ) ; // wait a tick for async to settle.
25
+
26
+ const instance = new Pattern ( $el ) ;
27
+ await events . await_pattern_init ( instance ) ;
28
+
27
29
expect ( $ ( "#lab" ) . html ( ) ) . toBe ( "<p>Rendering</p>" ) ;
28
30
} ) ;
29
31
30
- it ( "It does not render when the DOM element doesn't have the pattern trigger" , function ( ) {
32
+ it ( "It does not render when the DOM element doesn't have the pattern trigger" , async function ( ) {
31
33
var $el = $ ( "<p></p>" ) ;
32
34
$el . appendTo ( "#lab" ) ;
33
- jest . spyOn ( pattern . prototype , "render" ) . mockImplementation ( ( ) => {
35
+ jest . spyOn ( Pattern . prototype , "render" ) . mockImplementation ( ( ) => {
34
36
return $ ( "<p>Rendering</p>" ) ;
35
37
} ) ;
36
- pattern . init ( $el ) ;
38
+ const instance = new Pattern ( $el ) ;
39
+ await events . await_pattern_init ( instance ) ;
40
+
37
41
expect ( $ ( "#lab" ) . html ( ) ) . toBe ( "<p></p>" ) ;
38
42
} ) ;
39
43
40
- it ( "uses content for non-input elements" , function ( ) {
44
+ it ( "uses content for non-input elements" , async function ( ) {
41
45
var $el = $ ( '<p class="pat-markdown"/>' ) . text ( "This is markdown" ) ;
42
46
$el . appendTo ( "#lab" ) ;
43
47
const spy_render = jest
44
- . spyOn ( pattern . prototype , "render" )
48
+ . spyOn ( Pattern . prototype , "render" )
45
49
. mockImplementation ( ( ) => {
46
50
return $ ( "<p/>" ) ;
47
51
} ) ;
48
- pattern . init ( $el ) ;
52
+ const instance = new Pattern ( $el ) ;
53
+ await events . await_pattern_init ( instance ) ;
54
+
49
55
expect ( spy_render ) . toHaveBeenCalledWith ( "This is markdown" ) ;
50
56
} ) ;
51
57
52
- it ( "uses value for input elements" , function ( ) {
58
+ it ( "uses value for input elements" , async function ( ) {
53
59
var $el = $ ( '<textarea class="pat-markdown"/>' ) . val ( "This is markdown" ) ;
54
60
$el . appendTo ( "#lab" ) ;
55
61
const spy_render = jest
56
- . spyOn ( pattern . prototype , "render" )
62
+ . spyOn ( Pattern . prototype , "render" )
57
63
. mockImplementation ( ( ) => {
58
64
return $ ( "<p/>" ) ;
59
65
} ) ;
60
- pattern . init ( $el ) ;
66
+ const instance = new Pattern ( $el ) ;
67
+ await events . await_pattern_init ( instance ) ;
68
+
61
69
expect ( spy_render ) . toHaveBeenCalledWith ( "This is markdown" ) ;
62
70
} ) ;
63
71
} ) ;
64
72
65
73
describe ( "when rendering" , function ( ) {
66
74
it ( "wraps rendering in a div" , async function ( ) {
67
- const $rendering = await pattern . prototype . render ( "*This is markdown*" ) ;
75
+ const $rendering = await Pattern . prototype . render ( "*This is markdown*" ) ;
68
76
expect ( $rendering [ 0 ] . tagName ) . toBe ( "DIV" ) ;
69
77
} ) ;
70
78
71
79
it ( "converts markdown into HTML" , async function ( ) {
72
- const $rendering = await pattern . prototype . render ( "*This is markdown*" ) ;
80
+ const $rendering = await Pattern . prototype . render ( "*This is markdown*" ) ;
73
81
expect ( $rendering . html ( ) ) . toBe ( `<p><em>This is markdown</em></p>\n` ) ;
74
82
} ) ;
75
83
} ) ;
76
84
77
85
describe ( "Session extraction" , function ( ) {
78
86
it ( "Unknown section" , function ( ) {
79
87
expect (
80
- pattern . prototype . extractSection ( "## My title\n\nContent" , "Other title" )
88
+ Pattern . prototype . extractSection ( "## My title\n\nContent" , "Other title" )
81
89
) . toBe ( null ) ;
82
90
} ) ;
83
91
84
92
it ( "Last hash-section" , function ( ) {
85
93
expect (
86
- pattern . prototype . extractSection ( "## My title\n\nContent" , "My title" )
94
+ Pattern . prototype . extractSection ( "## My title\n\nContent" , "My title" )
87
95
) . toBe ( "## My title\n\nContent" ) ;
88
96
} ) ;
89
97
90
98
it ( "Hash-section with following section at same level " , function ( ) {
91
99
expect (
92
- pattern . prototype . extractSection (
100
+ Pattern . prototype . extractSection (
93
101
"## My title\n\nContent\n## Next section\n" ,
94
102
"My title"
95
103
)
@@ -98,7 +106,7 @@ describe("pat-markdown", function () {
98
106
99
107
it ( "Hash-section with following section at lower level " , function ( ) {
100
108
expect (
101
- pattern . prototype . extractSection (
109
+ Pattern . prototype . extractSection (
102
110
"## My title\n\nContent\n### Next section\n" ,
103
111
"My title"
104
112
)
@@ -107,7 +115,7 @@ describe("pat-markdown", function () {
107
115
108
116
it ( "Double underscore section" , function ( ) {
109
117
expect (
110
- pattern . prototype . extractSection (
118
+ Pattern . prototype . extractSection (
111
119
"My title\n=======\nContent" ,
112
120
"My title"
113
121
)
@@ -116,7 +124,7 @@ describe("pat-markdown", function () {
116
124
117
125
it ( "Double underscore section with following section at same level" , function ( ) {
118
126
expect (
119
- pattern . prototype . extractSection (
127
+ Pattern . prototype . extractSection (
120
128
"My title\n=======\nContent\n\nNext\n====\n" ,
121
129
"My title"
122
130
)
@@ -125,7 +133,7 @@ describe("pat-markdown", function () {
125
133
126
134
it ( "Double underscore section with following section at lower level" , function ( ) {
127
135
expect (
128
- pattern . prototype . extractSection (
136
+ Pattern . prototype . extractSection (
129
137
"My title\n=======\nContent\n\nNext\n----\n" ,
130
138
"My title"
131
139
)
@@ -134,7 +142,7 @@ describe("pat-markdown", function () {
134
142
135
143
it ( "Single underscore section" , function ( ) {
136
144
expect (
137
- pattern . prototype . extractSection (
145
+ Pattern . prototype . extractSection (
138
146
"My title\n-------\nContent" ,
139
147
"My title"
140
148
)
@@ -143,7 +151,7 @@ describe("pat-markdown", function () {
143
151
144
152
it ( "Single underscore section with following section at same level" , function ( ) {
145
153
expect (
146
- pattern . prototype . extractSection (
154
+ Pattern . prototype . extractSection (
147
155
"My title\n-------\nContent\n\nNext\n----\n" ,
148
156
"My title"
149
157
)
@@ -152,7 +160,7 @@ describe("pat-markdown", function () {
152
160
153
161
it ( "Single underscore section with following section at higher level" , function ( ) {
154
162
expect (
155
- pattern . prototype . extractSection (
163
+ Pattern . prototype . extractSection (
156
164
"My title\n-------\nContent\n\nNext\n====\n" ,
157
165
"My title"
158
166
)
@@ -162,6 +170,8 @@ describe("pat-markdown", function () {
162
170
163
171
describe ( "Code blocks" , function ( ) {
164
172
it ( "It correctly renders code blocks" , async function ( ) {
173
+ await import ( "../syntax-highlight/syntax-highlight" ) ;
174
+
165
175
document . body . innerHTML = `
166
176
<main>
167
177
<div class="pat-markdown">
@@ -177,9 +187,8 @@ some content
177
187
</main>
178
188
` ;
179
189
180
- new pattern ( document . querySelector ( ".pat-markdown" ) ) ;
181
- await utils . timeout ( 1 ) ; // wait a tick for async to settle.
182
- await utils . timeout ( 1 ) ; // wait a tick for async to settle.
190
+ const instance = new Pattern ( document . querySelector ( ".pat-markdown" ) ) ;
191
+ await events . await_pattern_init ( instance ) ;
183
192
184
193
expect ( document . body . querySelector ( "main > div > h1" ) . textContent ) . toBe ( "Title" ) ; // prettier-ignore
185
194
expect ( document . body . querySelector ( "main > div > p" ) . textContent ) . toBe ( "some content" ) ; // prettier-ignore
0 commit comments