From 8312842acc714e6e90a9ba110f07ffedf71be197 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 14 Feb 2023 11:04:40 +0100 Subject: [PATCH] (test) frontend lib testing --- app/frontend/src/javascript/lib/api.ts | 2 +- app/frontend/src/javascript/lib/parsing.ts | 4 +- test/frontend/lib/api.test.ts | 49 ++++++++++++++++++++++ test/frontend/lib/deferred.test.ts | 14 +++++++ test/frontend/lib/parsing.test.ts | 28 +++++++++++++ 5 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 test/frontend/lib/api.test.ts create mode 100644 test/frontend/lib/deferred.test.ts create mode 100644 test/frontend/lib/parsing.test.ts diff --git a/app/frontend/src/javascript/lib/api.ts b/app/frontend/src/javascript/lib/api.ts index b90cb27ce..a0bae755a 100644 --- a/app/frontend/src/javascript/lib/api.ts +++ b/app/frontend/src/javascript/lib/api.ts @@ -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])) diff --git a/app/frontend/src/javascript/lib/parsing.ts b/app/frontend/src/javascript/lib/parsing.ts index 7f1c43c10..5ac94b17a 100644 --- a/app/frontend/src/javascript/lib/parsing.ts +++ b/app/frontend/src/javascript/lib/parsing.ts @@ -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): NestedBaseArray => { + let parsedValue: NestedBaseArray; if (Array.isArray(value)) { parsedValue = []; for (const item of value) { diff --git a/test/frontend/lib/api.test.ts b/test/frontend/lib/api.test.ts new file mode 100644 index 000000000..dbea14337 --- /dev/null +++ b/test/frontend/lib/api.test.ts @@ -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'); + }); +}); diff --git a/test/frontend/lib/deferred.test.ts b/test/frontend/lib/deferred.test.ts new file mode 100644 index 000000000..beeca9522 --- /dev/null +++ b/test/frontend/lib/deferred.test.ts @@ -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'); + }); +}); diff --git a/test/frontend/lib/parsing.test.ts b/test/frontend/lib/parsing.test.ts new file mode 100644 index 000000000..02832b2a6 --- /dev/null +++ b/test/frontend/lib/parsing.test.ts @@ -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']); + }); +});