@@ -2,83 +2,84 @@ import matchers from 'jest-vue-matcher'
2
2
import { mount } from '@vue/test-utils'
3
3
import Label from '@/components/Fields/Label'
4
4
5
+ const HELP = '(help content)'
5
6
const LABEL = 'the label'
7
+ const ITEM = {
8
+ help : HELP ,
9
+ label : LABEL
10
+ }
6
11
let wrapper
7
12
8
- describe ( 'Label' , ( ) => {
9
- beforeEach ( ( ) => {
10
- wrapper = mount ( Label , {
11
- propsData : {
12
- item : {
13
- label : LABEL
14
- }
15
- }
16
- } )
17
- expect . extend ( matchers ( wrapper ) )
13
+ const setup = ( {
14
+ hasAsteriskJustAfterLabel = false ,
15
+ item = ITEM
16
+ } = { } ) => {
17
+ wrapper = mount ( Label , {
18
+ propsData : {
19
+ item
20
+ } ,
21
+ computed : {
22
+ hasAsteriskJustAfterLabel : ( ) => hasAsteriskJustAfterLabel
23
+ }
18
24
} )
25
+ expect . extend ( matchers ( wrapper ) )
26
+ return wrapper
27
+ }
19
28
29
+ describe ( 'Label' , ( ) => {
20
30
afterEach ( ( ) => {
21
31
wrapper . destroy ( )
22
32
} )
23
33
24
34
it ( 'shows a label' , ( ) => {
35
+ setup ( )
25
36
expect ( 'label' ) . toHaveText ( LABEL )
26
37
} )
27
38
28
- it ( 'shows an alternative label' , async ( ) => {
39
+ it ( 'shows an alternative label' , ( ) => {
29
40
const ALTERNATIVE_LABEL = 'an alternative label'
30
-
31
- await wrapper . setProps ( {
32
- item : {
33
- label : LABEL ,
34
- alternativeLabel : ALTERNATIVE_LABEL
35
- }
36
- } )
41
+ const item = { ...ITEM , alternativeLabel : ALTERNATIVE_LABEL }
42
+ setup ( { item } )
37
43
38
44
expect ( 'label' ) . toHaveText ( ALTERNATIVE_LABEL )
39
45
expect ( 'label' ) . not . toHaveText ( LABEL )
40
46
} )
41
47
42
- it ( 'hides the label' , async ( ) => {
43
- await wrapper . setProps ( {
44
- item : {
45
- label : LABEL ,
46
- showLabel : false
47
- }
48
- } )
48
+ it ( 'hides the label' , ( ) => {
49
+ const item = { ...ITEM , showLabel : false }
50
+ setup ( { item } )
49
51
50
52
expect ( 'label' ) . not . toBeADomElement ( )
51
53
} )
52
54
53
55
it ( 'has a for attribute by default' , ( ) => {
56
+ setup ( )
57
+
54
58
expect ( 'label' ) . toHaveAttribute ( 'for' , 'the-label' )
55
59
} )
56
60
57
- it ( 'has a custom for attribute' , async ( ) => {
61
+ it ( 'has a custom for attribute' , ( ) => {
58
62
const ATTR_FOR = 'a-custom-for-attribute'
59
-
60
- await wrapper . setProps ( {
61
- item : {
62
- label : LABEL ,
63
- for : ATTR_FOR
64
- }
65
- } )
63
+ const item = { ...ITEM , for : ATTR_FOR }
64
+ setup ( { item } )
66
65
67
66
expect ( 'label' ) . toHaveAttribute ( 'for' , ATTR_FOR )
68
67
} )
69
68
70
69
it ( 'is mandatory by default' , ( ) => {
71
- expect ( 'label' ) . toHaveText ( '*' )
70
+ setup ( )
71
+ expect ( 'label' ) . toHaveText ( `${ LABEL } ${ HELP } *` )
72
72
} )
73
73
74
- it ( 'is not mandatory' , async ( ) => {
75
- await wrapper . setProps ( {
76
- item : {
77
- label : LABEL ,
78
- isRequired : false
79
- }
80
- } )
74
+ it ( 'is not mandatory' , ( ) => {
75
+ const item = { ...ITEM , isRequired : false }
76
+ setup ( { item } )
81
77
82
78
expect ( 'label' ) . not . toHaveText ( '*' )
83
79
} )
80
+
81
+ it ( 'put the asterisk just after the label' , ( ) => {
82
+ setup ( { hasAsteriskJustAfterLabel : true } )
83
+ expect ( 'label' ) . toHaveText ( `${ LABEL } *${ HELP } ` )
84
+ } )
84
85
} )
0 commit comments