config.spec.js
5.2 KB
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
import { createLocalVue } from '@vue/test-utils'
import { isVue3 } from '../../src/vue'
import { BootstrapVue } from '../../src'
import { AlertPlugin } from '../../src/components/alert'
import { BVConfigPlugin } from '../../src/bv-config'
import { setConfig, resetConfig } from './config-set'
import {
getBreakpoints,
getBreakpointsDown,
getBreakpointsUp,
getComponentConfig,
getConfig,
getConfigValue
} from './config'
describe('utils/config', () => {
afterEach(() => {
resetConfig()
})
it('getConfig() works', async () => {
expect(getConfig()).toEqual({})
})
it('setConfig() works', async () => {
const config = {
BAlert: { variant: 'danger' }
}
const breakpointsConfig = {
breakpoints: ['aa', 'bb', 'cc', 'dd', 'ee']
}
expect(getConfig()).toEqual({})
// Try a component config
setConfig(config)
expect(getConfig()).toEqual(config)
expect(getConfig()).not.toBe(config)
expect(getComponentConfig('BAlert')).toEqual(config.BAlert)
expect(getComponentConfig('BAlert')).not.toBe(config.BAlert)
expect(getComponentConfig('BAlert', 'variant')).toEqual('danger')
// Try breakpoint config (should merge)
setConfig(breakpointsConfig)
expect(getBreakpoints()).toEqual(breakpointsConfig.breakpoints)
expect(getBreakpoints()).not.toBe(breakpointsConfig.breakpoints)
expect(getConfigValue('breakpoints')).toEqual(breakpointsConfig.breakpoints)
// should leave previous config
expect(getComponentConfig('BAlert', 'variant')).toEqual('danger')
// Should merge config
expect(getConfig()).toEqual({ ...config, ...breakpointsConfig })
// Reset the configuration
resetConfig()
expect(getConfig()).toEqual({})
})
if (!isVue3) {
// We do not have complete localVue support, so resetting config does not work in proper way
it('config via Vue.use(BootstrapVue) works', async () => {
const localVue = createLocalVue()
const config = {
BAlert: { variant: 'foobar' }
}
expect(getConfig()).toEqual({})
localVue.use(BootstrapVue, config)
expect(getConfig()).toEqual(config)
// Reset the configuration
resetConfig()
expect(getConfig()).toEqual({})
})
it('config via Vue.use(ComponentPlugin) works', async () => {
const localVue = createLocalVue()
const config = {
BAlert: { variant: 'foobar' }
}
expect(getConfig()).toEqual({})
localVue.use(AlertPlugin, config)
expect(getConfig()).toEqual(config)
// Reset the configuration
resetConfig()
expect(getConfig()).toEqual({})
})
it('config via Vue.use(BVConfig) works', async () => {
const localVue = createLocalVue()
const config = {
BAlert: { variant: 'foobar' }
}
expect(getConfig()).toEqual({})
localVue.use(BVConfigPlugin, config)
expect(getConfig()).toEqual(config)
// Reset the configuration
resetConfig()
expect(getConfig()).toEqual({})
})
}
it('getConfigValue() works', async () => {
const config = {
formControls: { size: 'sm' }
}
setConfig(config)
expect(getConfigValue('formControls')).toEqual(config.formControls)
// Should return a deep clone
expect(getConfigValue('formControls')).not.toBe(config.formControls)
// Shape of returned value should be the same each call
expect(getConfigValue('formControls')).toEqual(getConfigValue('formControls'))
// Should return undefined for not found
expect(getConfigValue('foo.bar[1].baz')).toBeUndefined()
})
it('getComponentConfig() works', async () => {
const config = {
BAlert: { variant: 'info' }
}
setConfig(config)
// Specific component config key
expect(getComponentConfig('BAlert', 'variant')).toEqual('info')
// Component's full config
expect(getComponentConfig('BAlert')).toEqual(config.BAlert)
// Should return a deep clone for full config
expect(getComponentConfig('BAlert')).not.toBe(config.BAlert)
// Should return empty object for not found component
expect(getComponentConfig('foobar')).toEqual({})
// Should return undefined for not found component key
expect(getComponentConfig('BAlert', 'foobar')).toBeUndefined()
})
it('getBreakpoints() works', async () => {
const breakpointsConfig = {
breakpoints: ['aa', 'bb', 'cc', 'dd', 'ee']
}
expect(getBreakpoints()).toEqual(['xs', 'sm', 'md', 'lg', 'xl'])
// Should return a deep clone
expect(getBreakpoints()).not.toBe(getBreakpoints())
// Set new breakpoints
setConfig(breakpointsConfig)
expect(getBreakpoints()).toEqual(breakpointsConfig.breakpoints)
// Should return a deep clone
expect(getBreakpoints()).not.toBe(getBreakpoints())
expect(getBreakpoints()).not.toBe(breakpointsConfig.breakpoints)
})
it('getBreakpointsUp() works', async () => {
expect(getBreakpointsUp()).toEqual(['', 'sm', 'md', 'lg', 'xl'])
// Should return a deep clone
expect(getBreakpointsUp()).not.toBe(getBreakpointsUp())
})
it('getBreakpointsDown() works', async () => {
expect(getBreakpointsDown()).toEqual(['xs', 'sm', 'md', 'lg', ''])
// Should return a deep clone
expect(getBreakpointsDown()).not.toBe(getBreakpointsDown())
})
})