Skip to content

Commit 83f6799

Browse files
committed
feat(layout): layout 组件补充测试用例(changelog-needed)
affects: @ued-plus/components ISSUES CLOSED: none
1 parent cea9c66 commit 83f6799

File tree

5 files changed

+186
-24
lines changed

5 files changed

+186
-24
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { nextTick, ref } from 'vue'
2+
import { describe, expect, it } from 'vitest'
3+
import { mount } from '@vue/test-utils'
4+
import { UedRow, UedCol } from '@ued-plus/components'
5+
6+
describe('Row.vue', () => {
7+
it('create', () => {
8+
const wrapper = mount(UedRow)
9+
expect(wrapper.classes()).toContain('ued-row')
10+
})
11+
12+
it('gutter', () => {
13+
const wrapper = mount(UedRow, {
14+
props: {
15+
gutter: 20,
16+
},
17+
})
18+
const rowElm = wrapper.element as HTMLElement
19+
expect(rowElm.style.marginLeft).toEqual('-10px')
20+
expect(rowElm.style.marginRight).toEqual('-10px')
21+
})
22+
23+
it('justify', () => {
24+
const wrapper = mount(UedRow, {
25+
props: {
26+
justify: 'end',
27+
},
28+
})
29+
expect(wrapper.classes()).toContain('is-justify-end')
30+
})
31+
32+
it('align', () => {
33+
const wrapper = mount(UedRow, {
34+
props: {
35+
align: 'bottom',
36+
},
37+
})
38+
expect(wrapper.classes()).toContain('is-align-bottom')
39+
})
40+
})
41+
42+
describe('Col.vue', () => {
43+
it('create', () => {
44+
const wrapper = mount(UedCol)
45+
expect(wrapper.classes()).toContain('ued-col')
46+
})
47+
48+
it('span', () => {
49+
const wrapper = mount(UedCol, {
50+
props: {
51+
span: 12,
52+
},
53+
})
54+
expect(wrapper.classes()).toContain('ued-col-12')
55+
})
56+
57+
it('pull', () => {
58+
const wrapper = mount(UedCol, {
59+
props: {
60+
pull: 3,
61+
},
62+
})
63+
expect(wrapper.classes()).toContain('ued-col-pull-3')
64+
})
65+
66+
it('push', () => {
67+
const wrapper = mount(UedCol, {
68+
props: {
69+
push: 3,
70+
},
71+
})
72+
expect(wrapper.classes()).toContain('ued-col-push-3')
73+
})
74+
75+
it('gutter', () => {
76+
const wrapper = mount({
77+
setup: () => () => (
78+
<UedRow gutter={20}>
79+
<UedCol span={12} ref="col"></UedCol>
80+
</UedRow>
81+
),
82+
})
83+
84+
const colElm = wrapper.findComponent({ ref: 'col' }).element as HTMLElement
85+
expect(colElm.style.paddingLeft === '10px').toBe(true)
86+
expect(colElm.style.paddingRight === '10px').toBe(true)
87+
})
88+
89+
it('change gutter value', async () => {
90+
const outer = ref(20)
91+
92+
const wrapper = mount({
93+
setup: () => () => (
94+
<UedRow gutter={outer.value} ref="row">
95+
<UedCol span={12} ref="col" />
96+
</UedRow>
97+
),
98+
})
99+
100+
const rowElm = wrapper.findComponent({ ref: 'row' }).element as HTMLElement
101+
const colElm = wrapper.findComponent({ ref: 'col' }).element as HTMLElement
102+
103+
expect(rowElm.style.marginLeft === '-10px').toBe(true)
104+
expect(rowElm.style.marginRight === '-10px').toBe(true)
105+
expect(colElm.style.paddingLeft === '10px').toBe(true)
106+
expect(colElm.style.paddingRight === '10px').toBe(true)
107+
108+
outer.value = 40
109+
await nextTick()
110+
111+
expect(rowElm.style.marginLeft === '-20px').toBe(true)
112+
expect(rowElm.style.marginRight === '-20px').toBe(true)
113+
expect(colElm.style.paddingLeft === '20px').toBe(true)
114+
expect(colElm.style.paddingRight === '20px').toBe(true)
115+
})
116+
117+
it('responsive', () => {
118+
const wrapper = mount({
119+
setup: () => () => (
120+
<UedRow gutter={20}>
121+
<UedCol
122+
ref="col"
123+
sm={{ span: 4, offset: 2 }}
124+
md={8}
125+
lg={{ span: 6, offset: 3 }}
126+
/>
127+
</UedRow>
128+
),
129+
})
130+
131+
const colElmClass = wrapper.findComponent({ ref: 'col' }).classes()
132+
133+
expect(colElmClass.includes('ued-col-sm-4')).toBe(true)
134+
expect(colElmClass.includes('ued-col-sm-4')).toBe(true)
135+
expect(colElmClass.includes('ued-col-offset-sm-2')).toBe(true)
136+
expect(colElmClass.includes('ued-col-lg-6')).toBe(true)
137+
expect(colElmClass.includes('ued-col-offset-lg-3')).toBe(true)
138+
expect(colElmClass.includes('ued-col-md-8')).toBe(true)
139+
})
140+
})

packages/components/src/layout/col.vue

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import './styles/col.scss'
99
import { computed, inject, PropType } from 'vue'
1010
import { isPositiveNumber } from '@ued-plus/utils'
11+
import { rowpKey } from './contant'
1112
1213
defineOptions({ name: 'UedCol' })
1314
@@ -61,7 +62,16 @@ const colProps = defineProps({
6162
},
6263
})
6364
64-
const rowGutter = inject('row-gutter', undefined)
65+
const rowInject = inject(rowpKey, undefined)
66+
67+
const gutter = computed(() => {
68+
if (rowInject?.gutter) {
69+
return isPositiveNumber(rowInject.gutter)
70+
? Math.floor(rowInject.gutter)
71+
: undefined
72+
}
73+
return undefined
74+
})
6575
6676
const handleBootstrap = (size: number | ColProps, sizeType: string) => {
6777
if (typeof size === 'object') {
@@ -91,7 +101,7 @@ const colClass = computed(() => {
91101
[`ued-col-offset-${colProps.offset}`]: isPositiveNumber(colProps.offset),
92102
[`ued-col-pull-${colProps.pull}`]: isPositiveNumber(colProps.pull),
93103
[`ued-col-push-${colProps.push}`]: isPositiveNumber(colProps.push),
94-
'is-guttered': rowGutter,
104+
'is-guttered': gutter.value,
95105
...xsSize,
96106
...smSize,
97107
...mdSize,
@@ -101,7 +111,7 @@ const colClass = computed(() => {
101111
})
102112
103113
const colStyle = computed(() => {
104-
const paddingNum = rowGutter ? rowGutter / 2 : undefined
114+
const paddingNum = gutter.value ? gutter.value / 2 : undefined
105115
return {
106116
'padding-left': `${paddingNum}px`,
107117
'padding-right': `${paddingNum}px`,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type { InjectionKey } from 'vue'
2+
import { RowPropsType } from './row'
3+
export const rowpKey: InjectionKey<RowPropsType> = Symbol('RowKey')
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ExtractPropTypes } from 'vue'
2+
3+
export const RowProps = {
4+
gutter: {
5+
type: Number,
6+
default: 0,
7+
},
8+
justify: {
9+
type: String,
10+
default: undefined,
11+
},
12+
align: {
13+
type: String,
14+
default: undefined,
15+
},
16+
tag: {
17+
type: String,
18+
default: undefined,
19+
},
20+
}
21+
22+
export type RowPropsType = ExtractPropTypes<typeof RowProps>

packages/components/src/layout/row.vue

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,20 @@
66

77
<script lang="ts" setup>
88
import './styles/row.scss'
9-
import { computed, provide } from 'vue'
9+
import { computed, provide, reactive, toRefs } from 'vue'
1010
import { isPositiveNumber } from '@ued-plus/utils'
11+
import { rowpKey } from './contant'
12+
import { RowProps } from './row'
1113
1214
defineOptions({ name: 'UedRow' })
1315
14-
const rowProps = defineProps({
15-
gutter: {
16-
type: Number,
17-
default: 0,
18-
},
19-
justify: {
20-
type: String,
21-
default: undefined,
22-
},
23-
align: {
24-
type: String,
25-
default: undefined,
26-
},
27-
tag: {
28-
type: String,
29-
default: undefined,
30-
},
31-
})
16+
const rowProps = defineProps(RowProps)
3217
3318
provide(
34-
'row-gutter',
35-
isPositiveNumber(rowProps.gutter) ? Math.floor(rowProps.gutter) : undefined
19+
rowpKey,
20+
reactive({
21+
...toRefs(rowProps),
22+
})
3623
)
3724
3825
const rowClass = computed(() => {

0 commit comments

Comments
 (0)