Skip to content

Commit 2d99e57

Browse files
committed
Control if next & previous actions are allowed in the fast API adapter
- Fix connectPortalRoutes named export
1 parent 0bf8b20 commit 2d99e57

File tree

3 files changed

+87
-30
lines changed

3 files changed

+87
-30
lines changed

components/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export { default as store } from '~core/store';
1818
export { default as bus } from '~core/eventBus';
1919

2020
export {
21-
connectPortalRoutesDict as connectPortalRoutesDict,
21+
connectPortalRoutesDict as connectPortalRoutes,
2222
} from '~constants/portal-routes';
2323

2424
export default (widgets = {}, options = {}) => {

tools/api/fastApi/adapter.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,22 @@ export const fastApiTableAdapter = (endpoint, rowsPerPage = 10) => {
5454
};
5555

5656
/**
57-
* @returns {Promise<{total: number, page: number, items: *[]}>}
57+
* @returns {{total: number, page: number, items: *[]}|Promise<{total: number, page: number, items: *[]}>}
5858
*/
5959
const next = () => {
60+
if (state.page >= Math.ceil(state.total / limit)) return state;
61+
6062
state.page++;
6163

6264
return load();
6365
};
6466

6567
/**
66-
* @returns {Promise<{total: number, page: number, items: *[]}>}
68+
* @returns {{total: number, page: number, items: *[]}|Promise<{total: number, page: number, items: *[]}>}
6769
*/
6870
const previous = () => {
71+
if (state.page <= 1) return state;
72+
6973
state.page--;
7074

7175
return load();

tools/api/fastApi/adapter.spec.js

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -111,47 +111,100 @@ describe('#fastApiTableAdapter', () => {
111111
});
112112

113113
describe('#next', () => {
114-
beforeEach(async () => {
115-
fetchResponse.contentRangeTotal = 1;
116-
fetchResponse.items = ['foo'];
114+
describe('if there is a next page', () => {
115+
beforeEach(async () => {
116+
fetchResponse.contentRangeTotal = 11;
117+
fetchResponse.items = ['foo'];
117118

118-
result = await adapter.next();
119-
});
119+
await adapter.load();
120+
result = await adapter.next();
121+
});
120122

121-
it('increases the current page and performs a request', () => {
122-
expect(fetch).toHaveBeenCalledWith('/foo?limit=10&offset=10');
123+
it('increases the current page and performs a request', () => {
124+
expect(fetch).toHaveBeenCalledTimes(2);
125+
expect(fetch).toHaveBeenCalledWith('/foo?limit=10&offset=10');
126+
});
127+
128+
it('returns the result of calling load', () => {
129+
expect(result).toEqual({
130+
page: 2,
131+
total: 11,
132+
items: ['foo'],
133+
});
134+
});
123135
});
124136

125-
it('returns the result of calling load', () => {
126-
expect(result).toEqual({
127-
page: 2,
128-
total: 1,
129-
items: ['foo'],
137+
describe('if there is NOT a next page', () => {
138+
beforeEach(async () => {
139+
fetchResponse.contentRangeTotal = 10;
140+
fetchResponse.items = ['foo'];
141+
142+
await adapter.load();
143+
result = await adapter.next();
144+
});
145+
146+
it('does not perform a request', () => {
147+
expect(fetch).toHaveBeenCalledTimes(1);
148+
});
149+
150+
it('returns the current state', () => {
151+
expect(result).toEqual({
152+
page: 1,
153+
total: 10,
154+
items: ['foo'],
155+
});
130156
});
131157
});
132158
});
133159

134160
describe('#previous', () => {
135-
beforeEach(async () => {
136-
fetchResponse.contentRangeTotal = 1;
137-
fetchResponse.items = ['bar'];
161+
describe('if there is a previous page', () => {
162+
beforeEach(async () => {
163+
fetchResponse.contentRangeTotal = 33;
164+
fetchResponse.items = ['bar'];
138165

139-
// Increase page to 3
140-
await adapter.next();
141-
await adapter.next();
166+
// Increase page to 3
167+
await adapter.load();
168+
await adapter.next();
169+
await adapter.next();
142170

143-
result = await adapter.previous();
144-
});
171+
result = await adapter.previous();
172+
});
145173

146-
it('decreases the current page and performs a request', () => {
147-
expect(fetch).toHaveBeenCalledWith('/foo?limit=10&offset=20');
174+
it('decreases the current page and performs a request', () => {
175+
expect(fetch).toHaveBeenCalledWith('/foo?limit=10&offset=20');
176+
});
177+
178+
it('returns the result of calling load', () => {
179+
expect(result).toEqual({
180+
page: 2,
181+
total: 33,
182+
items: ['bar'],
183+
});
184+
});
148185
});
149186

150-
it('returns the result of calling load', () => {
151-
expect(result).toEqual({
152-
page: 2,
153-
total: 1,
154-
items: ['bar'],
187+
describe('if there is NOT a previous page', () => {
188+
beforeEach(async () => {
189+
fetchResponse.contentRangeTotal = 33;
190+
fetchResponse.items = ['bar'];
191+
192+
// Do not increase page
193+
await adapter.load();
194+
195+
result = await adapter.previous();
196+
});
197+
198+
it('does not performs a request', () => {
199+
expect(fetch).toHaveBeenCalledTimes(1);
200+
});
201+
202+
it('returns the current state', () => {
203+
expect(result).toEqual({
204+
page: 1,
205+
total: 33,
206+
items: ['bar'],
207+
});
155208
});
156209
});
157210
});

0 commit comments

Comments
 (0)