Skip to content

Commit 4d7a27b

Browse files
committed
fix(WEB-1038): include template id on update
1 parent a2cbe85 commit 4d7a27b

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Copyright since 2025 Mifos Initiative
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
import { TestBed } from '@angular/core/testing';
10+
import { FormBuilder } from '@angular/forms';
11+
import { ActivatedRoute, Router } from '@angular/router';
12+
import { of } from 'rxjs';
13+
14+
import { ThemingService } from 'app/shared/theme-toggle/theming.service';
15+
import { TemplatesService } from '../templates.service';
16+
import { CreateEditComponent } from './create-edit-template.component';
17+
18+
describe('CreateEditComponent', () => {
19+
const templateData = {
20+
entities: [
21+
{ id: 0, name: 'client' },
22+
{ id: 1, name: 'loan' }
23+
],
24+
types: [
25+
{ id: 0, name: 'Document' },
26+
{ id: 2, name: 'SMS' }
27+
],
28+
template: {
29+
id: 76,
30+
entity: 'client',
31+
type: 'SMS',
32+
name: 'SELF_SERVICE_LOGIN_SUCCESS_EMAIL_SUBJECT',
33+
text: '<p>Original</p>',
34+
mappers: [] as any[]
35+
}
36+
};
37+
38+
let router: { navigate: jest.Mock };
39+
let templatesService: { createTemplate: jest.Mock; updateTemplate: jest.Mock };
40+
41+
function createComponent(mode: 'create' | 'edit', data = templateData): CreateEditComponent {
42+
TestBed.resetTestingModule();
43+
router = { navigate: jest.fn() };
44+
templatesService = {
45+
createTemplate: jest.fn().mockReturnValue(of({ resourceId: 77 })),
46+
updateTemplate: jest.fn().mockReturnValue(of({ resourceId: data.template?.id }))
47+
};
48+
49+
TestBed.configureTestingModule({
50+
providers: [
51+
FormBuilder,
52+
{ provide: ActivatedRoute, useValue: { data: of({ templateData: data, mode }) } },
53+
{ provide: Router, useValue: router },
54+
{ provide: TemplatesService, useValue: templatesService },
55+
{ provide: ThemingService, useValue: { theme: of('light-theme') } }]
56+
});
57+
58+
let component: CreateEditComponent;
59+
TestBed.runInInjectionContext(() => {
60+
component = new CreateEditComponent();
61+
});
62+
component.ngOnInit();
63+
return component;
64+
}
65+
66+
it('includes the template id in the update payload on edit submit', () => {
67+
const component = createComponent('edit');
68+
jest.spyOn(component, 'getEditorContent').mockReturnValue('<p>Updated</p>');
69+
70+
component.submit();
71+
72+
expect(templatesService.updateTemplate).toHaveBeenCalledWith(
73+
{
74+
id: templateData.template.id,
75+
entity: 0,
76+
type: 2,
77+
name: 'SELF_SERVICE_LOGIN_SUCCESS_EMAIL_SUBJECT',
78+
text: '<p>Updated</p>',
79+
mappers: []
80+
},
81+
templateData.template.id
82+
);
83+
expect(templatesService.createTemplate).not.toHaveBeenCalled();
84+
});
85+
86+
it('does not add an id to the create payload', () => {
87+
const component = createComponent('create');
88+
component.templateForm.patchValue({
89+
entity: 1,
90+
type: 2,
91+
name: 'New Template',
92+
text: '<p>Draft</p>'
93+
});
94+
jest.spyOn(component, 'getEditorContent').mockReturnValue('<p>Created</p>');
95+
96+
component.submit();
97+
98+
const [payload] = templatesService.createTemplate.mock.calls[0];
99+
expect(payload).not.toHaveProperty('id');
100+
expect(payload).toMatchObject({
101+
entity: 1,
102+
type: 2,
103+
name: 'New Template',
104+
text: '<p>Created</p>'
105+
});
106+
expect(templatesService.updateTemplate).not.toHaveBeenCalled();
107+
});
108+
});

src/app/templates/create-edit-template/create-edit-template.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export class CreateEditComponent implements OnInit {
286286
);
287287
});
288288
} else {
289+
template.id = this.templateData.template.id;
289290
this.templateService.updateTemplate(template, this.templateData.template.id).subscribe(() => {
290291
this.router.navigate(['../'], { relativeTo: this.route });
291292
});

0 commit comments

Comments
 (0)