Skip to content

Commit

Permalink
feat(demo): add checkout form models and country field (#2615)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Nov 22, 2023
1 parent 2539cf6 commit b90454e
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

import { AddressFormFactory } from './address-form.factory';

describe('Daffodil Demo | Checkout | Forms | Address Form | Factories | AddressFormFactory', () => {
describe('@daffodil/demo | AddressFormFactory', () => {

let addressFormFactory;

Expand Down Expand Up @@ -41,6 +41,7 @@ describe('Daffodil Demo | Checkout | Forms | Address Form | Factories | AddressF
street: '',
city: '',
state: '',
country: '',
postcode: '',
telephone: '',
};
Expand All @@ -62,6 +63,7 @@ describe('Daffodil Demo | Checkout | Forms | Address Form | Factories | AddressF
street: 'street',
city: 'city',
state: 'another state',
country: 'another state',
postcode: 'postcode',
telephone: 'telephone',
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { Injectable } from '@angular/core';
import {
UntypedFormGroup,
UntypedFormBuilder,
FormBuilder,
Validators,
} from '@angular/forms';

import { AddressFormGroup } from '../models/address-form.type';

@Injectable({
providedIn: 'root',
})
export class AddressFormFactory {

constructor(
private fb: UntypedFormBuilder,
private fb: FormBuilder,
) {}

create(address): UntypedFormGroup {
create(address): AddressFormGroup {
return this.fb.group({
firstname: [address ? address.firstname : '', Validators.required],
lastname: [address ? address.lastname : '', Validators.required],
street: [address ? address.street : '', Validators.required],
city: [address ? address.city : '', Validators.required],
country: [address ? address.state : '', Validators.required],
state: [address ? address.state : '', Validators.required],
postcode: [address ? address.postcode : '', Validators.required],
telephone: [address ? address.telephone : ''],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
FormControl,
FormGroup,
} from '@angular/forms';

import {
DaffCountry,
DaffSubdivision,
} from '@daffodil/geography';

export type AddressFormGroup = FormGroup<{
firstname: FormControl<string>;
lastname: FormControl<string>;
street: FormControl<string>;
city: FormControl<string>;
country: FormControl<DaffCountry['id']>;
state: FormControl<DaffSubdivision['id']>;
postcode: FormControl<string>;
telephone: FormControl<string>;
}>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FormGroup } from '@angular/forms';

import { AddressFormGroup } from '../../../forms/address-form/models/address-form.type';
import { PaymentInfoFormGroup } from '../../payment-info-form/models/payment-form.type';

export type PaymentFormGroup = FormGroup<{
paymentInfo: PaymentInfoFormGroup;
address: AddressFormGroup;
}>;


Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ describe('PaymentFormComponent', () => {
let paymentFormComponent: PaymentFormComponent;
let addressFormComponent: MockAddressFormComponent;
let paymentInfoFormComponent: MockPaymentInfoFormComponent;
const addressFormFactorySpy = jasmine.createSpyObj('AddressFormFactory', ['create']);
let stubAddressFormGroup: UntypedFormGroup;
const paymentInfoFormFactorySpy = jasmine.createSpyObj('PaymentInfoFormFactory', ['create']);
let stubPaymentInfoFormGroup: UntypedFormGroup;
let addressFormFactory: AddressFormFactory;
let paymentInfoFormFactory: PaymentInfoFormFactory;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -91,28 +89,22 @@ describe('PaymentFormComponent', () => {
MockPaymentInfoFormComponent,
PaymentFormComponent,
],
providers: [
{ provide: AddressFormFactory, useValue: addressFormFactorySpy },
{ provide: PaymentInfoFormFactory, useValue: paymentInfoFormFactorySpy },
],
})
.compileComponents();
}));

beforeEach(() => {
store = TestBed.inject(Store);
addressFormFactory = TestBed.inject(AddressFormFactory);
paymentInfoFormFactory = TestBed.inject(PaymentInfoFormFactory);

stubPaymentInfo = null;
stubBillingAddress = null;
stubBillingAddressIsShippingAddress = false;
stubAddressFormGroup = new AddressFormFactory(new UntypedFormBuilder()).create(stubPaymentInfo);
stubAddressFormGroup.markAsDirty();
stubAddressFormGroup.markAsTouched();
addressFormFactorySpy.create.and.returnValue(stubAddressFormGroup);
stubPaymentInfoFormGroup = new PaymentInfoFormFactory(new UntypedFormBuilder()).create(stubBillingAddress);
paymentInfoFormFactorySpy.create.and.returnValue(stubPaymentInfoFormGroup);

fixture = TestBed.createComponent(WrapperComponent);
store = TestBed.inject(Store);
spyOn(store, 'dispatch');

fixture = TestBed.createComponent(WrapperComponent);
wrapper = fixture.componentInstance;
wrapper.paymentInfoValue = stubPaymentInfo;
wrapper.billingAddressValue = stubBillingAddress;
Expand Down Expand Up @@ -146,7 +138,7 @@ describe('PaymentFormComponent', () => {
describe('on <demo-address-form>', () => {

it('should set formGroup', () => {
expect(<UntypedFormGroup> addressFormComponent.formGroup).toEqual(<UntypedFormGroup> paymentFormComponent.form.controls['address']);
expect(addressFormComponent.formGroup).toEqual(paymentFormComponent.form.controls.address);
});

it('should set formSubmitted', () => {
Expand All @@ -157,30 +149,14 @@ describe('PaymentFormComponent', () => {
describe('on <demo-payment-info-form>', () => {

it('should set formGroup', () => {
expect(<UntypedFormGroup> paymentInfoFormComponent.formGroup).toEqual(<UntypedFormGroup> paymentFormComponent.form.controls['paymentInfo']);
expect(paymentInfoFormComponent.formGroup).toEqual(paymentFormComponent.form.controls.paymentInfo);
});

it('should set formSubmitted', () => {
expect(paymentInfoFormComponent.submitted).toEqual(false);
});
});

describe('ngOnInit', () => {

it('should call PaymentInfoFormFactory with paymentInfo', () => {
expect(paymentInfoFormFactorySpy.create).toHaveBeenCalledWith(stubPaymentInfo);
});

it('should call AddressFormFactory with billingAddress', () => {
expect(addressFormFactorySpy.create).toHaveBeenCalledWith(stubBillingAddress);
});

it('sets form.value to returned factory values', () => {
expect(paymentFormComponent.form.value.address).toEqual(stubAddressFormGroup.value);
expect(paymentFormComponent.form.value.paymentInfo).toEqual(stubPaymentInfoFormGroup.value);
});
});

describe('when checkbox is clicked', () => {

it('should emit toggleBillingAddressIsShippingAddress', () => {
Expand Down Expand Up @@ -221,9 +197,9 @@ describe('PaymentFormComponent', () => {

beforeEach(() => {
const formBuilder = new UntypedFormBuilder();
paymentFormComponent.form = formBuilder.group({
address: stubAddressFormGroup,
paymentInfo: stubPaymentInfoFormGroup,
paymentFormComponent.form.patchValue({
address: null,
paymentInfo: null,
});
});

Expand Down Expand Up @@ -277,12 +253,12 @@ describe('PaymentFormComponent', () => {

describe('and paymentInfoForm is valid', () => {

const expectedPaymentInfo: PaymentInfo = {
const expectedPaymentInfo = {
name: 'valid',
cardnumber: 2,
month: 2,
year: 2,
securitycode: 2,
cardnumber: '2',
month: '2',
year: '2',
securitycode: '2',
};

beforeEach(() => {
Expand Down Expand Up @@ -310,38 +286,36 @@ describe('PaymentFormComponent', () => {
describe('when form is valid', () => {

beforeEach(() => {
stubPaymentInfoFormGroup.setValue({
name: 'valid',
cardnumber: 'valid',
month: '01',
year: 'valid',
securitycode: 'valid',
});
stubAddressFormGroup.setValue({
firstname: 'valid',
lastname: 'valid',
street: 'valid',
city: 'valid',
state: 'California',
postcode: 'valid',
telephone: 'valid',
});
const formBuilder = new UntypedFormBuilder();
paymentFormComponent.form = formBuilder.group({
address: stubAddressFormGroup,
paymentInfo: stubPaymentInfoFormGroup,
paymentFormComponent.form.patchValue({
address: {
firstname: 'valid',
lastname: 'valid',
street: 'valid',
city: 'valid',
state: 'California',
country: 'US',
postcode: 'valid',
telephone: 'valid',
},
paymentInfo: {
name: 'valid',
cardnumber: 'valid',
month: '01',
year: 'valid',
securitycode: 'valid',
},
});
fixture.detectChanges();
});

it('should emit updatePaymentInfo', () => {
paymentFormComponent.onSubmit();
expect(paymentFormComponent.updatePaymentInfo.emit).toHaveBeenCalledWith(stubPaymentInfoFormGroup.value);
expect(paymentFormComponent.updatePaymentInfo.emit).toHaveBeenCalledWith(paymentFormComponent.form.value.paymentInfo);
});

it('should emit updateBillingAddress with expected object', () => {
paymentFormComponent.onSubmit();
expect(paymentFormComponent.updateBillingAddress.emit).toHaveBeenCalledWith(stubAddressFormGroup.value);
expect(paymentFormComponent.updateBillingAddress.emit).toHaveBeenCalledWith(paymentFormComponent.form.value.address);
});

it('should call store.dispatch with an EnablePlaceOrderButton action', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import {
Output,
EventEmitter,
} from '@angular/core';
import {
UntypedFormGroup,
UntypedFormBuilder,
} from '@angular/forms';
import { UntypedFormBuilder } from '@angular/forms';
import { Store } from '@ngrx/store';

import { PaymentInfo } from '@daffodil/checkout';
import { DaffAddress } from '@daffodil/core';

import { PaymentFormGroup } from './models/payment-form.type';
import { EnablePlaceOrderButton } from '../../../actions/checkout.actions';
import * as fromDemoCheckout from '../../../reducers';
import { AddressFormFactory } from '../../forms/address-form/factories/address-form.factory';
Expand All @@ -33,7 +31,7 @@ export class PaymentFormComponent implements OnInit {
@Output() updateBillingAddress: EventEmitter<any> = new EventEmitter();
@Output() toggleBillingAddressIsShippingAddress: EventEmitter<any> = new EventEmitter();

form: UntypedFormGroup;
form: PaymentFormGroup;

constructor(
private fb: UntypedFormBuilder,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {
Component,
OnInit,
Input,
} from '@angular/core';
import {
UntypedFormGroup,
FormControl,
} from '@angular/forms';

import { PaymentInfoFormGroup } from '../../models/payment-form.type';

interface MonthOption {
label: string;
Expand All @@ -25,7 +22,7 @@ interface YearOption {
})
export class PaymentInfoFormComponent {

@Input() formGroup: UntypedFormGroup;
@Input() formGroup: PaymentInfoFormGroup;
@Input() submitted: boolean;

constructor() { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {
FormControl,
FormGroup,
} from '@angular/forms';

export type PaymentInfoFormGroup = FormGroup<{
name: FormControl<string>;
cardnumber: FormControl<string>;
month: FormControl<string>;
year: FormControl<string>;
securitycode: FormControl<string>;
}>;


Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('ShippingFormComponent', () => {
wrapper.editModeValue = false;
wrapper.shippingAddressValue = stubShippingAddress;

stubAddressFormGroup = new AddressFormFactory(new UntypedFormBuilder()).create(stubShippingAddress).value;
stubAddressFormGroup = TestBed.inject(AddressFormFactory).create(stubShippingAddress);
addressFormFactorySpy.create.and.returnValue(stubAddressFormGroup);

fixture.detectChanges();
Expand Down Expand Up @@ -149,10 +149,6 @@ describe('ShippingFormComponent', () => {
expect(addressFormFactorySpy.create).toHaveBeenCalledWith(stubShippingAddress);
});

it('sets form.value.address to addressFormFactory.create()', () => {
expect(shippingFormComponent.form.value.address).toEqual(stubAddressFormGroup);
});

it('sets form.value.shippingOption to shippingOptionFormService.getShippingOptionFormGroup()', () => {
expect(shippingFormComponent.form.value.shippingOption).toEqual(shippingOptionFormService.getShippingOptionFormGroup().value);
});
Expand Down

0 comments on commit b90454e

Please sign in to comment.