mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-11-28 09:24:24 +01:00
(test) frontend lib testing
This commit is contained in:
parent
892ffbe138
commit
8312842acc
@ -4,7 +4,7 @@ import { serialize } from 'object-to-formdata';
|
||||
|
||||
export default class ApiLib {
|
||||
static filtersToQuery (filters?: ApiFilter, keepNullValues = true): string {
|
||||
if (!filters) return '';
|
||||
if (!filters || Object.keys(filters).length < 1) return '';
|
||||
|
||||
return '?' + Object.entries(filters)
|
||||
.filter(filter => keepNullValues || !_.isNil(filter[1]))
|
||||
|
@ -7,8 +7,8 @@ export default class ParsingLib {
|
||||
* Try to parse the given value to get the value with the matching type.
|
||||
* It supports parsing arrays.
|
||||
*/
|
||||
static parse = (value: string|string[]): NestedBaseArray => {
|
||||
let parsedValue: NestedBaseArray = value;
|
||||
static parse = (value: ValueOrArray<string>): NestedBaseArray => {
|
||||
let parsedValue: NestedBaseArray;
|
||||
if (Array.isArray(value)) {
|
||||
parsedValue = [];
|
||||
for (const item of value) {
|
||||
|
49
test/frontend/lib/api.test.ts
Normal file
49
test/frontend/lib/api.test.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import ApiLib from 'lib/api';
|
||||
|
||||
describe('ApiLib', () => {
|
||||
test('empty filters to query', () => {
|
||||
const res = ApiLib.filtersToQuery({});
|
||||
expect(res).toBe('');
|
||||
});
|
||||
test('no filters to query', () => {
|
||||
const res = ApiLib.filtersToQuery(null);
|
||||
expect(res).toBe('');
|
||||
});
|
||||
test('one filter to query', () => {
|
||||
const res = ApiLib.filtersToQuery({ foo: 1 });
|
||||
expect(res).toBe('?foo=1');
|
||||
});
|
||||
test('many filters to query', () => {
|
||||
const res = ApiLib.filtersToQuery({ foo: 1, bar: 'toto', baz: false });
|
||||
expect(res).toBe('?foo=1&bar=toto&baz=false');
|
||||
});
|
||||
test('drop null values from query', () => {
|
||||
const res = ApiLib.filtersToQuery({ foo: null, bar: 'toto', baz: false }, false);
|
||||
expect(res).toBe('?bar=toto&baz=false');
|
||||
});
|
||||
test('keep null values into query', () => {
|
||||
const res = ApiLib.filtersToQuery({ foo: null, bar: 'toto', baz: false }, true);
|
||||
expect(res).toBe('?foo=null&bar=toto&baz=false');
|
||||
});
|
||||
test('serialize a single attachment', () => {
|
||||
const res = ApiLib.serializeAttachments({ foo: 1, item_file_attributes: { attachment_files: ['bar'] } }, 'item', ['item_file_attributes']);
|
||||
expect(res.get('item[foo]')).toBe('1');
|
||||
expect(res.get('item[item_file_attributes][attachment]')).toBe('bar');
|
||||
});
|
||||
test('serialize multiple attachments', () => {
|
||||
const res = ApiLib.serializeAttachments({ foo: 1, item_file_attributes: [{ attachment_files: ['bar'] }, { attachment_files: ['poo'] }] }, 'item', ['item_file_attributes']);
|
||||
expect(res.get('item[foo]')).toBe('1');
|
||||
expect(res.get('item[item_file_attributes][0][attachment]')).toBe('bar');
|
||||
expect(res.get('item[item_file_attributes][1][attachment]')).toBe('poo');
|
||||
});
|
||||
test('serialize some existing attachments', () => {
|
||||
const res = ApiLib.serializeAttachments({ foo: 1, item_file_attributes: [{ id: 4, _destroy: true, is_main: false }, { id: 7, _destroy: false, is_main: true }] }, 'item', ['item_file_attributes']);
|
||||
expect(res.get('item[foo]')).toBe('1');
|
||||
expect(res.get('item[item_file_attributes][0][id]')).toBe('4');
|
||||
expect(res.get('item[item_file_attributes][0][_destroy]')).toBe('true');
|
||||
expect(res.get('item[item_file_attributes][0][is_main]')).toBeNull();
|
||||
expect(res.get('item[item_file_attributes][1][id]')).toBe('7');
|
||||
expect(res.get('item[item_file_attributes][1][_destroy]')).toBeNull();
|
||||
expect(res.get('item[item_file_attributes][1][is_main]')).toBe('true');
|
||||
});
|
||||
});
|
14
test/frontend/lib/deferred.test.ts
Normal file
14
test/frontend/lib/deferred.test.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import Deferred from 'lib/deferred';
|
||||
|
||||
describe('Deferred', () => {
|
||||
test('resolve a deferred promise', () => {
|
||||
const deferred = new Deferred();
|
||||
deferred.resolve(4);
|
||||
expect(deferred.promise).resolves.toBe(4);
|
||||
});
|
||||
test('reject a deferred promise', () => {
|
||||
const deferred = new Deferred();
|
||||
deferred.reject('error');
|
||||
expect(deferred.promise).rejects.toBe('error');
|
||||
});
|
||||
});
|
28
test/frontend/lib/parsing.test.ts
Normal file
28
test/frontend/lib/parsing.test.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import ParsingLib from 'lib/parsing';
|
||||
|
||||
describe('ParsingLib', () => {
|
||||
test('parse a boolean', () => {
|
||||
const res = ParsingLib.simpleParse('true');
|
||||
expect(res).toBe(true);
|
||||
});
|
||||
test('parse a number', () => {
|
||||
const res = ParsingLib.simpleParse('10');
|
||||
expect(res).toBe(10);
|
||||
});
|
||||
test('parse an array of numbers', () => {
|
||||
const res = ParsingLib.parse(['10', '20', '30']);
|
||||
expect(res).toEqual([10, 20, 30]);
|
||||
});
|
||||
test('parse an array of booleans', () => {
|
||||
const res = ParsingLib.parse(['true', 'false']);
|
||||
expect(res).toEqual([true, false]);
|
||||
});
|
||||
test('parse a mixed array', () => {
|
||||
const res = ParsingLib.parse(['true', '10', 'foo']);
|
||||
expect(res).toEqual([true, 10, 'foo']);
|
||||
});
|
||||
test('parse an array of arrays', () => {
|
||||
const res = ParsingLib.parse([['bar', '10'], ['true', 'foo'], 'baz']);
|
||||
expect(res).toEqual([['bar', 10], [true, 'foo'], 'baz']);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user