@@ -107,6 +107,74 @@ describe('dashboard', () => {
107107 expect ( dashboard . items ) . to . eql ( [ { id : '0' } ] ) ;
108108 } ) ;
109109
110+ it ( 'should dispatch a dashboard-item-before-remove event before removal' , ( ) => {
111+ const spy = sinon . spy ( ) ;
112+ dashboard . addEventListener ( 'dashboard-item-before-remove' , spy ) ;
113+ const widget = getElementFromCell ( dashboard , 0 , 1 ) ;
114+ getRemoveButton ( widget as DashboardWidget ) . click ( ) ;
115+ expect ( spy ) . to . be . calledOnce ;
116+ expect ( spy . firstCall . args [ 0 ] . detail . item ) . to . eql ( { id : '1' } ) ;
117+ expect ( spy . firstCall . args [ 0 ] . detail . items ) . to . eql ( [ { id : '0' } ] ) ; // contains the state after removal
118+ expect ( spy . firstCall . args [ 0 ] . detail . section ) . to . be . undefined ;
119+ } ) ;
120+
121+ it ( 'should include section in dashboard-item-before-remove event for nested items' , async ( ) => {
122+ const sectionItem : DashboardSectionItem < TestDashboardItem > = {
123+ title : 'Section' ,
124+ items : [ { id : '2' } , { id : '3' } ] ,
125+ } ;
126+ dashboard . items = [ { id : '0' } , { id : '1' } , sectionItem ] ;
127+ await updateComplete ( dashboard ) ;
128+
129+ const spy = sinon . spy ( ) ;
130+ dashboard . addEventListener ( 'dashboard-item-before-remove' , spy ) ;
131+ const widget = getElementFromCell ( dashboard , 1 , 0 ) ;
132+ const section = widget ?. closest ( 'vaadin-dashboard-section' ) ;
133+ expect ( section ) . to . be . ok ;
134+ getRemoveButton ( widget as DashboardWidget ) . click ( ) ;
135+
136+ expect ( spy ) . to . be . calledOnce ;
137+ expect ( spy . firstCall . args [ 0 ] . detail . item ) . to . eql ( { id : '2' } ) ;
138+ expect ( spy . firstCall . args [ 0 ] . detail . section ) . to . equal ( sectionItem ) ;
139+ } ) ;
140+
141+ it ( 'should cancel removal when preventDefault is called on dashboard-item-before-remove' , ( ) => {
142+ const originalItems = dashboard . items ;
143+ dashboard . addEventListener ( 'dashboard-item-before-remove' , ( e ) => {
144+ e . preventDefault ( ) ;
145+ } ) ;
146+ const widget = getElementFromCell ( dashboard , 0 , 1 ) ;
147+ getRemoveButton ( widget as DashboardWidget ) . click ( ) ;
148+ expect ( dashboard . items ) . to . equal ( originalItems ) ;
149+ expect ( dashboard . items ) . to . eql ( [ { id : '0' } , { id : '1' } ] ) ;
150+ } ) ;
151+
152+ it ( 'should not fire dashboard-item-removed when dashboard-item-before-remove is prevented' , ( ) => {
153+ const beforeRemoveSpy = sinon . spy ( ) ;
154+ const removedSpy = sinon . spy ( ) ;
155+ dashboard . addEventListener ( 'dashboard-item-before-remove' , ( e ) => {
156+ beforeRemoveSpy ( ) ;
157+ e . preventDefault ( ) ;
158+ } ) ;
159+ dashboard . addEventListener ( 'dashboard-item-removed' , removedSpy ) ;
160+ const widget = getElementFromCell ( dashboard , 0 , 1 ) ;
161+ getRemoveButton ( widget as DashboardWidget ) . click ( ) ;
162+ expect ( beforeRemoveSpy ) . to . be . calledOnce ;
163+ expect ( removedSpy ) . to . not . be . called ;
164+ } ) ;
165+
166+ it ( 'should fire both events in correct order when not prevented' , ( ) => {
167+ const beforeRemoveSpy = sinon . spy ( ) ;
168+ const removedSpy = sinon . spy ( ) ;
169+ dashboard . addEventListener ( 'dashboard-item-before-remove' , beforeRemoveSpy ) ;
170+ dashboard . addEventListener ( 'dashboard-item-removed' , removedSpy ) ;
171+ const widget = getElementFromCell ( dashboard , 0 , 1 ) ;
172+ getRemoveButton ( widget as DashboardWidget ) . click ( ) ;
173+ expect ( beforeRemoveSpy ) . to . be . calledOnce ;
174+ expect ( removedSpy ) . to . be . calledOnce ;
175+ expect ( beforeRemoveSpy ) . to . have . been . calledBefore ( removedSpy ) ;
176+ } ) ;
177+
110178 it ( 'should dispatch an dashboard-item-removed event' , ( ) => {
111179 const spy = sinon . spy ( ) ;
112180 dashboard . addEventListener ( 'dashboard-item-removed' , spy ) ;
@@ -115,6 +183,27 @@ describe('dashboard', () => {
115183 expect ( spy ) . to . be . calledOnce ;
116184 expect ( spy . firstCall . args [ 0 ] . detail . item ) . to . eql ( { id : '1' } ) ;
117185 expect ( spy . firstCall . args [ 0 ] . detail . items ) . to . eql ( [ { id : '0' } ] ) ;
186+ expect ( spy . firstCall . args [ 0 ] . detail . section ) . to . be . undefined ;
187+ } ) ;
188+
189+ it ( 'should include section in dashboard-item-removed event for nested items' , async ( ) => {
190+ const sectionItem : DashboardSectionItem < TestDashboardItem > = {
191+ title : 'Section' ,
192+ items : [ { id : '2' } , { id : '3' } ] ,
193+ } ;
194+ dashboard . items = [ { id : '0' } , { id : '1' } , sectionItem ] ;
195+ await updateComplete ( dashboard ) ;
196+
197+ const spy = sinon . spy ( ) ;
198+ dashboard . addEventListener ( 'dashboard-item-removed' , spy ) ;
199+ const widget = getElementFromCell ( dashboard , 1 , 0 ) ;
200+ const section = widget ?. closest ( 'vaadin-dashboard-section' ) ;
201+ expect ( section ) . to . be . ok ;
202+ getRemoveButton ( widget as DashboardWidget ) . click ( ) ;
203+
204+ expect ( spy ) . to . be . calledOnce ;
205+ expect ( spy . firstCall . args [ 0 ] . detail . item ) . to . eql ( { id : '2' } ) ;
206+ expect ( spy . firstCall . args [ 0 ] . detail . section ) . to . equal ( sectionItem ) ;
118207 } ) ;
119208
120209 it ( 'should not dispatch an item-remove event' , async ( ) => {
0 commit comments