Skip to content

Commit 657d0e9

Browse files
committed
refactor: CHeaderBrand, CSidebarBrand - tag render mechanism change
1 parent 84aa264 commit 657d0e9

File tree

6 files changed

+97
-75
lines changed

6 files changed

+97
-75
lines changed
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
<template>
2-
<img class="c-header-brand"/>
3-
</template>
4-
51
<script>
2+
import CLink, { propsFactory } from '../link/CLink'
3+
import { mergeData } from 'vue-functional-data-merge'
4+
const props = Object.assign(propsFactory(), { tag: { type: String, default: 'div' }})
5+
66
export default {
7-
name: 'CHeaderBrand'
7+
functional: true,
8+
name: 'CHeaderBrand',
9+
props,
10+
render (h, { props, data, children }) {
11+
const isLink = props.href || props.to
12+
return h(
13+
isLink ? CLink : props.tag,
14+
mergeData(data, {
15+
staticClass: 'c-header-brand',
16+
props: isLink ? props : {}
17+
}),
18+
children
19+
)
20+
}
821
}
922
</script>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { mount } from '@vue/test-utils'
2+
import Component from '../CHeaderBrand'
3+
4+
const ComponentName = 'CHeaderBrand'
5+
const wrapper = mount(Component)
6+
const linkWrapper = mount(Component, {
7+
propsData: {
8+
href: 'https://coreui.io'
9+
}
10+
})
11+
12+
describe(`${ComponentName} .vue`, () => {
13+
it('has a name', () => {
14+
expect(Component.name).toMatch(ComponentName)
15+
})
16+
it('renders correctly as div', () => {
17+
expect(wrapper.element).toMatchSnapshot()
18+
})
19+
it('renders correctly as link', () => {
20+
expect(linkWrapper.element).toMatchSnapshot()
21+
})
22+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`CHeaderBrand .vue renders correctly as div 1`] = `
4+
<div
5+
class="c-header-brand"
6+
/>
7+
`;
8+
9+
exports[`CHeaderBrand .vue renders correctly as link 1`] = `
10+
<a
11+
class="c-header-brand"
12+
href="https://coreui.io"
13+
target="_self"
14+
/>
15+
`;
Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,24 @@
1-
<template>
2-
<div class="c-sidebar-brand">
3-
<slot>
4-
<CLink v-if="wrappedInLink" v-bind="linkProps">
5-
<img
6-
class="c-sidebar-brand-full"
7-
v-bind="fullAttributes"
8-
>
9-
<img
10-
class="c-sidebar-brand-minimized"
11-
v-bind="minimizedAttributes"
12-
>
13-
</CLink>
14-
<template v-else>
15-
<img
16-
class="c-sidebar-brand-full"
17-
v-bind="fullAttributes"
18-
>
19-
<img
20-
class="c-sidebar-brand-minimized"
21-
v-bind="minimizedAttributes"
22-
>
23-
</template>
24-
</slot>
25-
</div>
26-
</template>
1+
272

283
<script>
29-
import CLink from '../link/CLink'
4+
import CLink, { propsFactory } from '../link/CLink'
5+
import { mergeData } from 'vue-functional-data-merge'
6+
const props = Object.assign(propsFactory(), { tag: { type: String, default: 'div' }})
7+
308
export default {
9+
functional: true,
3110
name: 'CSidebarBrand',
32-
components: {
33-
CLink
34-
},
35-
props: {
36-
img: [String, Object],
37-
imgFull: [String, Object],
38-
imgMinimized: [String, Object],
39-
wrappedInLink: [String, Object]
40-
},
41-
computed: {
42-
linkProps () {
43-
return this.getObject(this.wrappedInLink, 'href')
44-
},
45-
minimizedAttributes () {
46-
return this.getObject(this.imgMinimized || this.img, 'src')
47-
},
48-
fullAttributes () {
49-
return this.getObject(this.imgFull || this.img, 'src')
50-
}
51-
},
52-
methods: {
53-
getObject (prop, key) {
54-
return typeof prop === 'object' ? prop : { [`${key}`]: prop }
55-
}
11+
props,
12+
render (h, { props, data, children }) {
13+
const isLink = props.href || props.to
14+
return h(
15+
isLink ? CLink : props.tag,
16+
mergeData(data, {
17+
staticClass: 'c-sidebar-brand',
18+
props: isLink ? props : {}
19+
}),
20+
children
21+
)
5622
}
5723
}
58-
</script>
24+
</script>
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { mount } from '@vue/test-utils'
22
import Component from '../CSidebarBrand'
33
const ComponentName = 'CSidebarBrand'
4-
const wrapper = mount(Component, {
4+
const wrapper = mount(Component)
5+
const linkWrapper = mount(Component, {
56
propsData: {
6-
wrappedInLink: { href: 'https://coreui.io' }
7+
href: 'https://coreui.io'
78
}
89
})
910

1011
describe(ComponentName, () => {
1112
it('has a name', () => {
1213
expect(Component.name).toMatch(ComponentName)
1314
})
14-
it('renders correctly', () => {
15+
it('renders correctly as div', () => {
1516
expect(wrapper.element).toMatchSnapshot()
1617
})
18+
it('renders correctly as link', () => {
19+
expect(linkWrapper.element).toMatchSnapshot()
20+
})
1721
})
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`CSidebarBrand renders correctly 1`] = `
4+
<a
5+
class="c-sidebar-brand"
6+
href="https://coreui.io"
7+
target="_self"
8+
/>
9+
`;
10+
11+
exports[`CSidebarBrand renders correctly as div 1`] = `
412
<div
513
class="c-sidebar-brand"
6-
>
7-
<a
8-
class=""
9-
href="https://coreui.io"
10-
target="_self"
11-
>
12-
<img
13-
class="c-sidebar-brand-full"
14-
/>
15-
16-
<img
17-
class="c-sidebar-brand-minimized"
18-
/>
19-
</a>
20-
</div>
14+
/>
15+
`;
16+
17+
exports[`CSidebarBrand renders correctly as link 1`] = `
18+
<a
19+
class="c-sidebar-brand"
20+
href="https://coreui.io"
21+
target="_self"
22+
/>
2123
`;

0 commit comments

Comments
 (0)