Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

/** Angular Imports */
import { Component, OnInit, inject } from '@angular/core';
import { UntypedFormGroup, UntypedFormBuilder, Validators, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { UntypedFormGroup, UntypedFormBuilder, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';

/** Custom Services */
import { ClientsService } from 'app/clients/clients.service';
import { Dates } from 'app/core/utils/dates';
import { SettingsService } from 'app/settings/settings.service';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
import { buildCloseClientPayload } from './close-client.utils';

/**
* Close Client Component
Expand All @@ -29,12 +30,12 @@ import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
]
})
export class CloseClientComponent implements OnInit {
private formBuilder = inject(UntypedFormBuilder);
private clientsService = inject(ClientsService);
private dateUtils = inject(Dates);
private route = inject(ActivatedRoute);
private router = inject(Router);
private settingsService = inject(SettingsService);
private readonly formBuilder = inject(UntypedFormBuilder);
private readonly clientsService = inject(ClientsService);
private readonly dateUtils = inject(Dates);
private readonly route = inject(ActivatedRoute);
private readonly router = inject(Router);
private readonly settingsService = inject(SettingsService);

/** Minimum date allowed. */
minDate = new Date(2000, 0, 1);
Expand Down Expand Up @@ -87,18 +88,9 @@ export class CloseClientComponent implements OnInit {
* Submits the form and closes the client.
*/
submit() {
const closeClientFormData = this.closeClientForm.value;
const locale = this.settingsService.language.code;
const dateFormat = this.settingsService.dateFormat;
const prevClosedDate: Date = this.closeClientForm.value.closureDate;
if (closeClientFormData.closureDate instanceof Date) {
closeClientFormData.closureDate = this.dateUtils.formatDate(prevClosedDate, dateFormat);
}
const data = {
...closeClientFormData,
dateFormat,
locale
};
const data = buildCloseClientPayload(this.closeClientForm.value, this.dateUtils, locale, dateFormat);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
this.clientsService.executeClientCommand(this.clientId, 'close', data).subscribe(() => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
this.router.navigate(['../../'], { relativeTo: this.route });
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/**
* Copyright since 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { describe, it, expect, jest, beforeEach } from '@jest/globals';
import { buildCloseClientPayload, CloseClientFormValue, isValidDate } from './close-client.utils';
Comment thread
coderabbitai[bot] marked this conversation as resolved.

describe('buildCloseClientPayload', () => {
const DEFAULT_LOCALE = 'en';
const DEFAULT_DATE_FORMAT = 'dd MMMM yyyy';
const MOCK_FORMATTED_DATE = '03 November 2025';

let formatDateSpy: jest.MockedFunction<(date: Date, format: string) => string>;

const mockDateUtils = {
formatDate: (date: Date, format: string) => formatDateSpy(date, format)
};

beforeEach(() => {
formatDateSpy = jest.fn(() => MOCK_FORMATTED_DATE);
});

const makeValidForm = (overrides: Partial<CloseClientFormValue> = {}): CloseClientFormValue => ({
closureDate: new Date(2025, 10, 3),
closureReasonId: 1,
...overrides
});

const callBuildPayload = (
formValue: CloseClientFormValue,
options?: {
locale?: string;
dateFormat?: string;
}
) =>
buildCloseClientPayload(
formValue,
mockDateUtils,
options?.locale ?? DEFAULT_LOCALE,
options?.dateFormat ?? DEFAULT_DATE_FORMAT
);

describe('Valid inputs', () => {
it('formats Date closureDate', () => {
const date = new Date(2025, 10, 3);

const result = callBuildPayload(makeValidForm({ closureDate: date }));

expect(formatDateSpy).toHaveBeenCalledWith(date, DEFAULT_DATE_FORMAT);
expect(formatDateSpy).toHaveBeenCalledTimes(1);
expect(result.closureDate).toBe(MOCK_FORMATTED_DATE);
});

it('passes through pre-formatted string closureDate', () => {
const preFormattedDate = '03 November 2025';

const result = callBuildPayload(makeValidForm({ closureDate: preFormattedDate }));

expect(formatDateSpy).not.toHaveBeenCalled();
expect(result.closureDate).toBe(preFormattedDate);
});

it('supports custom locale', () => {
const result = callBuildPayload(makeValidForm(), {
locale: 'fr'
});

expect(result.locale).toBe('fr');
});

it('supports custom dateFormat', () => {
const customFormat = 'yyyy-MM-dd';

const result = callBuildPayload(makeValidForm(), {
dateFormat: customFormat
});

expect(result.dateFormat).toBe(customFormat);
});

it('preserves closureReasonId', () => {
const result = callBuildPayload(makeValidForm({ closureReasonId: 42 }));

expect(result.closureReasonId).toBe(42);
});
});

describe('Immutability', () => {
it('does not mutate input object', () => {
const formValue = makeValidForm();
const originalDate = new Date(formValue.closureDate as Date);
const originalReasonId = formValue.closureReasonId;

callBuildPayload(formValue);

expect(formValue.closureDate).toEqual(originalDate);
expect(formValue.closureReasonId).toBe(originalReasonId);
});
});

describe('closureDate validation', () => {
it('rejects invalid Date object', () => {
const invalidDate = new Date('invalid');

expect(isValidDate(invalidDate)).toBe(false);

expect(() => callBuildPayload(makeValidForm({ closureDate: invalidDate }))).toThrow(TypeError);
expect(() => callBuildPayload(makeValidForm({ closureDate: invalidDate }))).toThrow(
/Invalid closureDate.*Expected a valid Date or valid date string/
);

expect(formatDateSpy).not.toHaveBeenCalled();
});

it.each([
null,
undefined,
12345,
{} as unknown as Date,
''
])('rejects invalid closureDate: %p', (value) => {
const callWithValue = () => callBuildPayload(makeValidForm({ closureDate: value as unknown as Date }));
expect(callWithValue).toThrow(TypeError);
expect(callWithValue).toThrow(/Invalid closureDate.*Expected a valid Date or valid date string/);
});

it('rejects whitespace-only closureDate string', () => {
const callWithWhitespace = () => callBuildPayload(makeValidForm({ closureDate: ' ' as unknown as Date }));
expect(callWithWhitespace).toThrow(TypeError);
expect(callWithWhitespace).toThrow(/Invalid closureDate.*Expected a valid Date or valid date string/);

expect(formatDateSpy).not.toHaveBeenCalled();
});

it('rejects malformed closureDate string', () => {
const callWithMalformed = () => callBuildPayload(makeValidForm({ closureDate: 'not-a-date' as unknown as Date }));
expect(callWithMalformed).toThrow(TypeError);
expect(callWithMalformed).toThrow(/Invalid closureDate.*Expected a valid Date or valid date string/);

expect(formatDateSpy).not.toHaveBeenCalled();
});
});

describe('closureReasonId validation', () => {
it.each([
0,
-1,
1.5,
Number.NaN
])('rejects invalid closureReasonId: %p', (value) => {
const callWithReasonId = () => callBuildPayload(makeValidForm({ closureReasonId: value }));
expect(callWithReasonId).toThrow(TypeError);
expect(callWithReasonId).toThrow(/Invalid closureReasonId.*expected a positive integer/);
});

it('accepts valid boundary values', () => {
expect(() => callBuildPayload(makeValidForm({ closureReasonId: 1 }))).not.toThrow();

expect(() =>
callBuildPayload(
makeValidForm({
closureReasonId: Number.MAX_SAFE_INTEGER
})
)
).not.toThrow();
});

it('rejects missing closureReasonId', () => {
const callWithMissingReasonId = () =>
callBuildPayload({ closureDate: new Date(), closureReasonId: undefined as unknown as number });
expect(callWithMissingReasonId).toThrow(TypeError);
expect(callWithMissingReasonId).toThrow(/Invalid closureReasonId.*expected a positive integer/);
});

it('converts closureReasonId string to number', () => {
const result = callBuildPayload(
makeValidForm({
closureReasonId: '42' as unknown as number
})
);

expect(result.closureReasonId).toBe(42);
expect(typeof result.closureReasonId).toBe('number');
});

it('converts stringified boundary values correctly', () => {
const result = callBuildPayload(
makeValidForm({
closureReasonId: '1' as unknown as number
})
);

expect(result.closureReasonId).toBe(1);
});

it.each([
'0',
'-1',
'1.5',
'not-a-number'
])('converts string %p to number and validates', (value) => {
const testValue = value as unknown as number;
const callWithStringReasonId = () => callBuildPayload(makeValidForm({ closureReasonId: testValue }));
expect(callWithStringReasonId).toThrow(TypeError);
expect(callWithStringReasonId).toThrow(/Invalid closureReasonId.*expected a positive integer/);
});
});

describe('isValidDate', () => {
it('returns true for valid Date objects', () => {
expect(isValidDate(new Date())).toBe(true);
expect(isValidDate(new Date(2025, 10, 3))).toBe(true);
expect(isValidDate(new Date('2025-03-24'))).toBe(true);
});

it('returns false for invalid Date objects', () => {
expect(isValidDate(new Date('invalid'))).toBe(false);
expect(isValidDate(new Date(Number.NaN))).toBe(false);
});

it.each([
'2025-03-24',
123456,
null,
undefined,
{}
])('returns false for non-Date values: %p', (value) => {
expect(isValidDate(value)).toBe(false);
});
});
});
Loading
Loading