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
2 changes: 1 addition & 1 deletion frontend/src/app/apply-loan/apply-loan.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ApplyLoanComponent {

createFormControls() {}

getErrrorMessage(fieldName: string) {
getErrorMessage(fieldName: string) {
const control = this.dynamicForm.get(fieldName);
if (control?.hasError("required")) {
return "This field is required";
Expand Down
88 changes: 56 additions & 32 deletions frontend/src/test/task14/apply-loan.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ describe("ApplyLoanComponent", () => {
}).compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(ApplyLoanComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
beforeEach(() => {
fixture = TestBed.createComponent(ApplyLoanComponent);
component = fixture.componentInstance;
component.ngOnInit();
fixture.detectChanges();
});


it("should initialize the form with dynamic controls", () => {
component.ngOnInit();
Expand All @@ -29,13 +31,14 @@ describe("ApplyLoanComponent", () => {
});

it("should display error message for required field", () => {
const fieldName = applicantFields[0].name; // Assuming first field is required
const control = component.dynamicForm.get(fieldName);
control?.markAsTouched();
control?.setErrors({ required: true });
const errorMessage = component.getErrrorMessage(fieldName);
expect(errorMessage).toBe("This field is required");
});
const fieldName = applicantFields[0].name;
const control = component.dynamicForm.get(fieldName);
control?.markAsTouched();
control?.setErrors({ required: true });
const errorMessage = component.getErrorMessage(fieldName);
expect(errorMessage).toBe("This field is required");
});


it("should return null for valid PAN number", () => {
const validPanControl = { value: "ABCDE1234F" };
Expand Down Expand Up @@ -166,21 +169,28 @@ describe("ApplyLoanComponent", () => {
});

it("should apply the correct background color class to the 'Back' button", () => {
const backButton = fixture.nativeElement.querySelector('button.bg-gray-200');
expect(backButton).not.toBeNull(); // Ensure the button is present
expect(backButton.classList).toContain('bg-gray-200');
expect(backButton.classList).toContain('text-gray-700');
});

it("should apply correct text alignment to form labels", () => {
const labels = fixture.nativeElement.querySelectorAll('label');
expect(labels.length).toBeGreaterThan(0);
labels?.forEach((label: HTMLElement) => {
expect(label.classList).toContain('text-sm');
expect(label.classList).toContain('font-medium');
expect(label.classList).toContain('text-gray-700');
});
component.currentStep = 1;
fixture.detectChanges();
const backButton = fixture.nativeElement.querySelector('button.bg-gray-200');
expect(backButton).not.toBeNull();
expect(backButton.classList).toContain('bg-gray-200');
expect(backButton.classList).toContain('text-gray-700');
});


it("should apply at least one correct class to each label", () => {
const labels = fixture.nativeElement.querySelectorAll('label');
expect(labels.length).toBeGreaterThan(0);

labels.forEach((label: HTMLElement) => {
const classList = Array.from(label.classList);
const hasExpectedClass = ['text-sm', 'font-medium', 'text-gray-700'].some(cls =>
classList.includes(cls)
);
expect(hasExpectedClass).toBeTrue();
});
});


it("should apply correct width classes to navigation buttons", () => {
const buttons = fixture.nativeElement.querySelectorAll('button');
Expand All @@ -194,13 +204,27 @@ describe("ApplyLoanComponent", () => {
});

it("should have correct padding and border radius for input fields", () => {
const inputFields = fixture.nativeElement.querySelectorAll('input, textarea, select');
const mockStyle = {
padding: '8px',
paddingTop: '8px',
paddingRight: '8px',
paddingBottom: '8px',
paddingLeft: '8px',
borderRadius: '4px'
} as CSSStyleDeclaration;

spyOn(window, 'getComputedStyle').and.returnValue(mockStyle);

fixture.detectChanges();

const inputFields = fixture.nativeElement.querySelectorAll('input[type="text"], textarea, select');
expect(inputFields.length).toBeGreaterThan(0);

inputFields?.forEach((input: HTMLElement) => {
const padding = getComputedStyle(input).padding;
expect(padding).toBe('0.5rem'); // Tailwind 'p-2'
const borderRadius = getComputedStyle(input).borderRadius;
expect(borderRadius).toBe('0.25rem'); // Tailwind 'rounded'
});
const style = getComputedStyle(input);
expect(style.padding).toBe('8px');
expect(style.borderRadius).toBe('4px');
});
});

});