Skip to content

Commit f845074

Browse files
committed
Extract mergeConfigWithDefaults for easier testability
1 parent 944dd00 commit f845074

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import mergeConfigWithDefaults from '../src/util/mergeConfigWithDefaults'
2+
3+
test('missing top level keys are pulled from the default config', () => {
4+
const userConfig = {
5+
colors: { red: '#ff0000' },
6+
modules: {},
7+
options: {},
8+
}
9+
10+
const defaultConfig = {
11+
colors: { green: '#00ff00' },
12+
screens: {
13+
sm: '576px'
14+
},
15+
modules: {},
16+
options: {},
17+
}
18+
19+
const result = mergeConfigWithDefaults(userConfig, defaultConfig)
20+
21+
expect(result).toEqual({
22+
colors: { red: '#ff0000' },
23+
screens: {
24+
sm: '576px'
25+
},
26+
modules: {},
27+
options: {},
28+
})
29+
})
30+
31+
test('user modules are merged with default modules', () => {
32+
const userConfig = {
33+
modules: { flexbox: false },
34+
options: {},
35+
}
36+
37+
const defaultConfig = {
38+
modules: {
39+
flexbox: ['responsive'],
40+
textAlign: ['responsive'],
41+
},
42+
options: {},
43+
}
44+
45+
const result = mergeConfigWithDefaults(userConfig, defaultConfig)
46+
47+
expect(result).toEqual({
48+
modules: {
49+
flexbox: false,
50+
textAlign: ['responsive'],
51+
},
52+
options: {},
53+
})
54+
})
55+
56+
test('user options are merged with default options', () => {
57+
const userConfig = {
58+
modules: {},
59+
options: { prefix: 'tw-' },
60+
}
61+
62+
const defaultConfig = {
63+
modules: {},
64+
options: {
65+
prefix: '-',
66+
important: false,
67+
},
68+
}
69+
70+
const result = mergeConfigWithDefaults(userConfig, defaultConfig)
71+
72+
expect(result).toEqual({
73+
modules: {},
74+
options: {
75+
prefix: 'tw-',
76+
important: false,
77+
},
78+
})
79+
})

src/index.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ import substituteResponsiveAtRules from './lib/substituteResponsiveAtRules'
1515
import substituteScreenAtRules from './lib/substituteScreenAtRules'
1616
import substituteClassApplyAtRules from './lib/substituteClassApplyAtRules'
1717

18-
function mergeConfigWithDefaults(config) {
19-
const defaultConfig = require('../defaultConfig')()
20-
_.defaults(config, defaultConfig)
21-
config.options = _.defaults(config.options, defaultConfig.options)
22-
config.options.modules = _.defaults(config.options.modules, defaultConfig.options.modules)
23-
return config
24-
}
18+
import mergeConfigWithDefaults from './util/mergeConfigWithDefaults'
2519

2620
const plugin = postcss.plugin('tailwind', config => {
2721
const plugins = []
@@ -36,7 +30,7 @@ const plugin = postcss.plugin('tailwind', config => {
3630
}
3731

3832
delete require.cache[require.resolve(path.resolve(config))]
39-
return mergeConfigWithDefaults(require(path.resolve(config)))
33+
return mergeConfigWithDefaults(require(path.resolve(config)), require('../defaultConfig')())
4034
}
4135

4236
return postcss(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import _ from 'lodash'
2+
3+
export default function(userConfig, defaultConfig) {
4+
_.defaults(userConfig, defaultConfig)
5+
userConfig.modules = _.defaults(userConfig.modules, defaultConfig.modules)
6+
userConfig.options = _.defaults(userConfig.options, defaultConfig.options)
7+
return userConfig
8+
}

0 commit comments

Comments
 (0)