-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
theme.js
180 lines (149 loc) · 5.63 KB
/
theme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { module, test } from "qunit";
import { setupTest } from "ember-qunit";
import { buildResolver } from "test-utils";
import sinon from "sinon";
import Service from "@ember/service";
import { EventEmitter } from "events";
import ThemeService from "services/theme";
module( "services/theme", function( hooks ) {
setupTest( hooks, {
resolver: buildResolver({})
});
class FakeMediaQueryList extends EventEmitter {
constructor( matches ) {
super();
this.matches = matches;
}
addEventListener( ...args ) {
return this.addListener( ...args );
}
removeEventListener( ...args ) {
return this.removeListener( ...args );
}
}
hooks.beforeEach(function() {
this.matchMediaStub = sinon.stub();
this.stubMatchMedia = arr => arr.map( ( matches, i ) => {
const mql = new FakeMediaQueryList( matches );
this.matchMediaStub.onCall( i ).callsFake( () => mql );
return mql;
});
const tmpElem = document.createElement( "div" );
this.getClassList = () => Array.from( tmpElem.classList.values() );
tmpElem.classList.add( "foo" );
this.owner.register( "service:-document", Service.extend({
defaultView: {
matchMedia: this.matchMediaStub
},
documentElement: {
classList: tmpElem.classList
}
}) );
this.owner.register( "service:settings", Service.extend({
content: {
gui: {
theme: "system"
}
}
}) );
});
test( "Apply theme classes", async function( assert ) {
this.owner.register( "service:theme", ThemeService.extend({
_checkSystemColorScheme() {}
}) );
/** @type {SettingsService} */
this.settings = this.owner.lookup( "service:settings" );
/** @type {ThemeService} */
this.theme = this.owner.lookup( "service:theme" );
assert.notOk( this.theme.systemTheme, "systemTheme is unset initially" );
assert.propEqual( this.getClassList(), [ "foo" ], "No theme classes set initially" );
this.set( "theme.systemTheme", "light" );
assert.propEqual( this.getClassList(), [ "foo", "theme-light" ], "Applies system theme" );
this.set( "theme.systemTheme", "dark" );
assert.propEqual( this.getClassList(), [ "foo", "theme-dark" ], "Changes system theme" );
this.set( "settings.content.gui.theme", "light" );
assert.propEqual( this.getClassList(), [ "foo", "theme-light" ], "Changes custom theme" );
this.set( "settings.content.gui.theme", "dark" );
assert.propEqual( this.getClassList(), [ "foo", "theme-dark" ], "Changes custom theme" );
this.set( "theme.systemTheme", "light" );
this.set( "settings.content.gui.theme", "" );
assert.propEqual( this.getClassList(), [ "foo", "theme-light" ], "Sys theme if unset" );
this.set( "settings.content.gui.theme", "unkown" );
assert.propEqual( this.getClassList(), [ "foo", "theme-light" ], "Sys theme if unknown" );
});
test( "System theme", async function( assert ) {
this.owner.register( "service:theme", ThemeService.extend({
_applyTheme: null
}) );
{
this.matchMediaStub.callsFake( () => ({ matches: false }) );
/** @type {ThemeService} */
const theme = this.owner.lookup( "service:theme", { singleton: false } );
theme.initialize();
assert.strictEqual( theme.systemTheme, "light", "Sets to default if no query matches" );
assert.propEqual(
this.matchMediaStub.args,
[
[ "(prefers-color-scheme: no-preference)" ],
[ "(prefers-color-scheme: light)" ],
[ "(prefers-color-scheme: dark)" ]
],
"Queries all schemes"
);
this.matchMediaStub.reset();
}
{
const mqls = this.stubMatchMedia([ false, false, true ]);
/** @type {ThemeService} */
const theme = this.owner.lookup( "service:theme", { singleton: false } );
theme.initialize();
assert.strictEqual( theme.systemTheme, "dark", "Sets theme to first matching query" );
assert.propEqual(
mqls.map( mql => mql.listenerCount( "change" ) ),
[ 0, 0, 1 ],
"Only the matching MediaQueryList has a change event listener attached"
);
const spy = sinon.spy( theme, "_checkSystemColorScheme" );
mqls[0].emit( "change", { matches: true } );
mqls[1].emit( "change", { matches: false } );
mqls[2].emit( "change", { matches: true } );
assert.notOk( spy.called, "Doesn't do anything if matching query still matches" );
assert.strictEqual( theme.systemTheme, "dark", "Theme doesn't get changed" );
this.matchMediaStub.reset();
const mqlsNext = this.stubMatchMedia([ false, true, false ]);
mqls[2].emit( "change", { matches: false } );
assert.ok( spy.calledOnce, "Checks system color schemes again on change" );
assert.propEqual(
this.matchMediaStub.args,
[
[ "(prefers-color-scheme: no-preference)" ],
[ "(prefers-color-scheme: light)" ]
],
"Queries schemes until the first one matches"
);
assert.propEqual(
mqlsNext.map( mql => mql.listenerCount( "change" ) ),
[ 0, 1, 0 ],
"Only the matching MediaQueryList has a change event listener attached"
);
assert.strictEqual( theme.systemTheme, "light", "Updates the system theme on change" );
spy.resetHistory();
mqls[2].emit( "change", { matches: false } );
assert.strictEqual(
mqls[2].listenerCount( "change" ),
0,
"Removes event listener from old MediaQueryList"
);
assert.notOk( spy.called, "Does only react to changes once" );
}
});
test( "Acceptance test", async function( assert ) {
this.owner.register( "service:theme", ThemeService );
this.stubMatchMedia([ false, false, true ]);
/** @type {ThemeService} */
const theme = this.owner.lookup( "service:theme" );
theme.initialize();
assert.strictEqual( theme.systemTheme, "dark", "System theme is dark" );
assert.propEqual( this.getClassList(), [ "foo", "theme-dark" ], "Sets system theme class" );
});
});