Skip to content

Commit 0a2aab8

Browse files
committed
Extract a function
1 parent dcc8ddb commit 0a2aab8

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

test/theme-switch.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,30 @@ test(`When user selected theme is auto, getInitialStateForIcon should return cor
189189
expect(getInitialStateForIcon()).toEqual([10, 0, 33, 0]);
190190
});
191191

192+
test(`When current theme is light and new theme is dark, createEvent should create event with proper details`, () => {
193+
const instance = new themeSwitchClass();
194+
const result = instance.createEvent(THEME_LIGHT, THEME_DARK);
195+
expect(result.detail.originId).toBe(instance.identifier);
196+
expect(result.detail.oldState).toBe(THEME_LIGHT);
197+
expect(result.detail.newState).toBe(THEME_DARK);
198+
});
199+
200+
test(`When current theme is dark and new theme is auto, createEvent should create event with proper details`, () => {
201+
const instance = new themeSwitchClass();
202+
const result = instance.createEvent(THEME_DARK, THEME_AUTO);
203+
expect(result.detail.originId).toBe(instance.identifier);
204+
expect(result.detail.oldState).toBe(THEME_DARK);
205+
expect(result.detail.newState).toBe(THEME_AUTO);
206+
});
207+
208+
test(`When current theme is auto and new theme is light, createEvent should create event with proper details`, () => {
209+
const instance = new themeSwitchClass();
210+
const result = instance.createEvent(THEME_AUTO, THEME_LIGHT);
211+
expect(result.detail.originId).toBe(instance.identifier);
212+
expect(result.detail.oldState).toBe(THEME_AUTO);
213+
expect(result.detail.newState).toBe(THEME_LIGHT);
214+
});
215+
192216
describe("Screenshot tests", () => {
193217
// Increase the timeout of executing all the test suit from
194218
// the default 5000 to a greater value to run fine on CI

theme-switch.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,7 @@ class ThemeSwitchElement extends HTMLElement {
133133
this.toggleTheme(oldTheme);
134134
const newTheme = getUserThemeSelection();
135135
// See https://stackoverflow.com/a/53804106/8583692
136-
const event = new CustomEvent(CUSTOM_EVENT_NAME, {
137-
detail: {
138-
originId: this.identifier,
139-
oldState: oldTheme,
140-
newState: newTheme
141-
},
142-
bubbles: true,
143-
composed: true,
144-
cancelable: false
145-
});
136+
const event = this.createEvent(oldTheme, newTheme);
146137
this.dispatchEvent(event);
147138
});
148139

@@ -160,6 +151,19 @@ class ThemeSwitchElement extends HTMLElement {
160151
this.shadowRoot.append(style);
161152
}
162153

154+
createEvent(oldTheme, newTheme) {
155+
return new CustomEvent(CUSTOM_EVENT_NAME, {
156+
detail: {
157+
originId: this.identifier,
158+
oldState: oldTheme,
159+
newState: newTheme
160+
},
161+
bubbles: true,
162+
composed: true,
163+
cancelable: false
164+
});
165+
}
166+
163167
// See https://stackoverflow.com/q/48316611
164168
toggleTheme(currentTheme) {
165169
if (currentTheme === THEME_AUTO) {

theme-switch.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)