1
- import { afterEach , beforeEach , describe , expect , test , vi } from "vitest" ;
1
+ import { afterEach , beforeAll , beforeEach , describe , expect , test , vi } from "vitest" ;
2
2
import { defineComponent , markRaw } from "vue" ;
3
3
import { createTestingPinia } from "@pinia/testing" ;
4
4
import { flushPromises , mount } from "@vue/test-utils" ;
@@ -12,15 +12,19 @@ import {
12
12
CERTIFICATION_BODY_DECISION ,
13
13
LEVEL_AB ,
14
14
LEVEL_C1 ,
15
+ LEVEL_C2 ,
16
+ LEVEL_C3 ,
15
17
LEVEL_CONVENTIONAL ,
16
18
} from "@/referentiels/ab.js" ;
17
19
18
20
import record from "@/utils/__fixtures__/record-with-features.json" assert { type : "json " } ;
19
21
import EditForm from "@/components/forms/SingleItemCertificationBodyForm.vue" ;
20
22
import TableComponent from "@/components/records/Table/index.vue" ;
23
+ import { useUserStore } from "@/stores/user" ;
21
24
22
25
const pinia = createTestingPinia ( { createSpy : vi . fn , stubActions : false } ) ;
23
26
const recordStore = useRecordStore ( pinia ) ;
27
+ const userStore = useUserStore ( pinia ) ;
24
28
const permissions = usePermissions ( pinia ) ;
25
29
const storage = useCartoBioStorage ( pinia ) ;
26
30
@@ -51,97 +55,146 @@ describe("SingleItemCertificationBodyForm", () => {
51
55
document . body . outerHTML = "" ;
52
56
} ) ;
53
57
54
- test ( "we assign a certification state" , async ( ) => {
55
- const form = wrapper . getComponent ( EditForm ) ;
56
-
57
- // if "Conventionnel", there is no date field
58
- await form . find ( `#conversion-${ LEVEL_CONVENTIONAL } ` ) . setValue ( ) ;
59
- expect ( form . find ( "#engagement_date" ) . exists ( ) ) . toEqual ( false ) ;
58
+ describe ( "item is not readonly" , async ( ) => {
59
+ beforeAll ( async ( ) => {
60
+ userStore . user = {
61
+ organismeCertificateur : {
62
+ id : 1 ,
63
+ } ,
64
+ } ;
65
+ } ) ;
66
+ test ( "we assign a certification state" , async ( ) => {
67
+ const form = wrapper . getComponent ( EditForm ) ;
60
68
61
- // if AB, date field is not mandatory
62
- await form . find ( `#conversion-${ LEVEL_AB } ` ) . setValue ( ) ;
63
- expect ( form . find ( "#engagement_date" ) . attributes ( ) ) . not . toHaveProperty ( "required" ) ;
69
+ // if "Conventionnel", there is no date field
70
+ await form . find ( `#conversion-${ LEVEL_CONVENTIONAL } ` ) . setValue ( ) ;
71
+ expect ( form . find ( "#engagement_date" ) . exists ( ) ) . toEqual ( false ) ;
64
72
65
- // date field is mandatory otherwise
66
- await form . find ( `#conversion-${ LEVEL_C1 } ` ) . setValue ( ) ;
67
- expect ( form . find ( "#engagement_date" ) . attributes ( ) ) . toHaveProperty ( "required" , "" ) ;
68
- } ) ;
73
+ // if AB, date field is not mandatory
74
+ await form . find ( `#conversion-${ LEVEL_AB } ` ) . setValue ( ) ;
75
+ expect ( form . find ( "#engagement_date" ) . attributes ( ) ) . not . toHaveProperty ( "required" ) ;
69
76
70
- test ( "we toggle expanded annotations" , async ( ) => {
71
- const form = wrapper . getComponent ( EditForm ) ;
77
+ // date field is mandatory otherwise
78
+ await form . find ( `#conversion-${ LEVEL_C1 } ` ) . setValue ( ) ;
79
+ expect ( form . find ( "#engagement_date" ) . attributes ( ) ) . toHaveProperty ( "required" , "" ) ;
80
+ } ) ;
72
81
73
- // We have all the tags tags and no expand button
74
- expect ( form . findAll ( ".fr-tags-group--annotations > .annotation-choice" ) ) . toHaveLength ( 5 ) ;
75
- expect ( form . find ( ".fr-tags-group--annotations > .annotation--more" ) . attributes ( ) ) . toHaveProperty ( "hidden" ) ;
82
+ test ( "we toggle expanded annotations" , async ( ) => {
83
+ const form = wrapper . getComponent ( EditForm ) ;
76
84
77
- // expect(form.find('.fr-tags-group--annotations > .annotation--more').attributes()).not.toHaveProperty('hidden')
78
- // await form.find('.fr-tags-group--annotations > .annotation--more button').trigger('click')
85
+ // We have all the tags tags and no expand button
86
+ expect ( form . findAll ( ".fr-tags-group--annotations > .annotation-choice" ) ) . toHaveLength ( 5 ) ;
87
+ expect ( form . find ( ".fr-tags-group--annotations > .annotation--more" ) . attributes ( ) ) . toHaveProperty ( "hidden" ) ;
79
88
80
- const expectedChoices = Object . keys ( AnnotationTags ) ;
81
- expect ( form . findAll ( ".fr-tags-group--annotations > .annotation-choice" ) ) . toHaveLength ( expectedChoices . length ) ;
82
- } ) ;
89
+ // expect(form.find('.fr-tags-group--annotations > .annotation--more').attributes()).not.toHaveProperty('hidden')
90
+ // await form.find('.fr-tags-group--annotations > .annotation--more button').trigger('click')
83
91
84
- test ( "we select three choices, and two reasons" , async ( ) => {
85
- const form = wrapper . getComponent ( EditForm ) ;
92
+ const expectedChoices = Object . keys ( AnnotationTags ) ;
93
+ expect ( form . findAll ( ".fr-tags-group--annotations > .annotation-choice" ) ) . toHaveLength ( expectedChoices . length ) ;
94
+ } ) ;
86
95
87
- // We have 5 tags
88
- await form . find ( `.fr-tags-group--annotations > .annotation--${ ANNOTATIONS . DOWNGRADED } button` ) . trigger ( "click" ) ;
89
- await form
90
- . find ( `.fr-tags-group--annotations > .annotation--${ ANNOTATIONS . REDUCED_CONVERSION_PERIOD } button` )
91
- . trigger ( "click" ) ;
92
- await form . find ( `.fr-tags-group--annotations > .annotation--${ ANNOTATIONS . RISKY } button` ) . trigger ( "click" ) ;
96
+ test ( "we select three choices, and two reasons" , async ( ) => {
97
+ const form = wrapper . getComponent ( EditForm ) ;
98
+
99
+ // We have 5 tags
100
+ await form . find ( `.fr-tags-group--annotations > .annotation--${ ANNOTATIONS . DOWNGRADED } button` ) . trigger ( "click" ) ;
101
+ await form
102
+ . find ( `.fr-tags-group--annotations > .annotation--${ ANNOTATIONS . REDUCED_CONVERSION_PERIOD } button` )
103
+ . trigger ( "click" ) ;
104
+ await form . find ( `.fr-tags-group--annotations > .annotation--${ ANNOTATIONS . RISKY } button` ) . trigger ( "click" ) ;
105
+
106
+ expect ( form . find ( "#downgraded_state" ) . element . value ) . toEqual ( CERTIFICATION_BODY_DECISION . PENDING ) ;
107
+ expect ( form . find ( "#downgraded_state" ) . element . selectedOptions [ 0 ] . textContent ) . toEqual ( "En cours de traitement" ) ;
108
+
109
+ expect ( form . find ( "#reduced_conversion_period_state" ) . element . value ) . toEqual ( CERTIFICATION_BODY_DECISION . PENDING ) ;
110
+ expect ( form . find ( "#reduced_conversion_period_state" ) . element . selectedOptions [ 0 ] . textContent ) . toEqual (
111
+ "En cours de traitement" ,
112
+ ) ;
113
+
114
+ // we toggle and cancel the tag
115
+ await form . find ( `.fr-tags-group--annotations > .annotation--${ ANNOTATIONS . SURVEYED } button` ) . trigger ( "click" ) ;
116
+ await form . find ( `.fr-tags-group--annotations > .annotation--${ ANNOTATIONS . SURVEYED } button` ) . trigger ( "click" ) ;
117
+
118
+ await form . find ( "#reduced_conversion_period_state" ) . setValue ( CERTIFICATION_BODY_DECISION . REJECTED ) ;
119
+ await form . find ( "#downgraded_state" ) . setValue ( CERTIFICATION_BODY_DECISION . ACCEPTED ) ;
120
+
121
+ // click and assess server update
122
+ axios . __createMock . patch . mockResolvedValueOnce ( { data : record } ) ;
123
+ axios . __createMock . get . mockResolvedValueOnce ( { data : record } ) ;
124
+ await form . find ( ".fr-modal__footer button.fr-btn" ) . trigger ( "click" ) ;
125
+
126
+ await flushPromises ( ) ;
127
+ expect ( wrapper . findComponent ( EditForm ) . exists ( ) ) . toEqual ( false ) ;
128
+ expect ( axios . __createMock . patch ) . toHaveBeenCalled ( ) ;
129
+ expect ( axios . __createMock . patch . mock . lastCall ) . toMatchObject ( [
130
+ "/v2/audits/054f0d70-c3da-448f-823e-81fcf7c2bf6e/parcelles/2" ,
131
+ {
132
+ properties : {
133
+ annotations : [
134
+ {
135
+ code : ANNOTATIONS . DOWNGRADED ,
136
+ metadata : {
137
+ [ ANNOTATIONS . METADATA_STATE ] : CERTIFICATION_BODY_DECISION . ACCEPTED ,
138
+ } ,
139
+ } ,
140
+ {
141
+ code : ANNOTATIONS . REDUCED_CONVERSION_PERIOD ,
142
+ metadata : {
143
+ [ ANNOTATIONS . METADATA_STATE ] : CERTIFICATION_BODY_DECISION . REJECTED ,
144
+ } ,
145
+ } ,
146
+ {
147
+ code : ANNOTATIONS . RISKY ,
148
+ } ,
149
+ ] ,
150
+ } ,
151
+ } ,
152
+ {
153
+ headers : {
154
+ "If-Unmodified-Since" : expect . any ( String ) ,
155
+ } ,
156
+ } ,
157
+ ] ) ;
158
+ } ) ;
159
+ } ) ;
93
160
94
- expect ( form . find ( "#downgraded_state" ) . element . value ) . toEqual ( CERTIFICATION_BODY_DECISION . PENDING ) ;
95
- expect ( form . find ( "#downgraded_state" ) . element . selectedOptions [ 0 ] . textContent ) . toEqual ( "En cours de traitement" ) ;
161
+ describe ( "item is readonly" , async ( ) => {
162
+ beforeAll ( async ( ) => {
163
+ userStore . user = {
164
+ organismeCertificateur : {
165
+ id : 2 ,
166
+ } ,
167
+ } ;
168
+ } ) ;
169
+ test ( "certification state are disabled" , async ( ) => {
170
+ const form = wrapper . getComponent ( EditForm ) ;
171
+
172
+ expect ( form . find ( `#conversion-${ LEVEL_CONVENTIONAL } ` ) . isDisabled ( ) ) . toEqual ( true ) ;
173
+ expect ( form . find ( `#conversion-${ LEVEL_C1 } ` ) . isDisabled ( ) ) . toEqual ( true ) ;
174
+ expect ( form . find ( `#conversion-${ LEVEL_C2 } ` ) . isDisabled ( ) ) . toEqual ( true ) ;
175
+ expect ( form . find ( `#conversion-${ LEVEL_C3 } ` ) . isDisabled ( ) ) . toEqual ( true ) ;
176
+ expect ( form . find ( `#conversion-${ LEVEL_AB } ` ) . isDisabled ( ) ) . toEqual ( true ) ;
177
+ } ) ;
96
178
97
- expect ( form . find ( "#reduced_conversion_period_state" ) . element . value ) . toEqual ( CERTIFICATION_BODY_DECISION . PENDING ) ;
98
- expect ( form . find ( "#reduced_conversion_period_state" ) . element . selectedOptions [ 0 ] . textContent ) . toEqual (
99
- "En cours de traitement" ,
100
- ) ;
179
+ test ( "annotations are disabled" , async ( ) => {
180
+ const form = wrapper . getComponent ( EditForm ) ;
101
181
102
- // we toggle and cancel the tag
103
- await form . find ( ` .fr-tags-group--annotations > .annotation-- ${ ANNOTATIONS . SURVEYED } button` ) . trigger ( "click" ) ;
104
- await form . find ( ` .fr-tags-group--annotations > .annotation--${ ANNOTATIONS . SURVEYED } button` ) . trigger ( "click ") ;
182
+ // We have all the tags tags and no expand button
183
+ expect ( form . findAll ( " .fr-tags-group--annotations > .annotation-choice" ) ) . toHaveLength ( 5 ) ;
184
+ expect ( form . find ( " .fr-tags-group--annotations > .annotation--more" ) . attributes ( ) ) . toHaveProperty ( "hidden ") ;
105
185
106
- await form . find ( "#reduced_conversion_period_state" ) . setValue ( CERTIFICATION_BODY_DECISION . REJECTED ) ;
107
- await form . find ( "#downgraded_state" ) . setValue ( CERTIFICATION_BODY_DECISION . ACCEPTED ) ;
186
+ // expect( form.find('.fr-tags-group--annotations > .annotation--more').attributes()).not.toHaveProperty('hidden')
187
+ // await form.find('.fr-tags-group--annotations > .annotation--more button').trigger('click')
108
188
109
- // click and assess server update
110
- axios . __createMock . patch . mockResolvedValueOnce ( { data : record } ) ;
111
- axios . __createMock . get . mockResolvedValueOnce ( { data : record } ) ;
112
- await form . find ( ".fr-modal__footer button.fr-btn" ) . trigger ( "click" ) ;
189
+ const expectedChoices = Object . keys ( AnnotationTags ) ;
190
+ expect ( form . findAll ( ".fr-tags-group--annotations > .annotation-choice" ) ) . toHaveLength ( expectedChoices . length ) ;
113
191
114
- await flushPromises ( ) ;
115
- expect ( wrapper . findComponent ( EditForm ) . exists ( ) ) . toEqual ( false ) ;
116
- expect ( axios . __createMock . patch ) . toHaveBeenCalled ( ) ;
117
- expect ( axios . __createMock . patch . mock . lastCall ) . toMatchObject ( [
118
- "/v2/audits/054f0d70-c3da-448f-823e-81fcf7c2bf6e/parcelles/2" ,
119
- {
120
- properties : {
121
- annotations : [
122
- {
123
- code : ANNOTATIONS . DOWNGRADED ,
124
- metadata : {
125
- [ ANNOTATIONS . METADATA_STATE ] : CERTIFICATION_BODY_DECISION . ACCEPTED ,
126
- } ,
127
- } ,
128
- {
129
- code : ANNOTATIONS . REDUCED_CONVERSION_PERIOD ,
130
- metadata : {
131
- [ ANNOTATIONS . METADATA_STATE ] : CERTIFICATION_BODY_DECISION . REJECTED ,
132
- } ,
133
- } ,
134
- {
135
- code : ANNOTATIONS . RISKY ,
136
- } ,
137
- ] ,
138
- } ,
139
- } ,
140
- {
141
- headers : {
142
- "If-Unmodified-Since" : expect . any ( String ) ,
143
- } ,
144
- } ,
145
- ] ) ;
192
+ const annotations = form . findAll ( ".fr-tags-group--annotations > .annotation-choice > button" ) ;
193
+ expect ( annotations [ 0 ] . isDisabled ( ) ) . toEqual ( true ) ;
194
+ expect ( annotations [ 1 ] . isDisabled ( ) ) . toEqual ( true ) ;
195
+ expect ( annotations [ 2 ] . isDisabled ( ) ) . toEqual ( true ) ;
196
+ expect ( annotations [ 3 ] . isDisabled ( ) ) . toEqual ( true ) ;
197
+ expect ( annotations [ 4 ] . isDisabled ( ) ) . toEqual ( true ) ;
198
+ } ) ;
146
199
} ) ;
147
200
} ) ;
0 commit comments