Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components/forms): add harnesses for radio and radio group components #2754

Merged
merged 9 commits into from
Sep 20, 2024
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="sky-padding-even-lg">
<form [formGroup]="formGroup">
<sky-radio-group
data-sky-id="radio-group"
formControlName="paymentMethod"
headingLevel="4"
headingText="Payment method"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { SkyRadioGroupHarness } from '@skyux/forms/testing';

import { DemoComponent } from './demo.component';

describe('Basic radio group demo', () => {
async function setupTest(options: {
dataSkyId: string;
}): Promise<SkyRadioGroupHarness> {
const fixture = TestBed.createComponent(DemoComponent);

const loader = TestbedHarnessEnvironment.loader(fixture);

const harness = await loader.getHarness(
SkyRadioGroupHarness.with({ dataSkyId: options.dataSkyId }),
);

fixture.detectChanges();
await fixture.whenStable();

return harness;
}

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NoopAnimationsModule, DemoComponent],
});
});

it('should have the appropriate heading text/level/style, label text, and hint text', async () => {
const harness = await setupTest({ dataSkyId: 'radio-group' });

const radioButtons = await harness.getRadioButtons();

await expectAsync(harness.getHeadingText()).toBeResolvedTo(
'Payment method',
);
await expectAsync(harness.getHeadingLevel()).toBeResolvedTo(4);
await expectAsync(harness.getHeadingStyle()).toBeResolvedTo(4);
await expectAsync(harness.getHintText()).toBeResolvedTo(
'Card methods require proof of identification.',
);

await expectAsync(radioButtons[0].getLabelText()).toBeResolvedTo('Cash');
await expectAsync(radioButtons[0].getHintText()).toBeResolvedTo('');

await expectAsync(radioButtons[1].getLabelText()).toBeResolvedTo('Check');
await expectAsync(radioButtons[1].getHintText()).toBeResolvedTo('');

await expectAsync(radioButtons[2].getLabelText()).toBeResolvedTo(
'Apple pay',
);
await expectAsync(radioButtons[2].getHintText()).toBeResolvedTo('');

await expectAsync(radioButtons[3].getLabelText()).toBeResolvedTo('Credit');
await expectAsync(radioButtons[3].getHintText()).toBeResolvedTo(
'A 2% late fee is applied to payments made after the due date.',
);

await expectAsync(radioButtons[4].getLabelText()).toBeResolvedTo('Debit');
await expectAsync(radioButtons[4].getHintText()).toBeResolvedTo('');
});

it('should display an error message when there is a custom validation error', async () => {
const harness = await setupTest({ dataSkyId: 'radio-group' });

const radioHarness = (await harness.getRadioButtons())[1];

await radioHarness.check();

await expectAsync(harness.hasError('processingIssue')).toBeResolvedTo(true);
});

it('should show a help popover with the expected text', async () => {
const harness = await setupTest({
dataSkyId: 'radio-group',
});

await harness.clickHelpInline();

const helpPopoverTitle = await harness.getHelpPopoverTitle();
expect(helpPopoverTitle).toBe('Are there fees?');

const helpPopoverContent = await harness.getHelpPopoverContent();
expect(helpPopoverContent).toBe(
`We don't charge fees for any payment method. The only exception is when credit card payments are late, which incurs a 2% fee.`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h5 [class]="headingClass">
{{ headingText }}
</h5>
} @else {
<span [class]="headingClass">
<span [class]="'sky-radio-group-heading-text ' + headingClass">
{{ headingText }}
</span>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
display: flex;
}

h3,
h4,
h5 {
margin: 0;
display: inline-block;
legend {
h3,
h4,
h5 {
margin: 0;
display: inline-block;
}

span {
line-height: 1.1;
Blackbaud-SteveBrush marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
}
</span>
@if (labelText) {
@if (!labelHidden) {
<span class="sky-switch-label">{{ labelText }}</span>
}
<span
class="sky-switch-label sky-radio-label-text"
[class.sky-screen-reader-only]="labelHidden"
>{{ labelText }}</span
>
} @else {
<ng-content select="sky-radio-label" />
}
Expand Down
5 changes: 5 additions & 0 deletions libs/components/forms/testing/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ export { SkyFormErrorHarness } from './form-error/form-error-harness';
export { SkyFormErrorHarnessFilters } from './form-error/form-error-harness.filters';

export { SkyRadioFixture } from './radio-fixture';
export { SkyRadioGroupHarness } from './radio/radio-group-harness';
export { SkyRadioGroupHarnessFilters } from './radio/radio-group-harness-filters';
export { SkyRadioHarness } from './radio/radio-harness';
export { SkyRadioHarnessFilters } from './radio/radio-harness-filters';
export { SkyRadioLabelHarness } from './radio/radio-label-harness';
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<form [formGroup]="myForm">
<sky-radio-group
data-sky-id="radio-group"
formControlName="paymentMethod"
headingText="Payment method"
[attr.class]="class"
[headingHidden]="hideGroupHeading"
[headingLevel]="headingLevel"
[headingStyle]="headingStyle"
[helpKey]="helpKey"
[helpPopoverContent]="helpPopoverContent"
[helpPopoverTitle]="helpPopoverTitle"
[hintText]="hintText"
[required]="required"
[stacked]="stacked"
>
<sky-radio
data-sky-id="my-cash-radio"
helpPopoverContent="(xxx)xxx-xxxx"
helpPopoverTitle="Format"
labelText="Cash"
value="cash"
[helpKey]="helpKey"
[hintText]="cashHintText"
[labelHidden]="hideCashLabel"
/>
<sky-radio
data-sky-id="my-check-radio"
value="check"
label="Pay by check"
labelledBy="foo-check-id"
>
@if (!hideCheckLabel) {
<sky-radio-label> Check </sky-radio-label>
}
</sky-radio>
<sky-radio
data-sky-id="my-credit-radio"
labelText="Credit"
value="credit"
/>
@if (paymentMethod.errors?.['processingIssue']) {
<sky-form-error
errorName="processingIssue"
errorText="An error occurred with this payment method. Please contact us to resolve."
/>
}
</sky-radio-group>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Component, inject } from '@angular/core';
import {
AbstractControl,
FormsModule,
ReactiveFormsModule,
UntypedFormBuilder,
UntypedFormControl,
UntypedFormGroup,
ValidationErrors,
} from '@angular/forms';
// eslint-disable-next-line @nx/enforce-module-boundaries
import {
SkyRadioGroupHeadingLevel,
SkyRadioGroupHeadingStyle,
SkyRadioModule,
} from '@skyux/forms';

function validatePaymentMethod(
control: AbstractControl,
): ValidationErrors | null {
return control.value === 'check' ? { processingIssue: true } : null;
}

@Component({
standalone: true,
selector: 'test-radio-harness',
templateUrl: './radio-harness-test.component.html',
imports: [FormsModule, ReactiveFormsModule, SkyRadioModule],
})
export class RadioHarnessTestComponent {
public class = '';
public cashHintText: string | undefined;
public headingLevel: SkyRadioGroupHeadingLevel | undefined = 3;
public headingStyle: SkyRadioGroupHeadingStyle = 3;
public helpKey: string | undefined;
public helpPopoverContent: string | undefined;
public helpPopoverTitle: string | undefined;
public hideCashLabel = false;
public hideCheckLabel = false;
public hideGroupHeading = false;
public hintText: string | undefined;
public myForm: UntypedFormGroup;
public paymentMethod: UntypedFormControl;
public required = false;
public stacked = false;

#formBuilder = inject(UntypedFormBuilder);

constructor() {
this.paymentMethod = this.#formBuilder.control('cash', {
validators: [validatePaymentMethod],
});

this.myForm = this.#formBuilder.group({
paymentMethod: this.paymentMethod,
});
}

public disableForm(): void {
this.myForm.disable();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { SkyHarnessFilters } from '@skyux/core/testing';

/**
* A set of criteria that can be used to filter a list of `SkyRadioGroupHarness` instances.
* @internal
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface SkyRadioGroupHarnessFilters extends SkyHarnessFilters {}
Loading
Loading