Skip to content

Commit

Permalink
Merge pull request #494 from nextcloud/fix/entry
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv committed Sep 14, 2022
2 parents 5bbe474 + 30575b5 commit a827499
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
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

0 comments on commit a827499

Please sign in to comment.