File tree Expand file tree Collapse file tree 2 files changed +57
-2
lines changed Expand file tree Collapse file tree 2 files changed +57
-2
lines changed Original file line number Diff line number Diff line change @@ -6,13 +6,13 @@ const writeFile = util.promisify(fs.writeFile);
66
77module . exports . writeFrontmatterMarkdown = (
88 filePath ,
9- { body = "" , frontmatter }
9+ { body = "" , frontmatter = { } }
1010) => {
1111 const lines = [
1212 "---" ,
1313 yaml . stringify ( frontmatter ) . trim ( ) ,
1414 "---" ,
15- body . length > 0 ? body . trim ( ) : "" ,
15+ body ? body . toString ( ) . trim ( ) : "" ,
1616 ""
1717 ] ;
1818 const content = lines . join ( "\n" ) ;
Original file line number Diff line number Diff line change 1+ const util = require ( "util" ) ;
2+
3+ const mockPromisifiedFunction = jest . fn ( ) ;
4+
5+ util . promisify = jest . fn ( ( ) => mockPromisifiedFunction ) ;
6+
7+ jest . mock ( "fs" ) ;
8+
9+ const fileWriters = require ( "./file-writers" ) ;
10+
11+ afterEach ( ( ) => {
12+ jest . clearAllMocks ( ) ;
13+ } ) ;
14+
15+ describe ( "`writeFrontmatterMarkdown()`" , ( ) => {
16+ test ( "writes Markdown files with frontmatter" , ( ) => {
17+ const mockContent = {
18+ body : "This is the body" ,
19+ frontmatter : {
20+ name : "John Doe"
21+ }
22+ } ;
23+ const mockPath = "/Users/johndoe/file.md" ;
24+
25+ fileWriters . writeFrontmatterMarkdown ( mockPath , mockContent ) ;
26+
27+ expect ( mockPromisifiedFunction ) . toHaveBeenCalledTimes ( 1 ) ;
28+
29+ const [ filePath , content ] = mockPromisifiedFunction . mock . calls [ 0 ] ;
30+
31+ expect ( filePath ) . toBe ( mockPath ) ;
32+ expect ( content ) . toBe (
33+ `---\nname: ${ mockContent . frontmatter . name } \n---\n${ mockContent . body } \n`
34+ ) ;
35+ } ) ;
36+
37+ test ( "converts body to string" , ( ) => {
38+ const mockContent = {
39+ body : [ 1 , 2 , 3 ] ,
40+ frontmatter : {
41+ name : "John Doe"
42+ }
43+ } ;
44+ const mockPath = "/Users/johndoe/file.md" ;
45+
46+ fileWriters . writeFrontmatterMarkdown ( mockPath , mockContent ) ;
47+
48+ expect ( mockPromisifiedFunction ) . toHaveBeenCalledTimes ( 1 ) ;
49+
50+ const [ filePath , content ] = mockPromisifiedFunction . mock . calls [ 0 ] ;
51+
52+ expect ( filePath ) . toBe ( mockPath ) ;
53+ expect ( content ) . toBe ( "---\nname: John Doe\n---\n1,2,3\n" ) ;
54+ } ) ;
55+ } ) ;
You can’t perform that action at this time.
0 commit comments