Skip to content

Commit 3c149bb

Browse files
author
esuau
committed
#176 Override emitChangeEvent to use specialized change class
1 parent 40a0779 commit 3c149bb

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

src/switch/switch.component.spec.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
2-
import { ComponentFixture, TestBed, fakeAsync, tick, async } from "@angular/core/testing";
2+
import { ComponentFixture, TestBed } from "@angular/core/testing";
33
import { By } from "@angular/platform-browser";
4-
import { DebugElement } from "@angular/core";
54
import { StaticIconModule } from "../icon/static-icon.module";
65

7-
import { Switch } from "./switch.component";
8-
import { Checkbox } from "../checkbox/checkbox.module";
6+
import { Switch, SwitchChange } from "./switch.component";
97

108
describe("Switch", () => {
119
let component: Switch;
1210
let fixture: ComponentFixture<Switch>;
1311
let labelElement: HTMLElement;
1412
let buttonElement: HTMLElement;
15-
let svgElement: HTMLElement;
1613

1714
beforeEach(() => {
1815
TestBed.configureTestingModule({
@@ -70,4 +67,11 @@ describe("Switch", () => {
7067
expect(buttonElement.attributes.getNamedItem("aria-checked").value).toEqual("true");
7168
});
7269

70+
it("should emit SwitchChange event", (done: any) => {
71+
component.change.subscribe((data: any) => {
72+
expect(data instanceof SwitchChange).toEqual(true);
73+
done();
74+
});
75+
component.emitChangeEvent();
76+
});
7377
});

src/switch/switch.component.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Checkbox } from "../checkbox/checkbox.component";
22
import {
33
ChangeDetectorRef,
44
Component,
5-
Input
5+
Input,
6+
Output,
7+
EventEmitter
68
} from "@angular/core";
79
import { NG_VALUE_ACCESSOR } from "@angular/forms";
810

@@ -101,6 +103,12 @@ export class Switch extends Checkbox {
101103
*/
102104
id = "switch-" + Switch.switchCount;
103105

106+
/**
107+
* Emits event notifying other classes when a change in state occurs on a switch after a
108+
* click.
109+
*/
110+
@Output() change = new EventEmitter<SwitchChange>();
111+
104112
/**
105113
* Creates an instance of Switch.
106114
*/
@@ -110,4 +118,17 @@ export class Switch extends Checkbox {
110118

111119
console.warn("`ibm-switch` has been deprecated in favour of `ibm-toggle`");
112120
}
121+
122+
/**
123+
* Creates instance of `SwitchChange` used to propagate the change event.
124+
* @memberof To
125+
*/
126+
emitChangeEvent() {
127+
let event = new SwitchChange();
128+
event.source = this;
129+
event.checked = this.checked;
130+
131+
this.propagateChange(this.checked);
132+
this.change.emit(event);
133+
}
113134
}

src/toggle/toggle.component.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ComponentFixture, TestBed, fakeAsync, tick, async } from "@angular/core
33
import { By } from "@angular/platform-browser";
44
import { StaticIconModule } from "../icon/static-icon.module";
55

6-
import { Toggle } from "./toggle.component";
6+
import { Toggle, ToggleChange } from "./toggle.component";
77

88
describe("Toggle", () => {
99
let component: Toggle;
@@ -67,4 +67,11 @@ describe("Toggle", () => {
6767
expect(buttonElement.attributes.getNamedItem("aria-checked").value).toEqual("true");
6868
});
6969

70+
it("should emit ToggleChange event", (done: any) => {
71+
component.change.subscribe((data: any) => {
72+
expect(data instanceof ToggleChange).toEqual(true);
73+
done();
74+
});
75+
component.emitChangeEvent();
76+
});
7077
});

src/toggle/toggle.component.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import {
44
Component,
55
Input,
66
Output,
7-
EventEmitter,
8-
ChangeDetectionStrategy
7+
EventEmitter
98
} from "@angular/core";
109
import { NG_VALUE_ACCESSOR } from "@angular/forms";
1110

@@ -113,6 +112,12 @@ export class Toggle extends Checkbox {
113112
*/
114113
id = "toggle-" + Toggle.toggleCount;
115114

115+
/**
116+
* Emits event notifying other classes when a change in state occurs on a toggle after a
117+
* click.
118+
*/
119+
@Output() change = new EventEmitter<ToggleChange>();
120+
116121
/**
117122
* Creates an instance of Toggle.
118123
* @param {ChangeDetectorRef} changeDetectorRef
@@ -122,4 +127,17 @@ export class Toggle extends Checkbox {
122127
super(changeDetectorRef);
123128
Toggle.toggleCount++;
124129
}
130+
131+
/**
132+
* Creates instance of `ToggleChange` used to propagate the change event.
133+
* @memberof To
134+
*/
135+
emitChangeEvent() {
136+
let event = new ToggleChange();
137+
event.source = this;
138+
event.checked = this.checked;
139+
140+
this.propagateChange(this.checked);
141+
this.change.emit(event);
142+
}
125143
}

0 commit comments

Comments
 (0)