1 | const assert = require('assert');
|
2 | const str = require('../lib/util/str');
|
3 | const namedArgs = require('../lib/util/namedArgs');
|
4 | const obj = require('../lib/util/obj');
|
5 |
|
6 | describe('util:No in use', () => {
|
7 | describe('str:trim', () => {
|
8 | it('should trim dot', async () => {
|
9 | const out = str.trim('.cart.', '.');
|
10 | assert.equal(out, 'cart');
|
11 | });
|
12 | });
|
13 | });
|
14 |
|
15 | describe('util:in use', () => {
|
16 | describe('namedArgs', () => {
|
17 | it('should return empty object', async () => {
|
18 | const out = namedArgs(null);
|
19 | assert.equal(JSON.stringify(out), JSON.stringify({}));
|
20 | });
|
21 | });
|
22 | describe('obj:strNotations', () => {
|
23 | it('should return keys array', async () => {
|
24 | const out = obj.strNotations({
|
25 | cart: [
|
26 | {
|
27 | products: [
|
28 | {
|
29 | 'p.ids': [1, 2, 3],
|
30 | },
|
31 | ],
|
32 | },
|
33 | ],
|
34 | }, { repetition: 10, values: false });
|
35 |
|
36 | if (!Array.isArray(out)) {
|
37 | throw new Error('Array was expected.');
|
38 | }
|
39 | });
|
40 |
|
41 | it('should use custom escaper', async () => {
|
42 | const out = obj.strNotations(
|
43 | {
|
44 | cart: [
|
45 | {
|
46 | 'products.ids': [1, 2, 3],
|
47 | },
|
48 | ],
|
49 | },
|
50 | {
|
51 | repetition: 10,
|
52 | values: false,
|
53 | escape: (key, options) => key.split(options.seperator).join(`\\.${options.seperator}`),
|
54 | },
|
55 | );
|
56 |
|
57 | if (!Array.isArray(out)) {
|
58 | throw new Error('Array was expected.');
|
59 | }
|
60 | });
|
61 |
|
62 | it('should throw max rep exception', async () => {
|
63 | try {
|
64 | const out = obj.strNotations({
|
65 | cart: [
|
66 | {
|
67 | products: [
|
68 | {
|
69 | 'p.ids': [1, 2, 3],
|
70 | },
|
71 | ],
|
72 | },
|
73 | ],
|
74 | }, { repetition: 2, values: false });
|
75 | if (!Array.isArray(out)) {
|
76 | throw new Error('Array was expected.');
|
77 | }
|
78 | } catch (e) {
|
79 | assert.equal(e, 'Error: Max(2) repetation was reached.');
|
80 | }
|
81 | });
|
82 | });
|
83 | });
|