|
| 1 | +import { all, json, anyOrder } from './helpers.js'; |
| 2 | + |
| 3 | +describe('all (Args to string)', () => { |
| 4 | + test('Without arguments', () => { |
| 5 | + expect(all()).toEqual(''); |
| 6 | + }); |
| 7 | + |
| 8 | + test('undefined', () => { |
| 9 | + expect(all(undefined)).toEqual('undefined'); |
| 10 | + }); |
| 11 | + |
| 12 | + test('null', () => { |
| 13 | + expect(all(null)).toEqual('null'); |
| 14 | + }); |
| 15 | + |
| 16 | + test('Object', () => { |
| 17 | + expect(all({ a: 1, b: true, c: null })).toEqual('[object Object]'); |
| 18 | + }); |
| 19 | + |
| 20 | + test('Empty object', () => { |
| 21 | + expect(all({})).toEqual('[object Object]'); |
| 22 | + }); |
| 23 | + |
| 24 | + test('Array', () => { |
| 25 | + expect(all(['str', 1, undefined, true, null])).toEqual('str,1,,true,'); |
| 26 | + }); |
| 27 | + |
| 28 | + test('Empty array', () => { |
| 29 | + expect(all([])).toEqual(''); |
| 30 | + }); |
| 31 | + |
| 32 | + test('RegExp', () => { |
| 33 | + expect(all(/^(.*)?/)).toEqual('/^(.*)?/'); |
| 34 | + }); |
| 35 | + |
| 36 | + test('Multiple arguments', () => { |
| 37 | + expect(all('str', 100, null, true, undefined, /^(.*)?/)).toEqual( |
| 38 | + 'str-100-null-true-undefined-/^(.*)?/' |
| 39 | + ); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('json (Args to JSON string)', () => { |
| 44 | + test('Without arguments', () => { |
| 45 | + expect(json()).toEqual('[]'); |
| 46 | + }); |
| 47 | + |
| 48 | + test('undefined', () => { |
| 49 | + expect(json(undefined)).toEqual('[null]'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('null', () => { |
| 53 | + expect(json(null)).toEqual('[null]'); |
| 54 | + }); |
| 55 | + |
| 56 | + test('Object', () => { |
| 57 | + expect(json({ a: 1, b: true, c: null })).toEqual('[{"a":1,"b":true,"c":null}]'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('Empty object', () => { |
| 61 | + expect(json({})).toEqual('[{}]'); |
| 62 | + }); |
| 63 | + |
| 64 | + test('Array', () => { |
| 65 | + expect(json(['str', 1, undefined, true, null])).toEqual('[["str",1,null,true,null]]'); |
| 66 | + }); |
| 67 | + |
| 68 | + test('Empty array', () => { |
| 69 | + expect(json([])).toEqual('[[]]'); |
| 70 | + }); |
| 71 | + |
| 72 | + test('RegExp', () => { |
| 73 | + expect(json(/^(.*)?/)).toEqual('[{}]'); |
| 74 | + }); |
| 75 | + |
| 76 | + test('Multiple arguments', () => { |
| 77 | + expect(json({ a: 1 }, true, undefined, [false, { b: 2 }], /^(.*)?/)).toEqual( |
| 78 | + '[{"a":1},true,null,[false,{"b":2}],{}]' |
| 79 | + ); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('anyOrder (Args in any order to string)', () => { |
| 84 | + test('Without arguments', () => { |
| 85 | + expect(anyOrder()).toEqual(''); |
| 86 | + }); |
| 87 | + |
| 88 | + test('Strings', () => { |
| 89 | + expect(anyOrder('a', 'b', 'c', '0', '1')).toEqual(anyOrder('1', 'b', 'a', 'c', '0')); |
| 90 | + }); |
| 91 | + |
| 92 | + test('Numbers', () => { |
| 93 | + expect(anyOrder(1, 2, 3, 4, 100, 1000)).toEqual(anyOrder(100, 2, 1000, 4, 1, 3)); |
| 94 | + }); |
| 95 | + |
| 96 | + test('Booleans', () => { |
| 97 | + expect(anyOrder(true, false, true, false)).toEqual(anyOrder(false, false, true, true)); |
| 98 | + }); |
| 99 | + |
| 100 | + test('null and undefined', () => { |
| 101 | + expect(anyOrder(null, undefined)).toEqual(anyOrder(undefined, null)); |
| 102 | + }); |
| 103 | + |
| 104 | + test('Arrays', () => { |
| 105 | + const arr1 = ['str', 1, undefined, true, null]; |
| 106 | + const arr2 = [1, 2, 3, true, null]; |
| 107 | + const arr3 = [null, 100]; |
| 108 | + |
| 109 | + expect(anyOrder(arr1, arr2, arr3)).toEqual(anyOrder(arr3, arr1, arr2)); |
| 110 | + }); |
| 111 | + |
| 112 | + test('RegExp objects', () => { |
| 113 | + const r1 = /^(.*)?/; |
| 114 | + const r2 = /([A-Z])\w+/; |
| 115 | + const r3 = /(\d+(\.\d+)?)/gi; |
| 116 | + |
| 117 | + expect(anyOrder(r1, r2, r3)).toEqual(anyOrder(r2, r3, r1)); |
| 118 | + }); |
| 119 | + |
| 120 | + test('Multiple arguments', () => { |
| 121 | + const string = 'str'; |
| 122 | + const number = 100; |
| 123 | + const regexp = /^(.*)?/; |
| 124 | + |
| 125 | + expect(anyOrder(string, number, null, true, undefined, regexp, false)).toEqual( |
| 126 | + anyOrder(false, undefined, true, regexp, null, string, number) |
| 127 | + ); |
| 128 | + }); |
| 129 | +}); |
0 commit comments