@internetarchive/ia-book-actions 0.0.29 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/demo/index.html +55 -10
- package/package.json +5 -4
- package/src/assets/styles/collapsible-action-group.js +2 -1
- package/src/components/collapsible-action-group.js +27 -0
- package/src/components/info-icon.js +0 -9
- package/src/components/text-group.js +1 -1
- package/src/components/timer-countdown.js +178 -0
- package/src/core/config/analytics-event-and-category.js +1 -0
- package/src/core/config/sentry-events.js +20 -0
- package/src/core/services/actions-config.js +2 -2
- package/src/core/services/actions-handler/actions-handler-service.js +5 -2
- package/src/core/services/actions-handler/actions-handler.js +63 -5
- package/src/core/services/get-lending-actions.js +6 -11
- package/src/core/services/loan-renew-helper.js +161 -0
- package/src/core/services/loan-token-poller.js +19 -10
- package/src/ia-book-actions.js +339 -41
- package/test/components/collapsible-action-group.test.js +2 -2
- package/test/components/timer-countdown.test.js +45 -0
- package/test/core/services/get-lending-actions.test.js +4 -4
- package/test/core/services/loan-renew-helper.test.js +86 -0
- package/test/ia-book-actions.test.js +75 -12
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { expect, aTimeout } from '@open-wc/testing';
|
|
2
|
+
|
|
3
|
+
import { LocalCache } from '@internetarchive/local-cache';
|
|
4
|
+
import { LoanRenewHelper } from '../../../src/core/services/loan-renew-helper.js';
|
|
5
|
+
|
|
6
|
+
const identifier = 'booBar';
|
|
7
|
+
const loanRenewTimeConfig = {
|
|
8
|
+
loanTotalTime: 13,
|
|
9
|
+
loanRenewAtLast: 10,
|
|
10
|
+
pageChangedInLast: 12,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// localCache used for auto-loan-renew
|
|
14
|
+
const localCache = new LocalCache({
|
|
15
|
+
namespace: 'loanRenew',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
await localCache.set({
|
|
20
|
+
key: `${identifier}-loanTime`,
|
|
21
|
+
value: new Date(
|
|
22
|
+
new Date().getTime() + loanRenewTimeConfig.loanTotalTime * 1000
|
|
23
|
+
),
|
|
24
|
+
ttl: Number(loanRenewTimeConfig.loanTotalTime),
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('Loan Renew Determine', () => {
|
|
29
|
+
it('flip book page while reading a book', async () => {
|
|
30
|
+
await aTimeout(1500);
|
|
31
|
+
|
|
32
|
+
await localCache.set({
|
|
33
|
+
key: `${identifier}-pageChangedTime`,
|
|
34
|
+
value: new Date(), // current time
|
|
35
|
+
ttl: Number(loanRenewTimeConfig.loanTotalTime),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const loanRenewHelper = new LoanRenewHelper(
|
|
39
|
+
true, // hasPageChanged
|
|
40
|
+
identifier,
|
|
41
|
+
localCache,
|
|
42
|
+
loanRenewTimeConfig
|
|
43
|
+
);
|
|
44
|
+
await loanRenewHelper.handleLoanRenew();
|
|
45
|
+
|
|
46
|
+
expect(loanRenewHelper.result.renewNow).to.be.false;
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('auto renewed book when user is currently reading', async () => {
|
|
50
|
+
await localCache.set({
|
|
51
|
+
key: `${identifier}-pageChangedTime`,
|
|
52
|
+
value: new Date(), // current time
|
|
53
|
+
ttl: Number(loanRenewTimeConfig.loanTotalTime),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
await aTimeout(1500);
|
|
57
|
+
|
|
58
|
+
const loanRenewHelper = new LoanRenewHelper(
|
|
59
|
+
false, // timer-countdown event
|
|
60
|
+
identifier,
|
|
61
|
+
localCache,
|
|
62
|
+
loanRenewTimeConfig
|
|
63
|
+
);
|
|
64
|
+
await loanRenewHelper.handleLoanRenew();
|
|
65
|
+
|
|
66
|
+
expect(loanRenewHelper.result.renewNow).to.be.true;
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('get toast message template', async () => {
|
|
70
|
+
const loanRenewHelper = new LoanRenewHelper(
|
|
71
|
+
false,
|
|
72
|
+
identifier,
|
|
73
|
+
localCache,
|
|
74
|
+
loanRenewTimeConfig
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const toastMsg = loanRenewHelper.getMessageTexts(
|
|
78
|
+
loanRenewHelper.loanReturnWarning,
|
|
79
|
+
'65' // in seconds
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
expect(toastMsg).to.be.equal(
|
|
83
|
+
'This book will be automatically returned in 2 minutes unless you turn a page.'
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -4,11 +4,17 @@ import '../src/ia-book-actions.js';
|
|
|
4
4
|
import Sinon from 'sinon';
|
|
5
5
|
|
|
6
6
|
import { SharedResizeObserver } from '@internetarchive/shared-resize-observer';
|
|
7
|
+
import { LocalCache } from '@internetarchive/local-cache';
|
|
7
8
|
|
|
8
9
|
afterEach(() => {
|
|
9
10
|
Sinon.restore();
|
|
10
11
|
});
|
|
11
12
|
|
|
13
|
+
// localCache used for auto-loan-renew
|
|
14
|
+
const localCache = new LocalCache({
|
|
15
|
+
namespace: 'loanRenew',
|
|
16
|
+
});
|
|
17
|
+
|
|
12
18
|
const container = ({ userid, identifier, lendingStatus = {} } = {}) =>
|
|
13
19
|
html`<ia-book-actions
|
|
14
20
|
.userid=${userid}
|
|
@@ -81,7 +87,7 @@ describe('Primary Actions data', () => {
|
|
|
81
87
|
);
|
|
82
88
|
const expectedPrimaryActions = [
|
|
83
89
|
{
|
|
84
|
-
text: 'Borrow for 1 hour'
|
|
90
|
+
text: 'Borrow', // changed from 'Borrow for 1 hour'
|
|
85
91
|
callback: () => {},
|
|
86
92
|
className: 'ia-button primary',
|
|
87
93
|
},
|
|
@@ -161,7 +167,7 @@ describe('Borrow status actions', () => {
|
|
|
161
167
|
|
|
162
168
|
// removed available_to_borrow from lending bar
|
|
163
169
|
expect(el.lendingStatus.available_to_borrow).to.be.false;
|
|
164
|
-
expect(el.primaryActions[0].text).to.equal('Borrow
|
|
170
|
+
expect(el.primaryActions[0].text).to.equal('Borrow');
|
|
165
171
|
});
|
|
166
172
|
|
|
167
173
|
it('Check action for borrowable book without user', async () => {
|
|
@@ -182,9 +188,7 @@ describe('Borrow status actions', () => {
|
|
|
182
188
|
},
|
|
183
189
|
];
|
|
184
190
|
|
|
185
|
-
expect(el.primaryTitle).to.equal(
|
|
186
|
-
'Renewable every hour, pending availability.'
|
|
187
|
-
);
|
|
191
|
+
expect(el.primaryTitle).to.equal('');
|
|
188
192
|
expect(el.primaryActions.length).to.equal(2);
|
|
189
193
|
expect(el.primaryActions[0].text).to.equal('Log In and Borrow');
|
|
190
194
|
expect(el.primaryActions[0].text).to.equal(expectedPrimaryActions[0].text);
|
|
@@ -232,9 +236,14 @@ describe('Browsing expired status', () => {
|
|
|
232
236
|
},
|
|
233
237
|
})
|
|
234
238
|
);
|
|
235
|
-
expect(el.primaryTitle).contains('
|
|
239
|
+
expect(el.primaryTitle).contains('');
|
|
236
240
|
expect(el.primaryActions[0].text).to.equal('Return now');
|
|
237
241
|
expect(el.primaryActions[1].text).to.equal('Print Disability Access');
|
|
242
|
+
|
|
243
|
+
// default params of one-hour loan renew
|
|
244
|
+
expect(el.loanRenewResult.texts).to.equal('');
|
|
245
|
+
expect(el.loanRenewResult.renewNow).to.equal(false);
|
|
246
|
+
expect(el.loanRenewResult.secondsLeft).to.equal(0);
|
|
238
247
|
});
|
|
239
248
|
|
|
240
249
|
it('Book is browsing and going to expire after 1 second', async () => {
|
|
@@ -250,11 +259,23 @@ describe('Browsing expired status', () => {
|
|
|
250
259
|
})
|
|
251
260
|
);
|
|
252
261
|
|
|
262
|
+
// timer-countdown is still active
|
|
263
|
+
expect(el.shadowRoot.querySelector('timer-countdown')).to.not.be.null;
|
|
264
|
+
|
|
253
265
|
await aTimeout(1500); // wait for 1.5 second
|
|
254
266
|
await el.updateComplete;
|
|
255
267
|
|
|
256
|
-
expect(el.
|
|
257
|
-
|
|
268
|
+
expect(el.primaryActions[0].text).to.equal('Borrow');
|
|
269
|
+
|
|
270
|
+
// timer-countdown is gone now
|
|
271
|
+
expect(el.shadowRoot.querySelector('timer-countdown')).to.be.null;
|
|
272
|
+
|
|
273
|
+
// // book has been expired
|
|
274
|
+
expect(el.loanRenewResult.renewNow).to.equal(false);
|
|
275
|
+
expect(el.loanRenewResult.secondsLeft).to.equal(0);
|
|
276
|
+
expect(el.loanRenewResult.texts).to.equal(
|
|
277
|
+
'This book has been automatically returned due to inactivity.'
|
|
278
|
+
);
|
|
258
279
|
});
|
|
259
280
|
|
|
260
281
|
it('Expiring book cancels interval & emits event', async () => {
|
|
@@ -279,9 +300,8 @@ describe('Browsing expired status', () => {
|
|
|
279
300
|
el.lendingStatus = browsingStatus;
|
|
280
301
|
await el.updateComplete;
|
|
281
302
|
|
|
282
|
-
expect(el.primaryTitle).contains('
|
|
303
|
+
expect(el.primaryTitle).contains('');
|
|
283
304
|
expect(el.primaryActions[0].text).to.equal('Return now');
|
|
284
|
-
expect(el.tokenPoller.loanTokenInterval).to.not.equal(undefined);
|
|
285
305
|
|
|
286
306
|
const expiredStatus = { ...browsingStatus, browsingExpired: true };
|
|
287
307
|
el.lendingStatus = expiredStatus;
|
|
@@ -289,12 +309,55 @@ describe('Browsing expired status', () => {
|
|
|
289
309
|
await aTimeout(1500); // wait for 1.5 sec
|
|
290
310
|
|
|
291
311
|
expect(eventReceived).to.equal(true);
|
|
292
|
-
expect(el.
|
|
293
|
-
expect(el.primaryActions[0].text).to.equal('Borrow again');
|
|
312
|
+
expect(el.primaryActions[0].text).to.equal('Borrow');
|
|
294
313
|
expect(el.tokenPoller.loanTokenInterval).to.equal(undefined);
|
|
295
314
|
});
|
|
296
315
|
});
|
|
297
316
|
|
|
317
|
+
describe('Auto renew one hour loan', () => {
|
|
318
|
+
it('Book is browsing and renewed it now', async () => {
|
|
319
|
+
const el = await fixture(
|
|
320
|
+
container({
|
|
321
|
+
userid: '@userid',
|
|
322
|
+
identifier: 'Foo',
|
|
323
|
+
lendingStatus: {
|
|
324
|
+
is_lendable: true,
|
|
325
|
+
user_has_browsed: true,
|
|
326
|
+
browseHasExpired: false,
|
|
327
|
+
secondsLeftOnLoan: 12,
|
|
328
|
+
},
|
|
329
|
+
})
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
el.loanRenewResult = {
|
|
333
|
+
texts: 'This book has been renewed for 1 hour.',
|
|
334
|
+
renewNow: true,
|
|
335
|
+
secondsLeft: 10,
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
await localCache.set({
|
|
339
|
+
key: `${el.identifier}-loanTime`,
|
|
340
|
+
value: new Date(new Date().getTime() + 10 * 1000),
|
|
341
|
+
ttl: Number(10),
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
const myEvent = new CustomEvent('loanAutoRenewed');
|
|
345
|
+
el.addEventListener('loanAutoRenewed', () => {
|
|
346
|
+
el.handleLoanAutoRenewed();
|
|
347
|
+
});
|
|
348
|
+
el.dispatchEvent(myEvent);
|
|
349
|
+
|
|
350
|
+
await aTimeout(1500); // wait for 1.5 second
|
|
351
|
+
await el.updateComplete;
|
|
352
|
+
|
|
353
|
+
expect(el.loanRenewResult.texts).to.equal(
|
|
354
|
+
'This book has been renewed for 1 hour.'
|
|
355
|
+
);
|
|
356
|
+
expect(el.loanRenewResult.renewNow).to.equal(true);
|
|
357
|
+
expect(el.loanRenewResult.secondsLeft).to.equal(10);
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
298
361
|
describe('Shared Resize Observer', () => {
|
|
299
362
|
it('can receive a Shared Resize Observer', async () => {
|
|
300
363
|
const sharedObserverStub = new SharedResizeObserver();
|