diff --git a/__tests__/newFileMenu.spec.ts b/__tests__/newFileMenu.spec.ts index f42404af..6b6548f1 100644 --- a/__tests__/newFileMenu.spec.ts +++ b/__tests__/newFileMenu.spec.ts @@ -119,7 +119,7 @@ describe('NewFileMenu addEntry', () => { iconClass: 'icon-filetype-text', handler: () => {} } as unknown as Entry) - }).toThrowError('Invalid entry property') + }).toThrowError('Invalid id or displayName property') expect(() => { newFileMenu.registerEntry({ @@ -129,7 +129,7 @@ describe('NewFileMenu addEntry', () => { iconClass: 'icon-filetype-text', handler: () => {} } as unknown as Entry) - }).toThrowError('Invalid entry property') + }).toThrowError('Invalid id or displayName property') expect(() => { newFileMenu.registerEntry({ @@ -139,7 +139,7 @@ describe('NewFileMenu addEntry', () => { iconClass: 'icon-filetype-text', handler: () => {} } as unknown as Entry) - }).toThrowError('Invalid entry property') + }).toThrowError('Invalid templateName property') expect(() => { newFileMenu.registerEntry({ @@ -149,7 +149,7 @@ describe('NewFileMenu addEntry', () => { iconClass: 123456, handler: () => {} } as unknown as Entry) - }).toThrowError('Invalid entry property') + }).toThrowError('Invalid icon provided') expect(() => { newFileMenu.registerEntry({ @@ -159,7 +159,7 @@ describe('NewFileMenu addEntry', () => { iconSvgInline: 123456, handler: () => {} } as unknown as Entry) - }).toThrowError('Invalid entry property') + }).toThrowError('Invalid icon provided') expect(() => { newFileMenu.registerEntry({ @@ -170,7 +170,7 @@ describe('NewFileMenu addEntry', () => { if: true, handler: () => {} } as unknown as Entry) - }).toThrowError('Invalid entry, if must be a valid function') + }).toThrowError('Invalid if property') expect(() => { newFileMenu.registerEntry({ @@ -180,7 +180,15 @@ describe('NewFileMenu addEntry', () => { iconClass: 'icon-filetype-text', handler: 'handler' } as unknown as Entry) - }).toThrowError('Invalid entry handler') + }).toThrowError('Invalid handler property') + + expect(() => { + newFileMenu.registerEntry({ + id: 'empty-file', + displayName: '123456', + iconClass: 'icon-filetype-text', + } as unknown as Entry) + }).toThrowError('At least a templateName or a handler must be provided') }) }) diff --git a/lib/newFileMenu.ts b/lib/newFileMenu.ts index 2055816b..a06f1387 100644 --- a/lib/newFileMenu.ts +++ b/lib/newFileMenu.ts @@ -28,7 +28,7 @@ export interface Entry { /** Translatable string displayed in the menu */ displayName: string // Default new file name - templateName: string + templateName?: string // Condition wether this entry is shown or not if?: (context: Object) => Boolean /** @@ -81,24 +81,34 @@ export class NewFileMenu { } private validateEntry(entry: Entry) { - if (!entry.id || !entry.displayName || !entry.templateName || !(entry.iconSvgInline || entry.iconClass) ||!entry.handler) { + if (!entry.id || !entry.displayName || !(entry.iconSvgInline || entry.iconClass)) { throw new Error('Invalid entry') } if (typeof entry.id !== 'string' - || typeof entry.displayName !== 'string' - || typeof entry.templateName !== 'string' - || (entry.iconClass && typeof entry.iconClass !== 'string') + || typeof entry.displayName !== 'string') { + throw new Error('Invalid id or displayName property') + } + + if ((entry.iconClass && typeof entry.iconClass !== 'string') || (entry.iconSvgInline && typeof entry.iconSvgInline !== 'string')) { - throw new Error('Invalid entry property') + throw new Error('Invalid icon provided') } if (entry.if !== undefined && typeof entry.if !== 'function') { - throw new Error('Invalid entry, if must be a valid function') + throw new Error('Invalid if property') + } + + if (entry.templateName && typeof entry.templateName !== 'string') { + throw new Error('Invalid templateName property') + } + + if (entry.handler && typeof entry.handler !== 'function') { + throw new Error('Invalid handler property') } - if (typeof entry.handler !== 'function') { - throw new Error('Invalid entry handler') + if (!entry.templateName && !entry.handler) { + throw new Error('At least a templateName or a handler must be provided') } if (this.getEntryIndex(entry.id) !== -1) {