Skip to content

Commit f8b3736

Browse files
committed
test(theme-common): More tests for currently known options
1 parent 22edeab commit f8b3736

File tree

2 files changed

+211
-124
lines changed

2 files changed

+211
-124
lines changed

packages/docusaurus-theme-common/src/utils/__tests__/__snapshots__/codeBlockUtils.test.ts.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ exports[`getCodeBlockTitle returns titleProp with empty string on option 1`] = `
1212

1313
exports[`getCodeBlockTitle with nothing set 1`] = `undefined`;
1414

15+
exports[`getLineNumbersStart from metastring parses flags as 1 1`] = `1`;
16+
17+
exports[`getLineNumbersStart from metastring parses value 1`] = `10`;
18+
1519
exports[`getLineNumbersStart handles metadata combined with other options set as flag 1`] = `1`;
1620

1721
exports[`getLineNumbersStart handles metadata combined with other options set with number 1`] = `10`;
@@ -36,6 +40,30 @@ exports[`getLineNumbersStart with nothing set 1`] = `undefined`;
3640

3741
exports[`getLineNumbersStart with nothing set 2`] = `undefined`;
3842

43+
exports[`getLineNumbersStart with parsed metaoption handles metadata combined with other options set as flag 1`] = `1`;
44+
45+
exports[`getLineNumbersStart with parsed metaoption handles metadata combined with other options set with number 1`] = `10`;
46+
47+
exports[`getLineNumbersStart with parsed metaoption handles metadata standalone set as flag 1`] = `1`;
48+
49+
exports[`getLineNumbersStart with parsed metaoption handles metadata standalone set with number 1`] = `10`;
50+
51+
exports[`getLineNumbersStart with parsed metaoption handles prop combined with metaoptions set to false 1`] = `undefined`;
52+
53+
exports[`getLineNumbersStart with parsed metaoption handles prop combined with metaoptions set to number 1`] = `10`;
54+
55+
exports[`getLineNumbersStart with parsed metaoption handles prop combined with metaoptions set to true 1`] = `1`;
56+
57+
exports[`getLineNumbersStart with parsed metaoption handles prop standalone set to false 1`] = `undefined`;
58+
59+
exports[`getLineNumbersStart with parsed metaoption handles prop standalone set to number 1`] = `10`;
60+
61+
exports[`getLineNumbersStart with parsed metaoption handles prop standalone set to true 1`] = `1`;
62+
63+
exports[`getLineNumbersStart with parsed metaoption with nothing set 1`] = `undefined`;
64+
65+
exports[`getLineNumbersStart with parsed metaoption with nothing set 2`] = `undefined`;
66+
3967
exports[`parseLines does not parse content with metastring 1`] = `
4068
{
4169
"code": "aaaaa

packages/docusaurus-theme-common/src/utils/__tests__/codeBlockUtils.test.ts

Lines changed: 183 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ describe('parseCodeBlockMetaOptions', () => {
3939
it('does not parse mismatched quote delimiters', () => {
4040
expect(
4141
parseCodeBlockMetaOptions(`title="index.js'`, undefined).title,
42-
).toBe(``);
42+
).toBeUndefined();
4343
});
4444

4545
it('parses undefined metastring', () => {
46-
expect(parseCodeBlockMetaOptions(undefined, undefined).title).toBe(``);
46+
expect(
47+
parseCodeBlockMetaOptions(undefined, undefined).title,
48+
).toBeUndefined();
4749
});
4850

4951
it('parses metastring with no title specified', () => {
50-
expect(parseCodeBlockMetaOptions(`{1,2-3}`, undefined).title).toBe(``);
52+
expect(
53+
parseCodeBlockMetaOptions(`{1,2-3}`, undefined).title,
54+
).toBeUndefined();
5155
});
5256

5357
it('parses with multiple metadata title first', () => {
@@ -86,7 +90,34 @@ describe('parseCodeBlockMetaOptions', () => {
8690
).toBe(`console.log('Hello, World!')`);
8791
});
8892
});
93+
94+
// showLineNumber logic is tested in combination with getLineNumber below
95+
96+
describe('live', () => {
97+
it('parses live as true', () => {
98+
expect(parseCodeBlockMetaOptions(`live`, undefined).live).toBe(true);
99+
});
100+
it('parses no live as undefined', () => {
101+
expect(
102+
parseCodeBlockMetaOptions(` otherOption `, undefined).live,
103+
).toBeUndefined();
104+
});
105+
});
106+
107+
describe('noInline', () => {
108+
it('parses noInline as true', () => {
109+
expect(parseCodeBlockMetaOptions(`noInline`, undefined).noInline).toBe(
110+
true,
111+
);
112+
});
113+
it('parses no noInline as undefined', () => {
114+
expect(
115+
parseCodeBlockMetaOptions(` otherOption `, undefined).noInline,
116+
).toBeUndefined();
117+
});
118+
});
89119
});
120+
90121
describe('parseLanguage', () => {
91122
it('works', () => {
92123
expect(parseLanguage('language-foo xxx yyy')).toBe('foo');
@@ -423,142 +454,170 @@ line
423454
});
424455

425456
describe('getLineNumbersStart', () => {
426-
it('with nothing set', () => {
427-
expect(
428-
getLineNumbersStart({
429-
showLineNumbers: undefined,
430-
metaOptions: {},
431-
}),
432-
).toMatchSnapshot();
433-
expect(
434-
getLineNumbersStart({
435-
showLineNumbers: undefined,
436-
metaOptions: {},
437-
}),
438-
).toMatchSnapshot();
439-
});
457+
describe('with parsed metaoption', () => {
458+
it('with nothing set', () => {
459+
expect(
460+
getLineNumbersStart({
461+
showLineNumbers: undefined,
462+
metaOptions: {},
463+
}),
464+
).toMatchSnapshot();
465+
expect(
466+
getLineNumbersStart({
467+
showLineNumbers: undefined,
468+
metaOptions: {},
469+
}),
470+
).toMatchSnapshot();
471+
});
440472

441-
describe('handles prop', () => {
442-
describe('combined with metaoptions', () => {
443-
it('set to true', () => {
444-
expect(
445-
getLineNumbersStart({
446-
showLineNumbers: true,
447-
metaOptions: {
448-
showLineNumbers: 2,
449-
},
450-
}),
451-
).toMatchSnapshot();
452-
});
473+
describe('handles prop', () => {
474+
describe('combined with metaoptions', () => {
475+
it('set to true', () => {
476+
expect(
477+
getLineNumbersStart({
478+
showLineNumbers: true,
479+
metaOptions: {
480+
showLineNumbers: 2,
481+
},
482+
}),
483+
).toMatchSnapshot();
484+
});
453485

454-
it('set to false', () => {
455-
expect(
456-
getLineNumbersStart({
457-
showLineNumbers: false,
458-
metaOptions: {
459-
showLineNumbers: 2,
460-
},
461-
}),
462-
).toMatchSnapshot();
463-
});
486+
it('set to false', () => {
487+
expect(
488+
getLineNumbersStart({
489+
showLineNumbers: false,
490+
metaOptions: {
491+
showLineNumbers: 2,
492+
},
493+
}),
494+
).toMatchSnapshot();
495+
});
464496

465-
it('set to number', () => {
466-
expect(
467-
getLineNumbersStart({
468-
showLineNumbers: 10,
469-
metaOptions: {
470-
showLineNumbers: 2,
471-
},
472-
}),
473-
).toMatchSnapshot();
497+
it('set to number', () => {
498+
expect(
499+
getLineNumbersStart({
500+
showLineNumbers: 10,
501+
metaOptions: {
502+
showLineNumbers: 2,
503+
},
504+
}),
505+
).toMatchSnapshot();
506+
});
474507
});
475-
});
476508

477-
describe('standalone', () => {
478-
it('set to true', () => {
479-
expect(
480-
getLineNumbersStart({
481-
showLineNumbers: true,
482-
metaOptions: {
483-
showLineNumbers: 2,
484-
},
485-
}),
486-
).toMatchSnapshot();
509+
describe('standalone', () => {
510+
it('set to true', () => {
511+
expect(
512+
getLineNumbersStart({
513+
showLineNumbers: true,
514+
metaOptions: {
515+
showLineNumbers: 2,
516+
},
517+
}),
518+
).toMatchSnapshot();
519+
});
520+
521+
it('set to false', () => {
522+
expect(
523+
getLineNumbersStart({
524+
showLineNumbers: false,
525+
metaOptions: {
526+
showLineNumbers: 2,
527+
},
528+
}),
529+
).toMatchSnapshot();
530+
});
531+
532+
it('set to number', () => {
533+
expect(
534+
getLineNumbersStart({
535+
showLineNumbers: 10,
536+
metaOptions: {
537+
showLineNumbers: 2,
538+
},
539+
}),
540+
).toMatchSnapshot();
541+
});
487542
});
543+
});
488544

489-
it('set to false', () => {
490-
expect(
491-
getLineNumbersStart({
492-
showLineNumbers: false,
493-
metaOptions: {
494-
showLineNumbers: 2,
495-
},
496-
}),
497-
).toMatchSnapshot();
545+
describe('handles metadata', () => {
546+
describe('standalone', () => {
547+
it('set as flag', () => {
548+
expect(
549+
getLineNumbersStart({
550+
showLineNumbers: undefined,
551+
metaOptions: {
552+
showLineNumbers: true,
553+
},
554+
}),
555+
).toMatchSnapshot();
556+
});
557+
it('set with number', () => {
558+
expect(
559+
getLineNumbersStart({
560+
showLineNumbers: undefined,
561+
metaOptions: {
562+
showLineNumbers: 10,
563+
},
564+
}),
565+
).toMatchSnapshot();
566+
});
498567
});
499568

500-
it('set to number', () => {
501-
expect(
502-
getLineNumbersStart({
503-
showLineNumbers: 10,
504-
metaOptions: {
505-
showLineNumbers: 2,
506-
},
507-
}),
508-
).toMatchSnapshot();
569+
describe('combined with other options', () => {
570+
it('set as flag', () => {
571+
expect(
572+
getLineNumbersStart({
573+
showLineNumbers: undefined,
574+
metaOptions: {
575+
title: 'file.txt',
576+
showLineNumbers: true,
577+
noInline: true,
578+
},
579+
}),
580+
).toMatchSnapshot();
581+
});
582+
it('set with number', () => {
583+
expect(
584+
getLineNumbersStart({
585+
showLineNumbers: undefined,
586+
metaOptions: {
587+
title: 'file.txt',
588+
showLineNumbers: 10,
589+
noInline: true,
590+
},
591+
}),
592+
).toMatchSnapshot();
593+
});
509594
});
510595
});
511596
});
512597

513-
describe('handles metadata', () => {
514-
describe('standalone', () => {
515-
it('set as flag', () => {
516-
expect(
517-
getLineNumbersStart({
518-
showLineNumbers: undefined,
519-
metaOptions: {
520-
showLineNumbers: true,
521-
},
522-
}),
523-
).toMatchSnapshot();
524-
});
525-
it('set with number', () => {
526-
expect(
527-
getLineNumbersStart({
528-
showLineNumbers: undefined,
529-
metaOptions: {
530-
showLineNumbers: 10,
531-
},
532-
}),
533-
).toMatchSnapshot();
534-
});
598+
describe('from metastring', () => {
599+
it('parses flags as 1', () => {
600+
expect(
601+
getLineNumbersStart({
602+
showLineNumbers: undefined,
603+
metaOptions: parseCodeBlockMetaOptions(
604+
' showLineNumbers ',
605+
undefined,
606+
),
607+
}),
608+
).toMatchSnapshot();
535609
});
536610

537-
describe('combined with other options', () => {
538-
it('set as flag', () => {
539-
expect(
540-
getLineNumbersStart({
541-
showLineNumbers: undefined,
542-
metaOptions: {
543-
title: 'file.txt',
544-
showLineNumbers: true,
545-
noInline: true,
546-
},
547-
}),
548-
).toMatchSnapshot();
549-
});
550-
it('set with number', () => {
551-
expect(
552-
getLineNumbersStart({
553-
showLineNumbers: undefined,
554-
metaOptions: {
555-
title: 'file.txt',
556-
showLineNumbers: 10,
557-
noInline: true,
558-
},
559-
}),
560-
).toMatchSnapshot();
561-
});
611+
it('parses value', () => {
612+
expect(
613+
getLineNumbersStart({
614+
showLineNumbers: undefined,
615+
metaOptions: parseCodeBlockMetaOptions(
616+
' showLineNumbers=10 ',
617+
undefined,
618+
),
619+
}),
620+
).toMatchSnapshot();
562621
});
563622
});
564623
});

0 commit comments

Comments
 (0)