@@ -11,6 +11,10 @@ import type { MCDocument, MCProject, MCUser } from '@mermaidchart/sdk/dist/types
11
11
12
12
/** Config file with auth_key setup */
13
13
const CONFIG_AUTHED = 'test/fixtures/config-authed.toml' ;
14
+ /** Markdown file that has every Mermaid diagrams already linked */
15
+ const LINKED_MARKDOWN_FILE = 'test/fixtures/linked-markdown-file.md' ;
16
+ /** Markdown file that has some unlinked Mermaid diagrams */
17
+ const UNLINKED_MARKDOWN_FILE = 'test/fixtures/unlinked-markdown-file.md' ;
14
18
15
19
type Optional < T > = T | undefined ;
16
20
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -269,16 +273,42 @@ describe('link', () => {
269
273
) ;
270
274
} ) ;
271
275
}
276
+
277
+ it ( 'should link diagrams in a markdown file' , async ( ) => {
278
+ const unlinkedMarkdownFile = 'test/output/markdown-file.md' ;
279
+ await copyFile ( UNLINKED_MARKDOWN_FILE , unlinkedMarkdownFile ) ;
280
+
281
+ const { program } = mockedProgram ( ) ;
282
+
283
+ vi . mock ( '@inquirer/confirm' ) ;
284
+ vi . mock ( '@inquirer/select' ) ;
285
+ vi . mocked ( confirm ) . mockResolvedValue ( true ) ;
286
+ vi . mocked ( select ) . mockResolvedValueOnce ( mockedProjects [ 0 ] . id ) ;
287
+
288
+ vi . mocked ( MermaidChart . prototype . createDocument )
289
+ . mockResolvedValueOnce ( mockedEmptyDiagram )
290
+ . mockResolvedValueOnce ( { ...mockedEmptyDiagram , documentID : 'second-id' } ) ;
291
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'link' , unlinkedMarkdownFile ] , {
292
+ from : 'user' ,
293
+ } ) ;
294
+
295
+ const file = await readFile ( unlinkedMarkdownFile , { encoding : 'utf8' } ) ;
296
+
297
+ expect ( file ) . toMatch ( `id: ${ mockedEmptyDiagram . documentID } \n` ) ;
298
+ expect ( file ) . toMatch ( `id: second-id\n` ) ;
299
+ } ) ;
272
300
} ) ;
273
301
274
302
describe ( 'pull' , ( ) => {
275
303
const diagram = 'test/output/connected-diagram.mmd' ;
276
304
const diagram2 = 'test/output/connected-diagram-2.mmd' ;
305
+ const linkedMarkdownFile = 'test/output/linked-markdown-file.md' ;
277
306
278
307
beforeEach ( async ( ) => {
279
308
await Promise . all ( [
280
309
copyFile ( 'test/fixtures/connected-diagram.mmd' , diagram ) ,
281
310
copyFile ( 'test/fixtures/connected-diagram.mmd' , diagram2 ) ,
311
+ copyFile ( LINKED_MARKDOWN_FILE , linkedMarkdownFile ) ,
282
312
] ) ;
283
313
} ) ;
284
314
@@ -327,16 +357,41 @@ title: My cool flowchart
327
357
expect ( diagramContents ) . toContain ( "flowchart TD\n A[I've been updated!]" ) ;
328
358
}
329
359
} ) ;
360
+
361
+ it ( 'should pull documents from within markdown file' , async ( ) => {
362
+ const { program } = mockedProgram ( ) ;
363
+
364
+ vi . mocked ( MermaidChart . prototype . getDocument )
365
+ . mockResolvedValueOnce ( {
366
+ ...mockedEmptyDiagram ,
367
+ code : "flowchart TD\n A[I've been updated!]" ,
368
+ } )
369
+ . mockResolvedValueOnce ( {
370
+ ...mockedEmptyDiagram ,
371
+ code : 'pie\n "Flowchart" : 2' ,
372
+ } ) ;
373
+
374
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'pull' , linkedMarkdownFile ] , {
375
+ from : 'user' ,
376
+ } ) ;
377
+
378
+ const file = await readFile ( linkedMarkdownFile , { encoding : 'utf8' } ) ;
379
+
380
+ expect ( file ) . toMatch ( "flowchart TD\n A[I've been updated!]" ) ;
381
+ expect ( file ) . toMatch ( 'pie\n "Flowchart" : 2' ) ;
382
+ } ) ;
330
383
} ) ;
331
384
332
385
describe ( 'push' , ( ) => {
333
386
const diagram = 'test/output/connected-diagram.mmd' ;
334
387
const diagram2 = 'test/output/connected-diagram-2.mmd' ;
388
+ const linkedMarkdownFile = 'test/output/linked-markdown-file.md' ;
335
389
336
390
beforeEach ( async ( ) => {
337
391
await Promise . all ( [
338
392
copyFile ( 'test/fixtures/connected-diagram.mmd' , diagram ) ,
339
393
copyFile ( 'test/fixtures/connected-diagram.mmd' , diagram2 ) ,
394
+ copyFile ( LINKED_MARKDOWN_FILE , linkedMarkdownFile ) ,
340
395
] ) ;
341
396
} ) ;
342
397
@@ -368,4 +423,16 @@ describe('push', () => {
368
423
} ) ,
369
424
) ;
370
425
} ) ;
426
+
427
+ it ( 'should push documents from within markdown file' , async ( ) => {
428
+ const { program } = mockedProgram ( ) ;
429
+
430
+ vi . mocked ( MermaidChart . prototype . getDocument ) . mockResolvedValue ( mockedEmptyDiagram ) ;
431
+
432
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'push' , linkedMarkdownFile ] , {
433
+ from : 'user' ,
434
+ } ) ;
435
+
436
+ expect ( vi . mocked ( MermaidChart . prototype . setDocument ) ) . toHaveBeenCalledTimes ( 2 ) ;
437
+ } ) ;
371
438
} ) ;
0 commit comments