Skip to content

Commit

Permalink
Fix message on create duplicates (#292)
Browse files Browse the repository at this point in the history
* fix: error handling on create a duplicate

* fix: tests
  • Loading branch information
HenryT-CG authored Nov 3, 2024
1 parent d818f69 commit 8d70343
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 49 deletions.
67 changes: 20 additions & 47 deletions src/app/theme/theme-designer/theme-designer.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,35 +330,27 @@ describe('ThemeDesignerComponent', () => {
component.actions$?.subscribe((actions) => {
const updateThemeAction = actions[1]
updateThemeAction.actionCallback()
expect(msgServiceSpy.error).toHaveBeenCalledOnceWith({ summaryKey: 'ACTIONS.EDIT.MESSAGE.CHANGE_NOK' })

expect(msgServiceSpy.error).toHaveBeenCalledOnceWith({ summaryKey: 'VALIDATION.ERRORS.FORM_INVALID' })
done()
})
})

it('should display error when updating theme call fails', (done: DoneFn) => {
const themeData = {
id: 'id',
description: 'desc',
logoUrl: 'logo_url',
faviconUrl: 'fav_url',
name: 'themeName',
properties: {
font: { 'font-family': 'myFont' },
general: { 'primary-color': 'rgb(0,0,0)' }
}
}
const themeResponse = {
resource: themeData
it('should display error when theme data are not ready', (done: DoneFn) => {
component.fontForm.patchValue({ 'font-family': 'updatedFont' })
component.propertiesForm.patchValue({ 'primary-color': 'rgb(255,255,255)' })
// display name is missing
const newBasicData = {
name: 'updatedName',
description: 'updatedDesc',
logoUrl: 'updated_logo_url',
faviconUrl: 'updated_favicon_url'
}
themeApiSpy.getThemeByName.and.returnValue(of(themeResponse) as any)
themeApiSpy.updateTheme.and.returnValue(throwError(() => new Error()))
component.basicForm.patchValue(newBasicData)

component.actions$?.subscribe((actions) => {
const updateThemeAction = actions[1]
updateThemeAction.actionCallback()
expect(msgServiceSpy.error).toHaveBeenCalledOnceWith({ summaryKey: 'ACTIONS.EDIT.MESSAGE.CHANGE_NOK' })

expect(msgServiceSpy.error).toHaveBeenCalledOnceWith({ summaryKey: 'VALIDATION.ERRORS.FORM_INVALID' })
done()
})
})
Expand All @@ -384,6 +376,7 @@ describe('ThemeDesignerComponent', () => {
component.generalForm.patchValue({ 'primary-color': 'rgb(255,255,255)' })
const newBasicData = {
name: 'updatedName',
displayName: 'updatedDisplayName',
description: 'updatedDesc',
logoUrl: 'updated_logo_url',
faviconUrl: 'updated_favicon_url'
Expand Down Expand Up @@ -444,7 +437,7 @@ describe('ThemeDesignerComponent', () => {

it('should display theme already exists message on theme save failure', () => {
themeApiSpy.createTheme.and.returnValue(
throwError(() => new HttpErrorResponse({ error: { key: 'PERSIST_ENTITY_FAILED' } }))
throwError(() => new HttpErrorResponse({ error: { errorCode: 'PERSIST_ENTITY_FAILED' } }))
)

component.saveAsTheme('myTheme', 'myDisplayName')
Expand Down Expand Up @@ -480,19 +473,9 @@ describe('ThemeDesignerComponent', () => {
faviconUrl: 'new_favicon_url'
}
component.basicForm.patchValue(newBasicData)
component.fontForm.patchValue({
'font-family': 'newFont'
})
component.generalForm.patchValue({
'primary-color': 'rgb(255,255,255)'
})
themeApiSpy.createTheme.and.returnValue(
of({
resource: {
name: 'myTheme'
}
}) as any
)
component.fontForm.patchValue({ 'font-family': 'newFont' })
component.generalForm.patchValue({ 'primary-color': 'rgb(255,255,255)' })
themeApiSpy.createTheme.and.returnValue(of({ resource: { name: 'myTheme' } }) as any)
component.changeMode = 'EDIT'

component.saveAsTheme('myTheme', 'myDisplayName')
Expand Down Expand Up @@ -590,19 +573,9 @@ describe('ThemeDesignerComponent', () => {
faviconUrl: 'new_favicon_url'
}
component.basicForm.patchValue(newBasicData)
component.fontForm.patchValue({
'font-family': 'newFont'
})
component.generalForm.patchValue({
'primary-color': 'rgb(255,255,255)'
})
themeApiSpy.createTheme.and.returnValue(
of({
resource: {
name: 'myTheme'
}
}) as any
)
component.fontForm.patchValue({ 'font-family': 'newFont' })
component.generalForm.patchValue({ 'primary-color': 'rgb(255,255,255)' })
themeApiSpy.createTheme.and.returnValue(of({ resource: { name: 'myTheme' } }) as any)
component.changeMode = 'NEW'

component.saveAsTheme('myTheme', 'myDisplayName')
Expand Down
5 changes: 3 additions & 2 deletions src/app/theme/theme-designer/theme-designer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export class ThemeDesignerComponent implements OnInit {

public onSaveTheme(): void {
if (this.basicForm.invalid || this.propertiesForm.invalid) {
this.msgService.error({ summaryKey: 'ACTIONS.EDIT.MESSAGE.CHANGE_NOK' })
this.msgService.error({ summaryKey: 'VALIDATION.ERRORS.FORM_INVALID' })
return
}
const newTheme: ThemeUpdateCreate = { ...this.basicForm.value }
Expand Down Expand Up @@ -394,10 +394,11 @@ export class ThemeDesignerComponent implements OnInit {
this.msgService.success({ summaryKey: 'ACTIONS.CREATE.MESSAGE.CREATE_OK' })
},
error: (err) => {
console.error(err)
this.msgService.error({
summaryKey: 'ACTIONS.CREATE.MESSAGE.CREATE_NOK',
detailKey:
err?.error?.key && err?.error?.key === 'PERSIST_ENTITY_FAILED'
err?.error?.errorCode && err?.error?.errorCode === 'PERSIST_ENTITY_FAILED'
? 'ACTIONS.CREATE.MESSAGE.THEME_ALREADY_EXISTS'
: err.error
})
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
},
"VALIDATION": {
"ERRORS": {
"FORM_INVALID": "Die Formulardaten sind nicht bereit zum Speichern des Themes.",
"PARSE_ERROR": "Parserfehler: Die enthaltene Struktur entspricht nicht dem erwarteten Format.",
"INTERNAL_ERROR": "Interner Fehler",
"EMPTY_REQUIRED_FIELD": "Dieses Feld ist ein Pflichtfeld.",
Expand Down
1 change: 1 addition & 0 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
},
"VALIDATION": {
"ERRORS": {
"FORM_INVALID": "The form data are not ready to save the Theme.",
"PARSE_ERROR": "Parse error: The contained structure does not match the expected format.",
"INTERNAL_ERROR": "Internal error",
"EMPTY_REQUIRED_FIELD": "This field is required.",
Expand Down

0 comments on commit 8d70343

Please sign in to comment.