test(tabler.helpers): limt text and sort table functions

This commit is contained in:
Nicolas Meienberger 2023-03-18 15:37:08 +01:00 committed by Nicolas Meienberger
parent c872f1e22c
commit 3c01ed1249
3 changed files with 57 additions and 7 deletions

View file

@ -0,0 +1,50 @@
import { createAppConfig } from '../../../../../server/tests/apps.factory';
import { limitText, sortTable } from '../table.helpers';
import { AppTableData } from '../table.types';
describe('sortTable function', () => {
const app = createAppConfig({ name: 'a', categories: ['social'] });
const app2 = createAppConfig({ name: 'b', categories: ['network', 'automation'] });
const app3 = createAppConfig({ name: 'c', categories: ['network'] });
// Randomize the order of the apps
const data: AppTableData = [app3, app, app2];
it('should sort by name in ascending order', () => {
const sortedData = sortTable({ data, direction: 'asc', col: 'name', search: '' });
expect(sortedData).toEqual([app, app2, app3]);
});
it('should sort by name in descending order', () => {
const sortedData = sortTable({ data, direction: 'desc', col: 'name', search: '' });
expect(sortedData).toEqual([app3, app2, app]);
});
it('should filter by search term', () => {
const sortedData = sortTable({ data, direction: 'asc', col: 'name', search: 'b' });
expect(sortedData).toEqual([app2]);
});
it('should filter by category', () => {
const sortedData = sortTable({ data, direction: 'asc', col: 'name', search: '', category: 'automation' });
expect(sortedData).toEqual([app2]);
});
});
describe('limitText function', () => {
it('should limit the text to the given limit', () => {
const limitedText = limitText('Lorem ipsum dolor sit amet', 10);
expect(limitedText).toEqual('Lorem ipsu...');
});
it('should not limit the text if it is shorter than the limit', () => {
const limitedText = limitText('Lorem ipsum dolor sit amet', 100);
expect(limitedText).toEqual('Lorem ipsum dolor sit amet');
});
});

View file

@ -18,10 +18,8 @@ export const sortTable = (params: SortParams) => {
if (aVal < bVal) {
return direction === 'asc' ? -1 : 1;
}
if (aVal > bVal) {
return direction === 'asc' ? 1 : -1;
}
return 0;
return direction === 'asc' ? 1 : -1;
});
if (category) {

View file

@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { App, PrismaClient } from '@prisma/client';
import { Architecture } from '../core/TipiConfig/TipiConfig';
import { AppInfo, appInfoSchema } from '../services/apps/apps.helpers';
import { APP_CATEGORIES } from '../services/apps/apps.types';
import { AppCategory, APP_CATEGORIES } from '../services/apps/apps.types';
interface IProps {
installed?: boolean;
@ -17,6 +17,8 @@ interface IProps {
type CreateConfigParams = {
id?: string;
name?: string;
categories?: AppCategory[];
};
const createAppConfig = (props?: CreateConfigParams) =>
@ -24,13 +26,13 @@ const createAppConfig = (props?: CreateConfigParams) =>
id: props?.id || faker.random.alphaNumeric(32),
available: true,
port: faker.datatype.number({ min: 30, max: 65535 }),
name: faker.random.alphaNumeric(32),
name: props?.name || faker.random.alphaNumeric(32),
description: faker.random.alphaNumeric(32),
tipi_version: 1,
short_desc: faker.random.alphaNumeric(32),
author: faker.random.alphaNumeric(32),
source: faker.internet.url(),
categories: [APP_CATEGORIES.AUTOMATION],
categories: props?.categories || [APP_CATEGORIES.AUTOMATION],
});
const createApp = async (props: IProps, db?: PrismaClient) => {