| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import type { ITemplate } from '@growi/core/dist/interfaces/template';
- import { mock } from 'vitest-mock-extended';
- import { useFormatter } from './use-formatter';
- const mocks = vi.hoisted(() => {
- return {
- useCurrentPagePathMock: vi.fn(() => { return {} }),
- };
- });
- vi.mock('~/stores/page', () => {
- return { useCurrentPagePath: mocks.useCurrentPagePathMock };
- });
- describe('useFormatter', () => {
- describe('format()', () => {
- it('returns an empty string when the argument is undefined', () => {
- // setup
- const mastacheMock = {
- render: vi.fn(),
- };
- vi.doMock('mustache', () => mastacheMock);
- // when
- const { format } = useFormatter();
- // call with undefined
- const markdown = format(undefined);
- // then
- expect(markdown).toBe('');
- expect(mastacheMock.render).not.toHaveBeenCalled();
- });
- });
- it('returns markdown as-is when mustache.render throws an error', () => {
- // setup
- const mastacheMock = {
- render: vi.fn(() => { throw new Error() }),
- };
- vi.doMock('mustache', () => mastacheMock);
- // when
- const { format } = useFormatter();
- const template = mock<ITemplate>();
- template.markdown = 'markdown body';
- const markdown = format(template);
- // then
- expect(markdown).toBe('markdown body');
- });
- it('returns markdown formatted when currentPagePath is undefined', () => {
- // when
- const { format } = useFormatter();
- const template = mock<ITemplate>();
- template.markdown = `
- title: {{{title}}}{{^title}}(empty){{/title}}
- path: {{{path}}}
- `;
- const markdown = format(template);
- // then
- expect(markdown).toBe(`
- title: (empty)
- path: /
- `);
- });
- it('returns markdown formatted', () => {
- // setup
- mocks.useCurrentPagePathMock.mockImplementation(() => {
- return { data: '/Sandbox' };
- });
- // 2023/5/31 15:01:xx
- vi.setSystemTime(new Date(2023, 4, 31, 15, 1));
- // when
- const { format } = useFormatter();
- const template = mock<ITemplate>();
- template.markdown = `
- title: {{{title}}}
- path: {{{path}}}
- date: {{yyyy}}/{{MM}}/{{dd}} {{HH}}:{{mm}}
- `;
- const markdown = format(template);
- // then
- expect(markdown).toBe(`
- title: Sandbox
- path: /Sandbox
- date: 2023/05/31 15:01
- `);
- });
- });
|