Merged PR 1838: feat(oms-return-search): add unit tests for ReturnSearchResultItemComponent a...

feat(oms-return-search): add unit tests for ReturnSearchResultItemComponent and fix address fallback

- Add comprehensive Spectator-based unit tests for ReturnSearchResultItemComponent, covering all computed properties and edge cases.
- Fix address computed property to correctly fall back to shipping address when billing address is missing, ensuring robust display logic.

Ref: #5113
This commit is contained in:
Nino Righi
2025-05-26 19:19:14 +00:00
committed by Lorenz Hilpert
parent 6e8df1c4ab
commit 2f04b56f71
2 changed files with 116 additions and 1 deletions

View File

@@ -0,0 +1,114 @@
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import { ReturnSearchResultItemComponent } from './return-search-result-item.component';
import { ReceiptListItem } from '@isa/oms/data-access';
import { formatName } from 'libs/oms/utils/format-name';
jest.mock('libs/oms/utils/format-name', () => ({
formatName: jest.fn(() => 'Formatted Name'),
}));
describe('ReturnSearchResultItemComponent', () => {
let spectator: Spectator<ReturnSearchResultItemComponent>;
const createComponent = createComponentFactory(
ReturnSearchResultItemComponent,
);
const baseItem: ReceiptListItem = {
billing: {
person: { firstName: 'John', lastName: 'Doe' },
organisation: { name: 'Acme Corp' },
address: { zipCode: '12345', city: 'Metropolis' },
communicationDetails: { email: 'john.doe@example.com' },
},
shipping: {
address: { zipCode: '54321', city: 'Gotham' },
},
printedDate: '2024-06-01T12:00:00Z',
receiptNumber: 'R-123',
orderNumber: 'O-456',
} as ReceiptListItem;
it('should create', () => {
spectator = createComponent({ props: { item: baseItem } });
expect(spectator.component).toBeTruthy();
});
it('should compute name using formatName util', () => {
spectator = createComponent({ props: { item: baseItem } });
expect(formatName).toHaveBeenCalledWith({
firstName: 'John',
lastName: 'Doe',
organisationName: 'Acme Corp',
});
expect(spectator.component.name()).toBe('Formatted Name');
});
it('should return receiptDate from item', () => {
spectator = createComponent({ props: { item: baseItem } });
expect(spectator.component.receiptDate()).toBe('2024-06-01T12:00:00Z');
});
it('should return receiptNumber from item', () => {
spectator = createComponent({ props: { item: baseItem } });
expect(spectator.component.receiptNumber()).toBe('R-123');
});
it('should return orderNumber from item', () => {
spectator = createComponent({ props: { item: baseItem } });
expect(spectator.component.orderNumber()).toBe('O-456');
});
it('should return email from billing communicationDetails', () => {
spectator = createComponent({ props: { item: baseItem } });
expect(spectator.component.email()).toBe('john.doe@example.com');
});
it('should return address from billing address if present', () => {
spectator = createComponent({ props: { item: baseItem } });
expect(spectator.component.address()).toBe('12345 Metropolis');
});
it('should return address from shipping address if billing address is missing', () => {
const item = {
...baseItem,
billing: { ...baseItem.billing, address: undefined },
} as ReceiptListItem;
spectator = createComponent({ props: { item } });
expect(spectator.component.address()).toBe('54321 Gotham');
});
it('should return empty string if neither billing nor shipping address is present', () => {
const item = {
...baseItem,
billing: { ...baseItem.billing, address: undefined },
shipping: { address: undefined },
} as ReceiptListItem;
spectator = createComponent({ props: { item } });
expect(spectator.component.address()).toBe('');
});
it('should handle missing billing, shipping, and communicationDetails gracefully', () => {
const item = {} as ReceiptListItem;
spectator = createComponent({ props: { item } });
expect(spectator.component.name()).toBe('Formatted Name');
expect(spectator.component.receiptDate()).toBeUndefined();
expect(spectator.component.receiptNumber()).toBeUndefined();
expect(spectator.component.orderNumber()).toBeUndefined();
expect(spectator.component.email()).toBeUndefined();
expect(spectator.component.address()).toBe('');
});
it('should throw if item input is not set', () => {
// Arrange & Act & Assert
expect(() => createComponent()).toThrow();
});
});

View File

@@ -43,7 +43,8 @@ export class ReturnSearchResultItemComponent {
});
address = computed(() => {
const address = this.item()?.billing?.address;
const address =
this.item()?.billing?.address ?? this.item()?.shipping?.address;
return address ? [address.zipCode, address.city].join(' ') : '';
});
}