@internetarchive/ia-book-actions 1.4.4-alpha9 → 1.4.5

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.
@@ -17,18 +17,17 @@ afterEach(() => {
17
17
 
18
18
  describe('Get Loan Token', () => {
19
19
  it('get loan token for browsed books', async () => {
20
- // id, borrowType, successCallback, errorCallback, pollerDelay
21
- const tokenPoller = new LoanTokenPoller(
22
- 'identifier1',
23
- 'browsed',
24
- () => {
20
+ const tokenPoller = new LoanTokenPoller({
21
+ identifier: 'identifier1',
22
+ borrowType: 'browsed',
23
+ successCallback: () => {
25
24
  console.log('success callback is executed!');
26
25
  },
27
- () => {
26
+ errorCallback: () => {
28
27
  console.log('error callback is executed!');
29
28
  },
30
- 2000 // 2 minutes'
31
- );
29
+ pollerDelay: 2000, // 2 minutes'
30
+ });
32
31
  const successCallbackSpy = Sinon.stub(tokenPoller, 'successCallback');
33
32
  successCallbackSpy();
34
33
  expect(successCallbackSpy.callCount).to.equal(1);
@@ -39,13 +38,13 @@ describe('Get Loan Token', () => {
39
38
  });
40
39
 
41
40
  it('get loan token for admin borrowed books', async () => {
42
- const tokenPoller = new LoanTokenPoller(
43
- 'identifier1',
44
- 'adminBorrowed',
45
- () => {},
46
- () => {},
47
- 2000 // 2 minutes'
48
- );
41
+ const tokenPoller = new LoanTokenPoller({
42
+ identifier: 'identifier1',
43
+ borrowType: 'adminBorrowed',
44
+ successCallback: () => {},
45
+ errorCallback: () => {},
46
+ pollerDelay: 2000, // 2 minutes'
47
+ });
49
48
 
50
49
  const successCallbackSpy = Sinon.stub(tokenPoller, 'successCallback');
51
50
  successCallbackSpy();
@@ -56,3 +55,100 @@ describe('Get Loan Token', () => {
56
55
  expect(tokenPoller.loanTokenInterval).to.equal(undefined);
57
56
  });
58
57
  });
58
+
59
+ describe('handleTokenError (no retry)', () => {
60
+ // A create_token failure is treated as terminal on the first attempt —
61
+ // no retry, whether it's the "stale loan read" race or anything else.
62
+ const makeTokenPoller = () =>
63
+ new LoanTokenPoller({
64
+ identifier: 'identifier1',
65
+ borrowType: 'browsed',
66
+ successCallback: () => {},
67
+ errorCallback: () => {},
68
+ pollerDelay: 2000,
69
+ });
70
+
71
+ it('reports the error immediately, without retrying, on the initial call', () => {
72
+ const tokenPoller = makeTokenPoller();
73
+ const handleLoanTokenPollerSpy = Sinon.spy(
74
+ tokenPoller,
75
+ 'handleLoanTokenPoller'
76
+ );
77
+ const errorCallbackSpy = Sinon.spy(tokenPoller, 'errorCallback');
78
+
79
+ tokenPoller.handleTokenError(
80
+ { error: 'You do not currently have this book borrowed.' },
81
+ true
82
+ );
83
+
84
+ expect(errorCallbackSpy.calledOnce).to.be.true;
85
+ expect(handleLoanTokenPollerSpy.called).to.be.false;
86
+ });
87
+
88
+ it('reports the error immediately on a routine (non-initial) poll too', () => {
89
+ const tokenPoller = makeTokenPoller();
90
+ const errorCallbackSpy = Sinon.spy(tokenPoller, 'errorCallback');
91
+
92
+ tokenPoller.handleTokenError(
93
+ { error: 'You do not currently have this book borrowed.' },
94
+ false
95
+ );
96
+
97
+ expect(errorCallbackSpy.calledOnce).to.be.true;
98
+ });
99
+
100
+ it('passes isInitial through to the error callback', () => {
101
+ const errorCallback = Sinon.spy();
102
+ const poller = new LoanTokenPoller({
103
+ identifier: 'foo',
104
+ borrowType: 'browsed',
105
+ successCallback: () => {},
106
+ errorCallback,
107
+ pollerDelay: 120,
108
+ });
109
+ poller.disconnectedCallback();
110
+
111
+ poller.handleTokenError({ error: 'something else went wrong' }, true);
112
+
113
+ expect(errorCallback.calledOnce).to.be.true;
114
+ expect(errorCallback.firstCall.args[0].detail.isInitial).to.be.true;
115
+ });
116
+ });
117
+
118
+ describe('skipInitialCall option', () => {
119
+ it('starts the recurring interval but fires no immediate create_token call', () => {
120
+ const poller = new LoanTokenPoller({
121
+ identifier: 'foo',
122
+ borrowType: 'browsed',
123
+ successCallback: () => {},
124
+ errorCallback: () => {},
125
+ pollerDelay: 120,
126
+ skipInitialCall: true,
127
+ });
128
+ const handleLoanTokenPollerSpy = Sinon.spy(poller, 'handleLoanTokenPoller');
129
+
130
+ // bookAccessed() already ran in the constructor above (before the spy
131
+ // was attached) -- re-run it to observe whether it calls out.
132
+ poller.bookAccessed();
133
+
134
+ expect(handleLoanTokenPollerSpy.called).to.be.false;
135
+ expect(window.IALendingIntervals.tokenPoller).to.not.equal(0);
136
+ poller.disconnectedCallback();
137
+ });
138
+
139
+ it('fires the immediate call as usual when skipInitialCall is not set', () => {
140
+ const poller = new LoanTokenPoller({
141
+ identifier: 'foo',
142
+ borrowType: 'browsed',
143
+ successCallback: () => {},
144
+ errorCallback: () => {},
145
+ pollerDelay: 120,
146
+ });
147
+ const handleLoanTokenPollerSpy = Sinon.spy(poller, 'handleLoanTokenPoller');
148
+
149
+ poller.bookAccessed();
150
+
151
+ expect(handleLoanTokenPollerSpy.calledWith(true)).to.be.true;
152
+ poller.disconnectedCallback();
153
+ });
154
+ });