fix/34436-edit-backlog-date-focus-backlog-details-firefox-quirk
commit
a6c5c5385a
@ -0,0 +1,84 @@ |
||||
import { TestBed } from '@angular/core/testing'; |
||||
import { GlobalEditFormChangesTrackerService } from './global-edit-form-changes-tracker.service'; |
||||
import {EditFormComponent} from "core-app/modules/fields/edit/edit-form/edit-form.component"; |
||||
|
||||
describe('GlobalEditFormChangesTrackerService', () => { |
||||
let service:GlobalEditFormChangesTrackerService; |
||||
const createForm = (changed?:boolean) => { |
||||
return { |
||||
change: { |
||||
isEmpty: () => !changed |
||||
} |
||||
} as EditFormComponent; |
||||
}; |
||||
|
||||
beforeEach(() => { |
||||
TestBed.configureTestingModule({}); |
||||
service = TestBed.inject(GlobalEditFormChangesTrackerService); |
||||
}); |
||||
|
||||
it('should be created', () => { |
||||
expect(service).toBeTruthy(); |
||||
}); |
||||
|
||||
it('should report no changes when empty', () => { |
||||
expect(service.thereAreFormsWithUnsavedChanges).toBeFalse(); |
||||
}); |
||||
|
||||
it('should report no changes when one form has no changes', () => { |
||||
const form = createForm(); |
||||
|
||||
service.addToActiveForms(form); |
||||
|
||||
expect(service.thereAreFormsWithUnsavedChanges).toBeFalse(); |
||||
}); |
||||
|
||||
it('should report no changes when multiple forms have no changes', () => { |
||||
const form = createForm(); |
||||
const form2 = createForm(); |
||||
const form3 = createForm(); |
||||
|
||||
service.addToActiveForms(form); |
||||
service.addToActiveForms(form2); |
||||
service.addToActiveForms(form3); |
||||
|
||||
expect(service.thereAreFormsWithUnsavedChanges).toBeFalse(); |
||||
}); |
||||
|
||||
it('should report no changes when the only form with changes is removed', () => { |
||||
const form = createForm(true); |
||||
|
||||
service.addToActiveForms(form); |
||||
service.removeFromActiveForms(form); |
||||
|
||||
expect(service.thereAreFormsWithUnsavedChanges).toBeFalse(); |
||||
}); |
||||
|
||||
it('should report changes when one form has changes', () => { |
||||
const form = createForm(true); |
||||
|
||||
service.addToActiveForms(form); |
||||
|
||||
expect(service.thereAreFormsWithUnsavedChanges).toBeTrue(); |
||||
}); |
||||
|
||||
it('should report forms with changes when multiple form have changes', () => { |
||||
const form = createForm(true); |
||||
const form2 = createForm(true); |
||||
const form3 = createForm(); |
||||
|
||||
service.addToActiveForms(form); |
||||
service.addToActiveForms(form2); |
||||
service.addToActiveForms(form3); |
||||
|
||||
expect(service.thereAreFormsWithUnsavedChanges).toBeTrue(); |
||||
}); |
||||
|
||||
it('should call thereAreFormsWithUnsavedChangesSpy on beforeunload', () => { |
||||
const thereAreFormsWithUnsavedChangesSpy = spyOnProperty(service, 'thereAreFormsWithUnsavedChanges', 'get'); |
||||
|
||||
window.dispatchEvent(new Event('beforeunload')); |
||||
|
||||
expect(thereAreFormsWithUnsavedChangesSpy).toHaveBeenCalled(); |
||||
}); |
||||
}); |
@ -0,0 +1,37 @@ |
||||
import { Injectable } from '@angular/core'; |
||||
import {EditFormComponent} from "core-app/modules/fields/edit/edit-form/edit-form.component"; |
||||
import {I18nService} from "core-app/modules/common/i18n/i18n.service"; |
||||
|
||||
@Injectable({ |
||||
providedIn: 'root' |
||||
}) |
||||
export class GlobalEditFormChangesTrackerService { |
||||
private activeForms = new Map<EditFormComponent, boolean>(); |
||||
|
||||
get thereAreFormsWithUnsavedChanges () { |
||||
return Array.from(this.activeForms.keys()).some(form => { |
||||
return !form.change.isEmpty(); |
||||
}); |
||||
} |
||||
|
||||
constructor( |
||||
private i18nService:I18nService, |
||||
) { |
||||
// Global beforeunload hook to show a data loss warn
|
||||
// when the user clicks on a link out of the Angular app
|
||||
window.addEventListener('beforeunload', (event) => { |
||||
if (this.thereAreFormsWithUnsavedChanges) { |
||||
event.preventDefault(); |
||||
event.returnValue = this.i18nService.t('js.work_packages.confirm_edit_cancel'); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public addToActiveForms(form:EditFormComponent) { |
||||
this.activeForms.set(form, true); |
||||
} |
||||
|
||||
public removeFromActiveForms(form:EditFormComponent) { |
||||
this.activeForms.delete(form); |
||||
} |
||||
} |
Loading…
Reference in new issue