Yuki Takei il y a 1 an
Parent
commit
f43a4f17e6
1 fichiers modifiés avec 48 ajouts et 23 suppressions
  1. 48 23
      apps/app/src/features/callout/services/callout.spec.ts

+ 48 - 23
apps/app/src/features/callout/services/callout.spec.ts

@@ -1,17 +1,18 @@
+import type { ContainerDirective } from 'mdast-util-directive';
 import remarkDirective from 'remark-directive';
 import remarkDirective from 'remark-directive';
 import remarkParse from 'remark-parse';
 import remarkParse from 'remark-parse';
 import { unified } from 'unified';
 import { unified } from 'unified';
 import { visit } from 'unist-util-visit';
 import { visit } from 'unist-util-visit';
 import { describe, it, expect } from 'vitest';
 import { describe, it, expect } from 'vitest';
 
 
-import { remarkPlugin } from './callout';
+import * as callout from './callout';
 
 
 describe('remarkPlugin', () => {
 describe('remarkPlugin', () => {
   it('should transform containerDirective to callout', () => {
   it('should transform containerDirective to callout', () => {
     const processor = unified()
     const processor = unified()
       .use(remarkParse)
       .use(remarkParse)
       .use(remarkDirective)
       .use(remarkDirective)
-      .use(remarkPlugin);
+      .use(callout.remarkPlugin);
 
 
     const markdown = `
     const markdown = `
 :::info
 :::info
@@ -23,33 +24,57 @@ This is an info callout.
     processor.runSync(tree);
     processor.runSync(tree);
 
 
     let calloutNode;
     let calloutNode;
-    visit(tree, 'callout', (node) => {
+    visit(tree, 'containerDirective', (node) => {
       calloutNode = node;
       calloutNode = node;
     });
     });
 
 
     expect(calloutNode).toBeDefined();
     expect(calloutNode).toBeDefined();
+
+    assert(calloutNode?.data?.hName != null);
+    assert(calloutNode?.data?.hProperties != null);
+
+    expect(calloutNode.data.hName).toBe('callout');
     expect(calloutNode.data.hProperties.type).toBe('info');
     expect(calloutNode.data.hProperties.type).toBe('info');
     expect(calloutNode.data.hProperties.label).toBe('info');
     expect(calloutNode.data.hProperties.label).toBe('info');
+
+    assert('children' in calloutNode.children[0]);
+    assert('value' in calloutNode.children[0].children[0]);
+
+    expect(calloutNode.children[0].children[0].value).toBe('This is an info callout.');
   });
   });
 
 
-  //   it('should extract and remove directive label', () => {
-  //     const processor = unified()
-  //       .use(remarkParse)
-  //       .use(remarkDirective)
-  //       .use(remarkPlugin);
-
-  //     const markdown = `
-  // :::info[custom label]
-  // This is an info callout.
-  // :::
-  //     `;
-
-  //     const tree = processor.parse(markdown);
-  //     processor.runSync(tree);
-
-//     const callout = tree.children.find(node => node.type === 'callout');
-//     expect(callout).toBeDefined();
-//     expect(callout.data.hProperties.label).toBe('Label');
-//     expect(callout.children.some(child => child.type === 'paragraph' && (child.children[0] as Text).value === 'Label')).toBe(false);
-//   });
+  it('should transform containerDirective to callout with custom label', () => {
+    const processor = unified()
+      .use(remarkParse)
+      .use(remarkDirective)
+      .use(callout.remarkPlugin);
+
+    const markdown = `
+:::info[CUSTOM LABEL]
+This is an info callout.
+:::
+    `;
+
+    const tree = processor.parse(markdown);
+    processor.runSync(tree);
+
+    let calloutNode: ContainerDirective | undefined;
+    visit(tree, 'containerDirective', (node) => {
+      calloutNode = node;
+    });
+
+    expect(calloutNode).toBeDefined();
+
+    assert(calloutNode?.data?.hName != null);
+    assert(calloutNode?.data?.hProperties != null);
+
+    expect(calloutNode.data.hName).toBe('callout');
+    expect(calloutNode.data.hProperties.type).toBe('info');
+    expect(calloutNode.data.hProperties.label).toBe('CUSTOM LABEL');
+
+    assert('children' in calloutNode.children[0]);
+    assert('value' in calloutNode.children[0].children[0]);
+
+    expect(calloutNode.children[0].children[0].value).toBe('This is an info callout.');
+  });
 });
 });