Skip to content

Commit

Permalink
Fix: save on copy (#316)
Browse files Browse the repository at this point in the history
* fix: save a new parameter on copy action

* fix: simplify code
  • Loading branch information
HenryT-CG authored Dec 22, 2024
1 parent 6ae4f0f commit d7a5f42
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<p-dialog
[header]="'DIALOG.DETAIL.' + this.changeMode + '.HEADER' | translate"
[header]="'DIALOG.DETAIL.' + changeMode + '.HEADER' | translate"
[(visible)]="displayDialog"
(onHide)="onDialogHide()"
[showHeader]="true"
[baseZIndex]="10000"
[modal]="true"
[closable]="true"
[draggable]="true"
Expand Down Expand Up @@ -285,18 +283,18 @@
<ng-template pTemplate="footer">
<div class="flex flex-wrap justify-content-end gap-2 mb-1">
<p-button
*ngIf="changeMode === 'VIEW'"
*ngIf="exceptionKey || changeMode === 'VIEW'"
id="am_detail_action_close"
icon="pi pi-times"
(onClick)="onDialogHide()"
[label]="'ACTIONS.CLOSE' | translate"
[ariaLabel]="'ACTIONS.CLOSE' | translate"
[pTooltip]="'ACTIONS.TOOLTIPS.CLOSE' | translate"
[label]="'ACTIONS.NAVIGATION.CLOSE' | translate"
[ariaLabel]="'ACTIONS.NAVIGATION.CLOSE' | translate"
[pTooltip]="'ACTIONS.NAVIGATION.CLOSE.TOOLTIP' | translate"
tooltipPosition="top"
tooltipEvent="hover"
></p-button>
<p-button
*ngIf="changeMode !== 'VIEW'"
*ngIf="!(exceptionKey || changeMode === 'VIEW')"
id="am_detail_action_cancel"
icon="pi pi-times"
(onClick)="onDialogHide()"
Expand All @@ -307,7 +305,7 @@
tooltipEvent="hover"
></p-button>
<p-button
*ngIf="changeMode !== 'VIEW'"
*ngIf="!(exceptionKey || changeMode === 'VIEW')"
id="am_detail_action_save"
icon="pi pi-save"
(onClick)="onSave()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { NO_ERRORS_SCHEMA } from '@angular/core'
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
import { provideHttpClient, HttpClient } from '@angular/common/http'
import { provideHttpClientTesting } from '@angular/common/http/testing'
import { FormControl, FormGroup } from '@angular/forms'
import { TranslateLoader, TranslateModule } from '@ngx-translate/core'
import { of, throwError } from 'rxjs'
import { FormControl, FormGroup } from '@angular/forms'

import { AppStateService, UserService } from '@onecx/angular-integration-interface'
import { createTranslateLoader, PortalMessageService } from '@onecx/portal-integration-angular'
Expand All @@ -19,8 +19,8 @@ import {
import { AnnouncementDetailComponent, dateRangeValidator } from './announcement-detail.component'

const announcement: Announcement = {
id: 'id',
modificationCount: 0,
id: 'id',
title: 'title',
content: 'content',
productName: 'productName',
Expand Down Expand Up @@ -72,15 +72,16 @@ describe('AnnouncementDetailComponent', () => {
providers: [
provideHttpClient(),
provideHttpClientTesting(),
{ provide: UserService, useValue: mockUserService },
{ provide: PortalMessageService, useValue: msgServiceSpy },
{ provide: AnnouncementInternalAPIService, useValue: apiServiceSpy },
{ provide: UserService, useValue: mockUserService }
{ provide: AnnouncementInternalAPIService, useValue: apiServiceSpy }
]
}).compileComponents()
msgServiceSpy.success.calls.reset()
msgServiceSpy.error.calls.reset()
msgServiceSpy.info.calls.reset()
msgServiceSpy.warning.calls.reset()
// to spy data: reset
apiServiceSpy.getAnnouncementById.calls.reset()
apiServiceSpy.createAnnouncement.calls.reset()
apiServiceSpy.updateAnnouncementById.calls.reset()
Expand All @@ -100,17 +101,19 @@ describe('AnnouncementDetailComponent', () => {
component.formGroup.reset()
})

it('should create', () => {
expect(component).toBeTruthy()
})

it('should create but not initialize if dialog is not open', () => {
expect(component).toBeTruthy()
component.displayDialog = false
component.ngOnChanges()
describe('construction', () => {
it('should create', () => {
expect(component).toBeTruthy()
})
})

describe('ngOnChange - init form', () => {
it('should create but not initialize if dialog is not open', () => {
expect(component).toBeTruthy()
component.displayDialog = false
component.ngOnChanges()
})

describe('VIEW', () => {
it('should reject initializing if dialog is not open', () => {
apiServiceSpy.getAnnouncementById.and.returnValue(of(announcement))
Expand Down Expand Up @@ -141,10 +144,9 @@ describe('AnnouncementDetailComponent', () => {
component.ngOnChanges()

expect(apiServiceSpy.getAnnouncementById).toHaveBeenCalled()
expect(component.loading).toBeFalse()
expect(component.formGroup.disabled).toBeTrue()
expect(component.formGroup.controls['title'].value).toBe(announcement.title)
expect(component.loading).toBeFalse()
//expect(component.productOptions).toEqual(allProductsSI)
})

it('should prepare viewing an announcement - failed: missing id', () => {
Expand Down Expand Up @@ -185,6 +187,7 @@ describe('AnnouncementDetailComponent', () => {
component.ngOnChanges()

expect(apiServiceSpy.getAnnouncementById).toHaveBeenCalled()
expect(component.loading).toBeFalse()
expect(component.formGroup.enabled).toBeTrue()
expect(component.formGroup.controls['title'].value).toEqual(announcement.title)
expect(component.formGroup.controls['content'].value).toEqual(announcement.content)
Expand Down Expand Up @@ -239,6 +242,8 @@ describe('AnnouncementDetailComponent', () => {
component.ngOnChanges()

expect(component.formGroup.reset).toHaveBeenCalled()
expect(component.formGroup.enabled).toBeTrue()
expect(component.formGroup.controls['title'].value).toBe(null)
// check default values
expect(component.formGroup.controls['priority'].value).toEqual(AnnouncementPriorityType.Normal)
expect(component.formGroup.controls['status'].value).toEqual(AnnouncementStatus.Inactive)
Expand All @@ -247,18 +252,20 @@ describe('AnnouncementDetailComponent', () => {
})

describe('COPY', () => {
it('should prepare copying an announcement - start with data from other announcement', () => {
it('should prepare copying an announcement - use data from other announcement', () => {
component.changeMode = 'COPY'
component.announcement = announcement

component.ngOnChanges()

expect(apiServiceSpy.getAnnouncementById).not.toHaveBeenCalled()
expect(component.formGroup.enabled).toBeTrue()
expect(component.formGroup.controls['title'].value).toBe(announcement.title)
})

it('should prepare copying an announcement - without date values', () => {
component.changeMode = 'COPY'
component.announcement = { ...announcement, id: undefined, startDate: undefined, endDate: undefined }
component.announcement = { ...announcement, startDate: undefined, endDate: undefined }

component.ngOnChanges()

Expand All @@ -269,7 +276,7 @@ describe('AnnouncementDetailComponent', () => {
})
})

describe('saving', () => {
describe('onSave - creating and updating a parameter', () => {
describe('CREATE', () => {
it('should create an announcement', () => {
apiServiceSpy.createAnnouncement.and.returnValue(of({}))
Expand All @@ -293,13 +300,26 @@ describe('AnnouncementDetailComponent', () => {
component.onSave()

expect(component.formGroup.valid).toBeTrue()
expect(console.error).toHaveBeenCalledWith('createAnnouncement', errorResponse)
expect(msgServiceSpy.error).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.CREATE.MESSAGE.NOK' })
expect(console.error).toHaveBeenCalledWith('createAnnouncement', errorResponse)
})
})

describe('COPY', () => {
it('should create a parameter based on another', () => {
apiServiceSpy.createAnnouncement.and.returnValue(of({}))
component.changeMode = 'COPY'
spyOn(component.hideDialogAndChanged, 'emit')
component.formGroup = formGroup

component.onSave()

expect(msgServiceSpy.success).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.CREATE.MESSAGE.OK' })
expect(component.hideDialogAndChanged.emit).toHaveBeenCalledWith(true)
})
})
describe('EDIT', () => {
it('should update an announcement', () => {
it('should update an announcement - successful', () => {
apiServiceSpy.updateAnnouncementById.and.returnValue(of({}))
component.changeMode = 'EDIT'
component.announcement = announcement
Expand All @@ -323,12 +343,44 @@ describe('AnnouncementDetailComponent', () => {

component.onSave()

expect(console.error).toHaveBeenCalledWith('updateAnnouncementById', errorResponse)
expect(msgServiceSpy.error).toHaveBeenCalledWith({ summaryKey: 'ACTIONS.EDIT.MESSAGE.NOK' })
expect(console.error).toHaveBeenCalledWith('updateAnnouncementById', errorResponse)
})
})
})

/*
* UI ACTIONS
*/
describe('Extra UI actions', () => {
describe('Closing dialog', () => {
it('should close the dialog if user triggers hiding', () => {
spyOn(component.hideDialogAndChanged, 'emit')
component.onDialogHide()

expect(component.hideDialogAndChanged.emit).toHaveBeenCalledWith(false)
})
})
})

/**
* Language tests
*/
it('should set a German date format', () => {
expect(component.dateFormat).toEqual('dd.mm.yy')
})

it('should set default date format', () => {
mockUserService.lang$.getValue.and.returnValue('en')
fixture = TestBed.createComponent(AnnouncementDetailComponent)
component = fixture.componentInstance
fixture.detectChanges()
expect(component.dateFormat).toEqual('mm/dd/yy')
})

/**
* Extras
*/
describe('form invalid', () => {
it('should display warning when trying to save an invalid announcement', () => {
component.formGroup = formGroup
Expand Down Expand Up @@ -387,33 +439,4 @@ describe('AnnouncementDetailComponent', () => {
expect(dateFormGroup.errors).toEqual(null)
})
})

/*
* UI ACTIONS
*/
describe('Extra UI actions', () => {
describe('Closing dialog', () => {
it('should close the dialog if user triggers hiding', () => {
spyOn(component.hideDialogAndChanged, 'emit')
component.onDialogHide()

expect(component.hideDialogAndChanged.emit).toHaveBeenCalledWith(false)
})
})
})

/**
* Language tests
*/
it('should set a German date format', () => {
expect(component.dateFormat).toEqual('dd.mm.yy')
})

it('should set default date format', () => {
mockUserService.lang$.getValue.and.returnValue('en')
fixture = TestBed.createComponent(AnnouncementDetailComponent)
component = fixture.componentInstance
fixture.detectChanges()
expect(component.dateFormat).toEqual('mm/dd/yy')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { SelectItem } from 'primeng/api'

import { PortalMessageService, UserService } from '@onecx/portal-integration-angular'
import {
CreateAnnouncementRequest,
UpdateAnnouncementRequest,
Announcement,
AnnouncementPriorityType,
AnnouncementStatus,
Expand Down Expand Up @@ -82,6 +80,7 @@ export class AnnouncementDetailComponent implements OnChanges {

public ngOnChanges() {
if (!this.displayDialog) return
this.exceptionKey = undefined
// matching mode and given data?
if ('CREATE' === this.changeMode && this.announcement) return
if (['EDIT', 'VIEW'].includes(this.changeMode))
Expand Down Expand Up @@ -159,7 +158,7 @@ export class AnnouncementDetailComponent implements OnChanges {
this.announcementApi
.updateAnnouncementById({
id: this.announcement?.id,
updateAnnouncementRequest: this.submitFormValues() as UpdateAnnouncementRequest
updateAnnouncementRequest: this.formGroup.value
})
.subscribe({
next: () => {
Expand All @@ -172,10 +171,10 @@ export class AnnouncementDetailComponent implements OnChanges {
}
})
}
if (this.changeMode === 'CREATE') {
if (['COPY', 'CREATE'].includes(this.changeMode)) {
this.announcementApi
.createAnnouncement({
createAnnouncementRequest: this.submitFormValues() as CreateAnnouncementRequest
createAnnouncementRequest: this.formGroup.value
})
.subscribe({
next: () => {
Expand All @@ -190,10 +189,6 @@ export class AnnouncementDetailComponent implements OnChanges {
}
}

private submitFormValues(): Announcement {
return { ...this.formGroup.value } as Announcement
}

/****************************************************************************
* SERVER responses & internal
*/
Expand Down
5 changes: 1 addition & 4 deletions src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@
},
"CANCEL": "Abbrechen",
"CHOOSE": "Auswählen",
"CLOSE": "Schließen",
"CONFIRM": "Bestätigen",
"LOADING": "...wird geladen",
"RELOAD": "Neuladen",
"SAVE": "Speichern",
Expand All @@ -89,9 +87,8 @@
"CANCEL": "Die Aktion abbrechen",
"CANCEL_AND_CLOSE": "Abbrechen und Dialog schließen",
"CANCEL_WITHOUT_SAVE": "Die Aktion ohne Speichern abbrechen",
"CLOSE": "Dialog schließen",
"SAVE": "Die Änderungen speichern",
"SAVE_AS": "Speichern als neues {{TYPE}}"
"SAVE_AS": "Speichern als neuen Parameter"
}
},
"INTERNAL": {
Expand Down
5 changes: 1 addition & 4 deletions src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@
},
"CANCEL": "Cancel",
"CHOOSE": "Choose",
"CLOSE": "Close",
"CONFIRM": "Confirm",
"LOADING": "...is loading",
"RELOAD": "Reload",
"SAVE": "Save",
Expand All @@ -89,9 +87,8 @@
"CANCEL": "Cancel action",
"CANCEL_AND_CLOSE": "Cancel and close dialog",
"CANCEL_WITHOUT_SAVE": "Cancel action without saving",
"CLOSE": "Close dialog",
"SAVE": "Save changes",
"SAVE_AS": "Save as new {{TYPE}}"
"SAVE_AS": "Save as new Announcement"
}
},
"INTERNAL": {
Expand Down

0 comments on commit d7a5f42

Please sign in to comment.