Portal/rsconcept/frontend/src/models/libraryAPI.test.ts

78 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
import { AccessPolicy, ILibraryItem, LibraryItemType, LocationHead } from './library';
import { matchLibraryItem, validateLocation } from './libraryAPI';
describe('Testing matching LibraryItem', () => {
const item1: ILibraryItem = {
id: 42,
item_type: LibraryItemType.RSFORM,
title: 'Item1',
alias: 'I1',
comment: 'comment',
time_create: 'I2',
time_update: '',
owner: null,
access_policy: AccessPolicy.PUBLIC,
location: LocationHead.COMMON,
read_only: false,
visible: true
};
const itemEmpty: ILibraryItem = {
id: -1,
item_type: LibraryItemType.RSFORM,
title: '',
alias: '',
comment: '',
time_create: '',
time_update: '',
owner: null,
access_policy: AccessPolicy.PUBLIC,
location: LocationHead.COMMON,
read_only: false,
visible: true
};
test('empty input', () => {
expect(matchLibraryItem(itemEmpty, '')).toEqual(true);
expect(matchLibraryItem(itemEmpty, '12')).toEqual(false);
expect(matchLibraryItem(itemEmpty, ' ')).toEqual(false);
});
test('regular input', () => {
expect(matchLibraryItem(item1, item1.title)).toEqual(true);
expect(matchLibraryItem(item1, item1.alias)).toEqual(true);
expect(matchLibraryItem(item1, item1.title + '@invalid')).toEqual(false);
expect(matchLibraryItem(item1, item1.alias + '@invalid')).toEqual(false);
expect(matchLibraryItem(item1, item1.time_create)).toEqual(false);
2024-09-12 15:07:45 +03:00
expect(matchLibraryItem(item1, item1.comment)).toEqual(true);
2024-06-07 20:17:03 +03:00
});
});
const validateLocationData = [
['', 'false'],
['U/U', 'false'],
['/A', 'false'],
['/U/user@mail', 'false'],
['U/u\\asdf', 'false'],
['/U/ asdf', 'false'],
['/User', 'false'],
['//', 'false'],
['/S/1 ', 'false'],
['/S/1/2 /3', 'false'],
['/P', 'true'],
['/L', 'true'],
['/U', 'true'],
['/S', 'true'],
['/S/Вася пупки', 'true'],
['/S/123', 'true'],
['/S/1234', 'true'],
['/S/1/!asdf/тест тест', 'true']
];
describe('Testing location validation', () => {
it.each(validateLocationData)('isValid %p', (input: string, expected: string) => {
const result = validateLocation(input);
expect(String(result)).toBe(expected);
});
});