Skip to content

Commit

Permalink
Unit tests for billing address (#17749)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmch91 authored Aug 14, 2023
1 parent dc7e363 commit 9c21396
Show file tree
Hide file tree
Showing 6 changed files with 454 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
// TODO: Add unit tests

// import { Address } from '@spartacus/core';
// import { Card } from '@spartacus/storefront';
// import { GetAddressCardContent } from './get-address-card-content.pipe';

// const mockedAddress: Address = {
// country: { isocode: 'PL' },
// titleCode: 'mr',
// firstName: 'John',
// lastName: 'Doe',
// line1: 'Noname street',
// line2: '',
// town: 'Warsaw',
// postalCode: '02651',
// phone: '',
// cellphone: '',
// defaultAddress: false,
// };

// describe('GetAddressCardContent', () => {
// const pipe = new GetAddressCardContent();

// it('should return empty object if address has not been provided', () => {
// expect(pipe.transform(undefined as unknown as Address)).toEqual({});
// });

// it('should show region as address region iso code if iso code present', () => {
// const isocode: string = 'testIso';
// const address: Address = {
// ...mockedAddress,
// region: { isocode },
// };

// expect(pipe.transform(address)).toContain(isocode);
// });

// it('should transform address object to card object', () => {
// const expectedResult: Card = {
// textBold: 'John Doe',
// text: ['Noname street', '', 'Warsaw, PL', '02651', ''],
// };

// expect(pipe.transform(mockedAddress)).toEqual(expectedResult);
// });
// });
/*
* SPDX-FileCopyrightText: 2023 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import { TestBed } from '@angular/core/testing';
import { Address } from '@spartacus/core';
import { GetAddressCardContent } from './get-address-card-content.pipe';

describe('GetAddressCardContentPipe', () => {
let pipe: GetAddressCardContent;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [GetAddressCardContent],
});

pipe = TestBed.inject(GetAddressCardContent);
});

it('should create an instance', () => {
expect(pipe).toBeTruthy();
});

it('should transform address to card content', () => {
const address = {
firstName: 'John',
lastName: 'Doe',
line1: '123 Main St',
line2: 'Apt 4B',
town: 'Cityville',
region: { isocode: 'CA' },
country: { isocode: 'US' },
postalCode: '12345',
phone: '555-1234',
};

const result = pipe.transform(address);

expect(result).toEqual({
textBold: 'John Doe',
text: ['123 Main St', 'Apt 4B', 'Cityville, CA, US', '12345', '555-1234'],
});
});

it('should handle missing address', () => {
const result = pipe.transform(null as unknown as Address);

expect(result).toEqual({});
});

it('should handle missing region and country', () => {
const address = {
firstName: 'Jane',
lastName: 'Smith',
line1: '456 Elm St',
town: 'Townsville',
postalCode: '67890',
phone: '555-5678',
};

const result = pipe.transform(address);
console.log(result);
expect(result).toEqual({
textBold: 'Jane Smith',
text: ['456 Elm St', undefined, 'Townsville', '67890', '555-5678'],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,43 @@ export class GetAddressCardContent implements PipeTransform {
return {};
}

let region = '';

if (address.region && address.region.isocode) {
region = address.region.isocode + ', ';
}

return {
textBold: address.firstName + ' ' + address.lastName,
textBold: `${address.firstName} ${address.lastName}`,
text: [
address.line1,
address.line2,
address.town + ', ' + region + address.country?.isocode,
this.getTownLine(address),
address.postalCode,
address.phone,
],
} as Card;
}

protected getTownLine(address: Address): string {
const region = address.region?.isocode || '';
const town = address.town || '';
const countryIsocode = address.country?.isocode || '';

const townLineParts = [];

if (town) {
townLineParts.push(town);
}

if (region) {
if (town) {
townLineParts.push(', ');
}
townLineParts.push(region);
}

if (countryIsocode) {
if (town || region) {
townLineParts.push(', ');
}
townLineParts.push(countryIsocode);
}

return townLineParts.join('');
}
}
Loading

0 comments on commit 9c21396

Please sign in to comment.