1
1
import { expect } from "chai" ;
2
+ import * as sinon from "sinon" ;
2
3
import { stub } from "sinon" ;
3
4
import * as React from "react" ;
4
- import { shallow , mount } from "enzyme" ;
5
+ import { ReactWrapper , mount } from "enzyme" ;
5
6
import * as PropTypes from "prop-types" ;
6
-
7
+ import { ProtocolData } from "../../interfaces" ;
7
8
import Admin from "../../models/Admin" ;
8
- import { Collections } from "../Collections" ;
9
+ import { Collections , CollectionEditForm } from "../Collections" ;
10
+ import buildStore from "../../store" ;
9
11
10
12
const collections = [
11
13
{
@@ -27,7 +29,7 @@ const collections = [
27
29
name : "RBDigital" ,
28
30
} ,
29
31
] ;
30
- import buildStore from "../../store" ;
32
+ const protocols : ProtocolData [ ] = [ { name : "test protocol" , settings : [ ] } ] ;
31
33
32
34
describe ( "Collections" , ( ) => {
33
35
let wrapper ;
@@ -127,5 +129,74 @@ describe("Collections", () => {
127
129
expect ( deletedCollection . find ( "a.edit-item" ) . length ) . to . equal ( 0 ) ;
128
130
expect ( deletedCollection . find ( "button.delete-item" ) . length ) . to . equal ( 0 ) ;
129
131
} ) ;
132
+
133
+ describe ( "confirm before disassociating libraries" , ( ) => {
134
+ let wrapper : ReactWrapper ;
135
+ let confirmStub : sinon . SinonStub ;
136
+
137
+ const initialLibraries = [
138
+ { short_name : "palace" , name : "Palace" } ,
139
+ { short_name : "another-library" , name : "Another Library" } ,
140
+ ] as const ;
141
+ const collection = {
142
+ id : 7 ,
143
+ name : "An OPDS Collection" ,
144
+ protocol : "OPDS Import" ,
145
+
146
+ libraries : [ ...initialLibraries ] ,
147
+ } ;
148
+
149
+ beforeEach ( ( ) => {
150
+ confirmStub = sinon . stub ( window , "confirm" ) ;
151
+
152
+ wrapper = mount (
153
+ < CollectionEditForm
154
+ disabled = { false }
155
+ data = { {
156
+ collections : [ collection ] ,
157
+ protocols : [ ] ,
158
+ allLibraries : [ ...initialLibraries ] ,
159
+ } }
160
+ item = { collection }
161
+ urlBase = "/collections"
162
+ listDataKey = "collections"
163
+ />
164
+ ) ;
165
+ } ) ;
166
+
167
+ afterEach ( ( ) => {
168
+ confirmStub . restore ( ) ;
169
+ } ) ;
170
+
171
+ it ( "calls window.confirm when delete button is clicked" , ( ) => {
172
+ confirmStub . returns ( false ) ;
173
+
174
+ // The confirmation dialog should not be invoked before we click.
175
+ expect ( confirmStub . calledOnce ) . to . be . false ;
176
+
177
+ wrapper . find ( "button.remove-btn" ) . at ( 0 ) . simulate ( "click" ) ;
178
+ expect ( confirmStub . calledOnce ) . to . be . true ;
179
+ expect ( confirmStub . firstCall . args . length ) . to . equal ( 1 ) ;
180
+ const message : string = confirmStub . firstCall . args [ 0 ] ;
181
+ expect ( message ) . to . equal (
182
+ 'Disassociating library "Palace" from this collection will ' +
183
+ "remove all loans and holds for its patrons. Do you wish to continue?"
184
+ ) ;
185
+ } ) ;
186
+
187
+ it ( "does not delete library if confirmation is canceled" , ( ) => {
188
+ confirmStub . returns ( false ) ;
189
+ wrapper . find ( "button.remove-btn" ) . at ( 0 ) . simulate ( "click" ) ;
190
+ // We didn't delete, so we should still have the originals.
191
+ expect ( wrapper . state ( "libraries" ) ) . to . deep . equal ( initialLibraries ) ;
192
+ } ) ;
193
+
194
+ it ( "deletes library if confirmation is accepted" , ( ) => {
195
+ confirmStub . returns ( true ) ;
196
+ wrapper . find ( "button.remove-btn" ) . at ( 0 ) . simulate ( "click" ) ;
197
+ // We deleted the first library, so it should be gone from the state.
198
+ expect ( wrapper . state ( "libraries" ) ) . to . deep . equal ( [ initialLibraries [ 1 ] ] ) ;
199
+ } ) ;
200
+ } ) ;
130
201
} ) ;
131
202
} ) ;
0 commit comments