Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
27 changes: 24 additions & 3 deletions projects/components/src/checkbox/checkbox.component.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fakeAsync } from '@angular/core/testing';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { createHostFactory, Spectator } from '@ngneat/spectator/jest';
import { CheckboxComponent } from './checkbox.component';
Expand All @@ -9,7 +10,7 @@ describe('Checkbox component', () => {

const createHost = createHostFactory({
component: CheckboxComponent,
imports: [TraceCheckboxModule, RouterTestingModule],
imports: [TraceCheckboxModule, RouterTestingModule, ReactiveFormsModule],
providers: [],
declareComponent: false
});
Expand Down Expand Up @@ -52,12 +53,32 @@ describe('Checkbox component', () => {

// Click will toggle the values to true
spectator.click(inputElement);
expect(spectator.component.checked).toBe(true);
expect(spectator.component.isChecked).toBe(true);
expect(checkboxChangeSpy).toHaveBeenCalledWith(true);

// Click will toggle the values to false
spectator.click(inputElement);
expect(spectator.component.checked).toBe(false);
expect(spectator.component.isChecked).toBe(false);
expect(checkboxChangeSpy).toHaveBeenCalledWith(false);
}));

test('should work correctly with control value accessor', () => {
const formControl = new FormControl(false);
spectator = createHost(
`<ht-checkbox [label]="label" [formControl]="formControl">
</ht-checkbox>`,
{
hostProps: {
formControl: formControl
}
}
);
expect(spectator.component.isChecked).toBe(false);

formControl.setValue(true);
expect(spectator.component.isChecked).toBe(true);

formControl.disable();
expect(spectator.component.isDisabled).toBe(true);
});
});
56 changes: 46 additions & 10 deletions projects/components/src/checkbox/checkbox.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { MatCheckboxChange } from '@angular/material/checkbox';

@Component({
Expand All @@ -8,31 +9,66 @@ import { MatCheckboxChange } from '@angular/material/checkbox';
template: `
<mat-checkbox
labelPosition="after"
[checked]="this.checked"
[disabled]="this.disabled"
(change)="onCheckboxChange($event)"
[checked]="this.isChecked"
[disabled]="this.isDisabled"
(change)="this.onCheckboxChange($event)"
class="ht-checkbox"
[ngClass]="{ disabled: this.disabled }"
[ngClass]="{ disabled: this.isDisabled }"
>
<ht-label class="label" *ngIf="this.label !== undefined && this.label !== ''" [label]="this.label"></ht-label>
</mat-checkbox>
`
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: CheckboxComponent,
multi: true
}
]
})
export class CheckboxComponent {
export class CheckboxComponent implements ControlValueAccessor {
@Input()
public label?: string;

@Input()
public checked: boolean | undefined;
public set checked(checked: boolean | undefined) {
this.isChecked = checked ?? false;
}

@Input()
public disabled: boolean | undefined;
public set disabled(disabled: boolean | undefined) {
this.isDisabled = disabled ?? false;
}

@Output()
public readonly checkedChange: EventEmitter<boolean> = new EventEmitter();

public isChecked: boolean = false;
public isDisabled: boolean = false;

private onTouched!: () => void;
private onChanged!: (value: boolean) => void;

public onCheckboxChange(event: MatCheckboxChange): void {
this.checked = event.checked;
this.checkedChange.emit(this.checked);
this.isChecked = event.checked;
this.checkedChange.emit(this.isChecked);
this.onChanged(this.isChecked);
this.onTouched();
}

public registerOnChange(fn: (value: boolean) => void): void {
this.onChanged = fn;
}

public registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}

public setDisabledState(isDisabled: boolean): void {
this.isDisabled = isDisabled;
}

public writeValue(isChecked: boolean | undefined): void {
this.isChecked = isChecked ?? false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommonModule } from '@angular/common';
import { fakeAsync } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatSlideToggle, MatSlideToggleChange, MatSlideToggleModule } from '@angular/material/slide-toggle';
import { createHostFactory, Spectator } from '@ngneat/spectator/jest';
import { ToggleSwitchSize } from './toggle-switch-size';
Expand All @@ -12,7 +12,7 @@ describe('Toggle Switch Component', () => {
const createHost = createHostFactory({
component: ToggleSwitchComponent,
shallow: true,
imports: [MatSlideToggleModule, FormsModule, CommonModule]
imports: [MatSlideToggleModule, FormsModule, CommonModule, ReactiveFormsModule]
});

test('should pass properties to Mat Slide toggle correctly', fakeAsync(() => {
Expand Down Expand Up @@ -41,4 +41,20 @@ describe('Toggle Switch Component', () => {
spectator.triggerEventHandler(MatSlideToggle, 'change', new MatSlideToggleChange(matToggleComponent!, false));
expect(onCheckedChangeSpy).toHaveBeenCalledWith(false);
}));

test('should work correctly with control value accessor', () => {
const formControl = new FormControl(false);
spectator = createHost(`<ht-toggle-switch [label]="label" [formControl]="formControl"></ht-toggle-switch>`, {
hostProps: {
formControl: formControl
}
});
expect(spectator.component.isChecked).toBe(false);

formControl.setValue(true);
expect(spectator.component.isChecked).toBe(true);

formControl.disable();
expect(spectator.component.isDisabled).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, Output } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { MatSlideToggleChange } from '@angular/material/slide-toggle';
import { ToggleSwitchSize } from './toggle-switch-size';

Expand All @@ -10,33 +11,73 @@ import { ToggleSwitchSize } from './toggle-switch-size';
<div class="toggle-switch">
<mat-slide-toggle
color="primary"
[checked]="this.checked"
[checked]="this.isChecked"
[ngClass]="{ 'small-slide-toggle': this.size === '${ToggleSwitchSize.Small}', disabled: this.disabled }"
[disabled]="this.disabled"
[disabled]="this.isDisabled"
(change)="this.onToggle($event)"
>
<div class="label">{{ this.label }}</div>
</mat-slide-toggle>
</div>
`
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: ToggleSwitchComponent,
multi: true
}
]
})
export class ToggleSwitchComponent {
export class ToggleSwitchComponent implements ControlValueAccessor {
@Input()
public checked?: boolean;
public label?: string = '';

@Input()
public label?: string = '';
public set checked(checked: boolean | undefined) {
this.isChecked = checked ?? false;
}

@Input()
public disabled?: boolean;
public set disabled(disabled: boolean | undefined) {
this.isDisabled = disabled ?? false;
}

@Input()
public size: ToggleSwitchSize = ToggleSwitchSize.Small;

@Output()
public readonly checkedChange: EventEmitter<boolean> = new EventEmitter();

public isChecked: boolean = false;
public isDisabled: boolean = false;

private onTouched!: () => void;
private onChanged!: (value: boolean) => void;

public constructor(private readonly cdr: ChangeDetectorRef) {}

public onToggle(toggleChange: MatSlideToggleChange): void {
this.checkedChange.emit(toggleChange.checked);
this.isChecked = toggleChange.checked;
this.checkedChange.emit(this.isChecked);
this.onChanged(this.isChecked);
this.onTouched();
}

public registerOnChange(fn: (value: boolean) => void): void {
this.onChanged = fn;
}

public registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}

public setDisabledState(isDisabled: boolean): void {
this.isDisabled = isDisabled;
this.cdr.markForCheck();
}

public writeValue(isChecked: boolean | undefined): void {
this.isChecked = isChecked ?? false;
this.cdr.markForCheck();
}
}