Skip to content

Commit e1c95fb

Browse files
committed
Fix Saving Configuration Bug
1 parent 48c8164 commit e1c95fb

File tree

6 files changed

+125
-100
lines changed

6 files changed

+125
-100
lines changed

browser/main/Main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ class Main extends React.Component {
149149
const { dispatch, config } = this.props
150150

151151
this.refreshTheme = setInterval(() => {
152-
chooseTheme(ConfigManager.get().ui)
152+
const conf = ConfigManager.get()
153+
chooseTheme(conf)
153154
}, 5 * 1000)
154155

155-
chooseTheme(config.ui)
156+
chooseTheme(config)
156157
applyTheme(config.ui.theme)
157158

158159
if (getLocales().indexOf(config.ui.language) !== -1) {

browser/main/lib/ConfigManager.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import _ from 'lodash'
22
import RcParser from 'browser/lib/RcParser'
33
import i18n from 'browser/lib/i18n'
44
import ee from 'browser/main/lib/eventEmitter'
5-
import { chooseTheme, applyTheme } from 'browser/main/lib/ThemeManager'
65

76
const OSX = global.process.platform === 'darwin'
87
const win = global.process.platform === 'win32'
@@ -204,9 +203,6 @@ function set(updates) {
204203
if (!validate(newConfig)) throw new Error('INVALID CONFIG')
205204
_save(newConfig)
206205

207-
chooseTheme(newConfig.ui)
208-
applyTheme(newConfig.ui.theme)
209-
210206
i18n.setLocale(newConfig.ui.language)
211207

212208
let editorTheme = document.getElementById('editorTheme')

browser/main/lib/ThemeManager.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
const chooseTheme = ui => {
1+
import ConfigManager from 'browser/main/lib/ConfigManager'
2+
3+
const saveChanges = newConfig => {
4+
ConfigManager.set(newConfig)
5+
}
6+
7+
const chooseTheme = config => {
8+
const { ui } = config
29
if (!ui.enableScheduleTheme) {
310
return
411
}
@@ -21,11 +28,13 @@ const chooseTheme = ui => {
2128
ui.defaultTheme = ui.theme
2229
ui.theme = ui.scheduledTheme
2330
applyTheme(ui.theme)
31+
saveChanges(config)
2432
}
2533
} else {
2634
if (ui.theme !== ui.defaultTheme) {
2735
ui.theme = ui.defaultTheme
2836
applyTheme(ui.theme)
37+
saveChanges(config)
2938
}
3039
}
3140
}
@@ -40,6 +49,11 @@ const applyTheme = theme => {
4049
]
4150
if (supportedThemes.indexOf(theme) !== -1) {
4251
document.body.setAttribute('data-theme', theme)
52+
if (document.body.querySelector('.MarkdownPreview')) {
53+
document.body
54+
.querySelector('.MarkdownPreview')
55+
.contentDocument.body.setAttribute('data-theme', theme)
56+
}
4357
} else {
4458
document.body.setAttribute('data-theme', 'default')
4559
}

browser/main/modals/PreferencesModal/UiTab.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import i18n from 'browser/lib/i18n'
1313
import { getLanguages } from 'browser/lib/Languages'
1414
import normalizeEditorFontFamily from 'browser/lib/normalizeEditorFontFamily'
1515
import uiThemes from 'browser/lib/ui-themes'
16+
import { chooseTheme, applyTheme } from 'browser/main/lib/ThemeManager'
1617

1718
const OSX = global.process.platform === 'darwin'
1819

@@ -194,6 +195,9 @@ class UiTab extends React.Component {
194195
preview: this.state.config.preview
195196
}
196197

198+
chooseTheme(newConfig)
199+
applyTheme(newConfig.ui.theme)
200+
197201
ConfigManager.set(newConfig)
198202

199203
store.dispatch({

tests/lib/themeManager-test.js

Lines changed: 0 additions & 93 deletions
This file was deleted.

tests/lib/themeManager.test.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @fileoverview Unit test for browser/main/lib/ThemeManager.js
3+
*/
4+
const { chooseTheme, applyTheme } = require('browser/main/lib/ThemeManager')
5+
jest.mock('../../browser/main/lib/ConfigManager', () => {
6+
return {
7+
set: () => {}
8+
}
9+
})
10+
11+
const originalDate = Date
12+
let context = {}
13+
14+
beforeAll(() => {
15+
const constantDate = new Date('2017-11-27T14:33:42Z')
16+
global.Date = class extends Date {
17+
constructor() {
18+
super()
19+
return constantDate
20+
}
21+
}
22+
})
23+
24+
beforeEach(() => {
25+
context = {
26+
ui: {
27+
theme: 'white',
28+
scheduledTheme: 'dark',
29+
enableScheduleTheme: true,
30+
defaultTheme: 'monokai'
31+
}
32+
}
33+
})
34+
35+
afterAll(() => {
36+
global.Date = originalDate
37+
})
38+
39+
test("enableScheduleTheme is false, theme shouldn't change", () => {
40+
context.ui.enableScheduleTheme = false
41+
42+
const beforeTheme = context.ui.theme
43+
chooseTheme(context)
44+
const afterTheme = context.ui.theme
45+
46+
expect(afterTheme).toBe(beforeTheme)
47+
})
48+
49+
// NOT IN SCHEDULE
50+
test("scheduleEnd is bigger than scheduleStart and not in schedule, theme shouldn't change", () => {
51+
const beforeTheme = context.ui.defaultTheme
52+
context.ui.scheduleStart = 720 // 12:00
53+
context.ui.scheduleEnd = 870 // 14:30
54+
chooseTheme(context)
55+
const afterTheme = context.ui.theme
56+
57+
expect(afterTheme).toBe(beforeTheme)
58+
})
59+
60+
test("scheduleStart is bigger than scheduleEnd and not in schedule, theme shouldn't change", () => {
61+
const beforeTheme = context.ui.defaultTheme
62+
context.ui.scheduleStart = 960 // 16:00
63+
context.ui.scheduleEnd = 600 // 10:00
64+
chooseTheme(context)
65+
const afterTheme = context.ui.theme
66+
67+
expect(afterTheme).toBe(beforeTheme)
68+
})
69+
70+
// IN SCHEDULE
71+
test('scheduleEnd is bigger than scheduleStart and in schedule, theme should change', () => {
72+
const beforeTheme = context.ui.scheduledTheme
73+
context.ui.scheduleStart = 720 // 12:00
74+
context.ui.scheduleEnd = 900 // 15:00
75+
chooseTheme(context)
76+
const afterTheme = context.ui.theme
77+
78+
expect(afterTheme).toBe(beforeTheme)
79+
})
80+
81+
test('scheduleStart is bigger than scheduleEnd and in schedule, theme should change', () => {
82+
const beforeTheme = context.ui.scheduledTheme
83+
context.ui.scheduleStart = 1200 // 20:00
84+
context.ui.scheduleEnd = 900 // 15:00
85+
chooseTheme(context)
86+
const afterTheme = context.ui.theme
87+
88+
expect(afterTheme).toBe(beforeTheme)
89+
})
90+
91+
test("theme to apply is not a supported theme, theme shouldn't change", () => {
92+
applyTheme('notATheme')
93+
const afterTheme = document.body.dataset.theme
94+
95+
expect(afterTheme).toBe('default')
96+
})
97+
98+
test('theme to apply is a supported theme, theme should change', () => {
99+
applyTheme(context.ui.defaultTheme)
100+
const afterTheme = document.body.dataset.theme
101+
102+
expect(afterTheme).toBe(context.ui.defaultTheme)
103+
})

0 commit comments

Comments
 (0)