|
6 | 6 | * @import {Plugin} from 'unified'
|
7 | 7 | */
|
8 | 8 |
|
| 9 | +/** |
| 10 | + * @typedef DeferredPlugin |
| 11 | + * Deferred plugin. |
| 12 | + * @property {Plugin<[]>} plugin |
| 13 | + * Plugin. |
| 14 | + * @property {(error: Error) => undefined} reject |
| 15 | + * Reject the plugin. |
| 16 | + * @property {() => undefined} resolve |
| 17 | + * Resolve the plugin. |
| 18 | + */ |
| 19 | + |
9 | 20 | import assert from 'node:assert/strict'
|
10 | 21 | import test from 'node:test'
|
11 | 22 | import 'global-jsdom/register'
|
@@ -1112,160 +1123,176 @@ test('MarkdownAsync', async function (t) {
|
1112 | 1123 | test('MarkdownHooks', async function (t) {
|
1113 | 1124 | await t.test('should support `MarkdownHooks`', async function () {
|
1114 | 1125 | const plugin = deferPlugin()
|
1115 |
| - |
1116 |
| - const {container} = render( |
| 1126 | + const result = render( |
1117 | 1127 | <MarkdownHooks children={'a'} rehypePlugins={[plugin.plugin]} />
|
1118 | 1128 | )
|
1119 | 1129 |
|
1120 |
| - assert.equal(container.innerHTML, '') |
| 1130 | + assert.equal(result.container.innerHTML, '') |
| 1131 | + |
1121 | 1132 | plugin.resolve()
|
1122 |
| - await waitFor(() => { |
1123 |
| - assert.notEqual(container.innerHTML, '') |
| 1133 | + |
| 1134 | + await waitFor(function () { |
| 1135 | + assert.notEqual(result.container.innerHTML, '') |
1124 | 1136 | })
|
1125 |
| - assert.equal(container.innerHTML, '<p>a</p>') |
| 1137 | + |
| 1138 | + assert.equal(result.container.innerHTML, '<p>a</p>') |
1126 | 1139 | })
|
1127 | 1140 |
|
1128 | 1141 | await t.test(
|
1129 | 1142 | 'should support async plugins w/ `MarkdownHooks` (`rehype-starry-night`)',
|
1130 | 1143 | async function () {
|
1131 | 1144 | const plugin = deferPlugin()
|
1132 |
| - |
1133 |
| - const {container} = render( |
| 1145 | + const result = render( |
1134 | 1146 | <MarkdownHooks
|
1135 | 1147 | children={'```js\nconsole.log(3.14)'}
|
1136 | 1148 | rehypePlugins={[plugin.plugin, rehypeStarryNight]}
|
1137 | 1149 | />
|
1138 | 1150 | )
|
1139 | 1151 |
|
1140 |
| - assert.equal(container.innerHTML, '') |
| 1152 | + assert.equal(result.container.innerHTML, '') |
| 1153 | + |
1141 | 1154 | plugin.resolve()
|
1142 |
| - await waitFor(() => { |
1143 |
| - assert.notEqual(container.innerHTML, '') |
| 1155 | + |
| 1156 | + await waitFor(function () { |
| 1157 | + assert.notEqual(result.container.innerHTML, '') |
1144 | 1158 | })
|
| 1159 | + |
1145 | 1160 | assert.equal(
|
1146 |
| - container.innerHTML, |
| 1161 | + result.container.innerHTML, |
1147 | 1162 | '<pre><code class="language-js"><span class="pl-en">console</span>.<span class="pl-c1">log</span>(<span class="pl-c1">3.14</span>)\n</code></pre>'
|
1148 | 1163 | )
|
1149 | 1164 | }
|
1150 | 1165 | )
|
1151 | 1166 |
|
1152 |
| - await t.test( |
1153 |
| - 'should support `MarkdownHooks` loading fallback', |
1154 |
| - async function () { |
1155 |
| - const plugin = deferPlugin() |
| 1167 | + await t.test('should support `fallback`', async function () { |
| 1168 | + const plugin = deferPlugin() |
| 1169 | + const result = render( |
| 1170 | + <MarkdownHooks |
| 1171 | + children={'a'} |
| 1172 | + fallback="Loading" |
| 1173 | + rehypePlugins={[plugin.plugin]} |
| 1174 | + /> |
| 1175 | + ) |
1156 | 1176 |
|
1157 |
| - const {container} = render( |
1158 |
| - <MarkdownHooks |
1159 |
| - children={'a'} |
1160 |
| - fallback="Loading" |
1161 |
| - rehypePlugins={[plugin.plugin]} |
1162 |
| - /> |
1163 |
| - ) |
| 1177 | + assert.equal(result.container.innerHTML, 'Loading') |
1164 | 1178 |
|
1165 |
| - assert.equal(container.innerHTML, 'Loading') |
1166 |
| - plugin.resolve() |
1167 |
| - await waitFor(() => { |
1168 |
| - assert.notEqual(container.innerHTML, 'Loading') |
1169 |
| - }) |
1170 |
| - assert.equal(container.innerHTML, '<p>a</p>') |
1171 |
| - } |
1172 |
| - ) |
| 1179 | + plugin.resolve() |
1173 | 1180 |
|
1174 |
| - await t.test('should support `MarkdownHooks` that error', async function () { |
1175 |
| - const plugin = deferPlugin() |
| 1181 | + await waitFor(function () { |
| 1182 | + assert.notEqual(result.container.innerHTML, 'Loading') |
| 1183 | + }) |
1176 | 1184 |
|
1177 |
| - const {container} = render( |
| 1185 | + assert.equal(result.container.innerHTML, '<p>a</p>') |
| 1186 | + }) |
| 1187 | + |
| 1188 | + await t.test('should support plugins that error', async function () { |
| 1189 | + const plugin = deferPlugin() |
| 1190 | + const result = render( |
1178 | 1191 | <ErrorBoundary>
|
1179 | 1192 | <MarkdownHooks children={'a'} rehypePlugins={[plugin.plugin]} />
|
1180 | 1193 | </ErrorBoundary>
|
1181 | 1194 | )
|
1182 | 1195 |
|
1183 |
| - assert.equal(container.innerHTML, '') |
| 1196 | + assert.equal(result.container.innerHTML, '') |
| 1197 | + |
| 1198 | + console.info('\nNote: the below error (`Error: rejected`) is expected.\n') |
| 1199 | + |
1184 | 1200 | plugin.reject(new Error('rejected'))
|
1185 |
| - await waitFor(() => { |
1186 |
| - assert.notEqual(container.innerHTML, '') |
| 1201 | + |
| 1202 | + await waitFor(function () { |
| 1203 | + assert.notEqual(result.container.innerHTML, '') |
1187 | 1204 | })
|
1188 |
| - assert.equal(container.innerHTML, 'Error: rejected') |
| 1205 | + |
| 1206 | + console.info('Note: the above error (`Error: rejected`) was expected.') |
| 1207 | + |
| 1208 | + assert.equal(result.container.innerHTML, 'Error: rejected') |
1189 | 1209 | })
|
1190 | 1210 |
|
1191 |
| - await t.test('should support `MarkdownHooks` rerenders', async function () { |
| 1211 | + await t.test('should support rerenders', async function () { |
1192 | 1212 | const pluginA = deferPlugin()
|
1193 | 1213 | const pluginB = deferPlugin()
|
1194 | 1214 |
|
1195 | 1215 | const result = render(
|
1196 | 1216 | <MarkdownHooks children={'a'} rehypePlugins={[pluginA.plugin]} />
|
1197 | 1217 | )
|
1198 | 1218 |
|
| 1219 | + assert.equal(result.container.innerHTML, '') |
| 1220 | + |
1199 | 1221 | result.rerender(
|
1200 | 1222 | <MarkdownHooks children={'b'} rehypePlugins={[pluginB.plugin]} />
|
1201 | 1223 | )
|
1202 | 1224 |
|
1203 | 1225 | assert.equal(result.container.innerHTML, '')
|
1204 |
| - pluginB.resolve() |
| 1226 | + |
1205 | 1227 | pluginA.resolve()
|
1206 |
| - await waitFor(() => { |
| 1228 | + pluginB.resolve() |
| 1229 | + |
| 1230 | + await waitFor(function () { |
1207 | 1231 | assert.notEqual(result.container.innerHTML, '')
|
1208 | 1232 | })
|
| 1233 | + |
1209 | 1234 | assert.equal(result.container.innerHTML, '<p>b</p>')
|
1210 | 1235 | })
|
1211 | 1236 | })
|
1212 | 1237 |
|
1213 | 1238 | /**
|
1214 |
| - * @typedef DeferredPlugin |
1215 |
| - * @property {Plugin<[]>} plugin |
1216 |
| - * A unified plugin |
1217 |
| - * @property {() => void} resolve |
1218 |
| - * Resolve the plugin. |
1219 |
| - * @property {(error: Error) => void} reject |
1220 |
| - * Reject the plugin. |
1221 |
| - */ |
1222 |
| - |
1223 |
| -/** |
1224 |
| - * Create an async unified plugin which waits until a promise is resolved. |
| 1239 | + * Create an async unified plugin that waits until a promise is resolved or |
| 1240 | + * rejected from the outside. |
1225 | 1241 | *
|
1226 | 1242 | * @returns {DeferredPlugin}
|
1227 |
| - * The plugin and resolver. |
| 1243 | + * Deferred plugin object. |
1228 | 1244 | */
|
1229 | 1245 | function deferPlugin() {
|
1230 |
| - /** @type {() => void} */ |
1231 |
| - let res |
1232 | 1246 | /** @type {(error: Error) => void} */
|
1233 |
| - let rej |
| 1247 | + let hoistedReject |
| 1248 | + /** @type {() => void} */ |
| 1249 | + let hoistedResolve |
1234 | 1250 | /** @type {Promise<void>} */
|
1235 |
| - const promise = new Promise((resolve, reject) => { |
1236 |
| - res = resolve |
1237 |
| - rej = reject |
| 1251 | + const promise = new Promise(function (resolve, reject) { |
| 1252 | + hoistedResolve = resolve |
| 1253 | + hoistedReject = reject |
1238 | 1254 | })
|
1239 | 1255 |
|
1240 | 1256 | return {
|
1241 |
| - resolve() { |
1242 |
| - res() |
| 1257 | + plugin() { |
| 1258 | + return function () { |
| 1259 | + return promise |
| 1260 | + } |
1243 | 1261 | },
|
1244 | 1262 | reject(error) {
|
1245 |
| - rej(error) |
| 1263 | + hoistedReject(error) |
1246 | 1264 | },
|
1247 |
| - plugin() { |
1248 |
| - return () => promise |
| 1265 | + resolve() { |
| 1266 | + hoistedResolve() |
1249 | 1267 | }
|
1250 | 1268 | }
|
1251 | 1269 | }
|
1252 | 1270 |
|
| 1271 | +/** |
| 1272 | + * Basic error boundary. |
| 1273 | + */ |
1253 | 1274 | class ErrorBoundary extends Component {
|
1254 |
| - state = { |
1255 |
| - error: null |
1256 |
| - } |
1257 |
| - |
1258 | 1275 | /**
|
1259 | 1276 | * @param {Error} error
|
| 1277 | + * Error. |
| 1278 | + * @returns {undefined} |
| 1279 | + * Nothing. |
1260 | 1280 | */
|
1261 | 1281 | componentDidCatch(error) {
|
1262 | 1282 | this.setState({error})
|
1263 | 1283 | }
|
1264 | 1284 |
|
1265 | 1285 | render() {
|
1266 |
| - const {children} = /** @type {{children: ReactNode}} */ (this.props) |
1267 |
| - const {error} = this.state |
| 1286 | + const props = /** @type {{children: ReactNode}} */ (this.props) |
1268 | 1287 |
|
1269 |
| - return error ? String(error) : children |
| 1288 | + return this.state.error ? String(this.state.error) : props.children |
| 1289 | + } |
| 1290 | + |
| 1291 | + state = { |
| 1292 | + /** |
| 1293 | + * @type {Error | undefined} |
| 1294 | + * Error. |
| 1295 | + */ |
| 1296 | + error: undefined |
1270 | 1297 | }
|
1271 | 1298 | }
|
0 commit comments