Skip to content

Commit

Permalink
Fix meta.results when search returns no results (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop authored Dec 8, 2022
1 parent 242ab17 commit 60bf4d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/plugins/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class PlaylistsRepository {
return new Page(items, {
pageSize: pagination.limit,
// `count` can be the empty array if the playlist has no items
filtered: count.length === 1 ? count[0].filtered : playlist.media.length,
filtered: filter ? (count[0]?.filtered ?? 0) : playlist.media.length,
total: playlist.media.length,

current: pagination,
Expand Down
56 changes: 56 additions & 0 deletions test/playlists.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,62 @@ describe('Playlists', () => {
assertItemsAndIncludedMedia(page4.body);
assertItemsAndIncludedMedia(page5.body);
});

it('supports filter strings', async () => {
const token = await uw.test.createTestSessionToken(user);

const items = await generateItems(7);
for (const item of items) {
item.title = `${item.title} matches_the_filter`;
}
await uw.playlists.addPlaylistItems(playlist, items);

const res = await supertest(uw.server)
.get(`/api/playlists/${playlist.id}/media?filter=matches_the_filter`)
.set('Cookie', `uwsession=${token}`)
.send()
.expect(200);

sinon.assert.match(res.body, {
meta: {
included: {
media: ['media'],
},
// Default page size
offset: 0,
pageSize: 100,
// Filtered size
results: 7,
// Playlist size
total: 207,
},
});
assert.strictEqual(res.body.data.length, 7);

assertItemsAndIncludedMedia(res.body);

const emptyRes = await supertest(uw.server)
.get(`/api/playlists/${playlist.id}/media?filter=matches_nothing_and_returns_empty`)
.set('Cookie', `uwsession=${token}`)
.send()
.expect(200);

sinon.assert.match(emptyRes.body, {
meta: {
included: {
media: ['media'],
},
// Default page size
offset: 0,
pageSize: 100,
// Filtered size
results: 0,
// Playlist size
total: 207,
},
});
assert.strictEqual(emptyRes.body.data.length, 0);
});
});

describe('POST /playlists/:id/media', () => {
Expand Down

0 comments on commit 60bf4d6

Please sign in to comment.