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

refactor: Handle getting value from disabled form controls in form-events util #499

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
124 changes: 122 additions & 2 deletions libs/ngxtension/form-events/src/form-events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('Form Events', () => {
);
});

it('returns a signal with the value, status, pristine, and touched values of a form after it has been interacted with', async () => {
it('returns an observable with the value, status, pristine, and touched values of a form after it has been interacted with', async () => {
const fixture: ComponentFixture<FormEventsComponent> =
TestBed.configureTestingModule({
imports: [FormEventsComponent],
Expand Down Expand Up @@ -165,10 +165,30 @@ describe('Form Events', () => {

fixture.detectChanges();

const signalVals: HTMLElement = fixture.debugElement.query(
const observableVals: HTMLElement = fixture.debugElement.query(
By.css('[data-testid="observable-values-initial-value-overwritten"]'),
).nativeElement;

expect(flattenJsonPipeFormatting(observableVals.textContent)).toBe(
flattenJsonPipeFormatting(`{
"value": {
"name": "custom"
},
"status": "VALID",
"touched": false,
"pristine": true,
"valid": true,
"invalid": false,
"pending": false,
"dirty": false,
"untouched": true
}`),
);

const signalVals: HTMLElement = fixture.debugElement.query(
By.css('[data-testid="signal-values-initial-value-overwritten"]'),
).nativeElement;

expect(flattenJsonPipeFormatting(signalVals.textContent)).toBe(
flattenJsonPipeFormatting(`{
"value": {
Expand All @@ -185,6 +205,63 @@ describe('Form Events', () => {
}`),
);
});

it('returns a value of controls or the whole form even if they are disabled', async () => {
const fixture: ComponentFixture<FormEventsComponent> =
TestBed.configureTestingModule({
imports: [FormEventsComponent],
}).createComponent(FormEventsComponent);

fixture.detectChanges();

const observableVals: HTMLElement = fixture.debugElement.query(
By.css('[data-testid="observable-values-disabled"]'),
).nativeElement;
const signalVals: HTMLElement = fixture.debugElement.query(
By.css('[data-testid="signal-values-disabled"]'),
).nativeElement;

const formDisabledButton: HTMLInputElement =
fixture.debugElement.nativeElement.querySelector('#formDisable');
const formEnabledButton: HTMLInputElement =
fixture.debugElement.nativeElement.querySelector('#formEnable');
const controlDisabledButton: HTMLInputElement =
fixture.debugElement.nativeElement.querySelector('#controlDisable');

const formValue = `{
"value": {
"name": ""
},
"status": "DISABLED",
"touched": false,
"pristine": true,
"valid": false,
"invalid": false,
"pending": false,
"dirty": false,
"untouched": true
}`;

formDisabledButton.click();
fixture.detectChanges();

expect(flattenJsonPipeFormatting(observableVals.textContent)).toBe(
flattenJsonPipeFormatting(formValue),
);
expect(flattenJsonPipeFormatting(signalVals.textContent)).toBe(
flattenJsonPipeFormatting(formValue),
);

formEnabledButton.click();
controlDisabledButton.click();
fixture.detectChanges();
expect(flattenJsonPipeFormatting(observableVals.textContent)).toBe(
flattenJsonPipeFormatting(formValue),
);
expect(flattenJsonPipeFormatting(signalVals.textContent)).toBe(
flattenJsonPipeFormatting(formValue),
);
});
});

@Component({
Expand Down Expand Up @@ -222,12 +299,35 @@ describe('Form Events', () => {
<input data-testid="name" formControlName="name" name="name" />
</form>

<button (click)="setFormDisabledState('formDisable')" id="formDisable">
Disable formDisabled
</button>
<button (click)="setFormDisabledState('formEnable')" id="formEnable">
Enable formDisabled
</button>
<button
(click)="setFormDisabledState('controlDisable')"
id="controlDisable"
>
Disable formDisabled's control
</button>

<pre data-testid="signal-values-initial-value-overwritten">{{
$formInitialValuesOverwritten() | json
}}</pre>
<pre data-testid="observable-values-initial-value-overwritten">{{
formInitialValuesOverwritten$ | async | json
}}</pre>

<form [formGroup]="formDisabled" id="test-form-disabled">
<label for="firstName">Name</label>
<input data-testid="name" formControlName="name" name="name" />
</form>

<pre data-testid="signal-values-disabled">{{ $formDisabled() | json }}</pre>
<pre data-testid="observable-values-disabled">{{
formDisabled$ | async | json
}}</pre>
`,
})
export default class FormEventsComponent implements OnInit {
Expand All @@ -240,6 +340,9 @@ export default class FormEventsComponent implements OnInit {
formInitialValueOverwritten = this.fb.group({
name: this.fb.control(''),
});
formDisabled = this.fb.group({
name: this.fb.control(''),
});

form$ = allEventsObservable(this.form);
$form = allEventsSignal(this.form);
Expand All @@ -251,7 +354,24 @@ export default class FormEventsComponent implements OnInit {
this.formInitialValueOverwritten,
);

formDisabled$ = allEventsObservable(this.formDisabled);
$formDisabled = allEventsSignal(this.formDisabled);

ngOnInit() {
this.formInitialValueOverwritten.controls.name.setValue('custom');
}

setFormDisabledState(type: 'formDisable' | 'formEnable' | 'controlDisable') {
switch (type) {
case 'formDisable':
this.formDisabled.disable();
break;
case 'formEnable':
this.formDisabled.enable();
break;
case 'controlDisable':
this.formDisabled.controls.name.disable();
break;
}
}
}
6 changes: 3 additions & 3 deletions libs/ngxtension/form-events/src/form-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ export function allEventsObservable<T>(
return defer(() =>
combineLatest([
valueEvents$(form).pipe(
startWith(form.value),
map((value) => (isValueEvent(value) ? value.value : value)),
startWith(form.getRawValue()),
map(() => form.getRawValue()),
distinctUntilChanged(
(previous, current) =>
JSON.stringify(previous) === JSON.stringify(current),
Expand Down Expand Up @@ -161,7 +161,7 @@ export function allEventsSignal<T>(
): Signal<FormEventData<T>> {
return toSignal(allEventsObservable(form), {
initialValue: {
value: form.value,
value: form.getRawValue(),
status: form.status,
pristine: form.pristine,
touched: form.touched,
Expand Down