Skip to content

Commit

Permalink
fix: handle zeros in paginated meta info correctly (#82)
Browse files Browse the repository at this point in the history
Co-authored-by: Anbraten <anton@ju60.de>
  • Loading branch information
mariusheine and anbraten authored Dec 12, 2023
1 parent c39becb commit a904d5c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ export type ServiceModel<

export function isPaginated<T>(response: T | T[] | Paginated<T>): response is Paginated<T> {
const { total, limit, skip, data } = response as Paginated<T>;
return total !== undefined && limit !== undefined && skip !== undefined && data !== undefined && Array.isArray(data);
return (
typeof total === 'number' &&
typeof limit === 'number' &&
(typeof skip === 'number' || typeof skip === 'string') &&
Array.isArray(data)
);
}
92 changes: 91 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { getId, PotentialIds } from '~/utils';
import { getId, isPaginated, PotentialIds } from '~/utils';

describe('getId()', () => {
it('should return "id" property from item', () => {
Expand All @@ -27,3 +27,93 @@ describe('getId()', () => {
expect(() => getId(item as PotentialIds)).toThrow('Unable to retrieve id from item');
});
});

describe('isPaginated()', () => {
it('should return true when total, limit, skip and data array are present', () => {
// given
const response = {
total: 100,
limit: 10,
skip: 20,
data: [],
};

// then
expect(isPaginated<typeof response>(response)).toBe(true);
});

it('should return true when total, limit and skip is "0"', () => {
// given
const response = {
total: 0,
limit: 0,
skip: 0,
data: [],
};

// then
expect(isPaginated<typeof response>(response)).toBe(true);
});

it('should return true when skip is a string', () => {
// given
const response = {
total: 100,
limit: 10,
skip: '20',
data: [],
};

// then
expect(isPaginated<typeof response>(response)).toBe(true);
});

it('should return false when total is missing', () => {
// given
const response = {
limit: 10,
skip: 20,
data: [],
};

// then
expect(isPaginated<typeof response>(response)).toBe(false);
});

it('should return false when limit is missing', () => {
// given
const response = {
total: 100,
skip: 20,
data: [],
};

// then
expect(isPaginated<typeof response>(response)).toBe(false);
});

it('should return false when skip is missing', () => {
// given
const response = {
total: 100,
limit: 10,
data: [],
};

// then
expect(isPaginated<typeof response>(response)).toBe(false);
});

it('should return false when data is no array', () => {
// given
const response = {
total: 100,
limit: 10,
skip: 20,
data: {},
};

// then
expect(isPaginated<typeof response>(response)).toBe(false);
});
});

0 comments on commit a904d5c

Please sign in to comment.