Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix templateName usage and errors strings #494

Merged
merged 1 commit into from Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 deletions __tests__/newFileMenu.spec.ts
Expand Up @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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({
Expand All @@ -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')
})
})

Expand Down
28 changes: 19 additions & 9 deletions lib/newFileMenu.ts
Expand Up @@ -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
/**
Expand Down Expand Up @@ -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) {
Expand Down