addons/shared/__tests__/utils.test.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {basename, generatorContains, group, mapObject, partition, truncate} from '../utils';
b69ab319
b69ab3110describe('basename', () => {
b69ab3111 it('/path/to/foo.txt -> foo.txt', () => {
b69ab3112 expect(basename('/path/to/foo.txt')).toEqual('foo.txt');
b69ab3113 });
b69ab3114
b69ab3115 it("/path/to/ -> ''", () => {
b69ab3116 expect(basename('/path/to/')).toEqual('');
b69ab3117 });
b69ab3118
b69ab3119 it('customizable delimiters', () => {
b69ab3120 expect(basename('/path/to/foo.txt', '.')).toEqual('txt');
b69ab3121 });
b69ab3122
b69ab3123 it('empty string', () => {
b69ab3124 expect(basename('')).toEqual('');
b69ab3125 });
b69ab3126
b69ab3127 it('delimiter not in string', () => {
b69ab3128 expect(basename('hello world')).toEqual('hello world');
b69ab3129 });
b69ab3130});
b69ab3131
b69ab3132describe('mapObject', () => {
b69ab3133 it('maps object types', () => {
b69ab3134 expect(mapObject({foo: 123, bar: 456}, ([key, value]) => [key, {value}])).toEqual({
b69ab3135 foo: {value: 123},
b69ab3136 bar: {value: 456},
b69ab3137 });
b69ab3138 });
b69ab3139
b69ab3140 it('handles different key types', () => {
b69ab3141 expect(mapObject({foo: 123, bar: 456}, ([key, value]) => [value, key])).toEqual({
b69ab3142 123: 'foo',
b69ab3143 456: 'bar',
b69ab3144 });
b69ab3145 });
b69ab3146});
b69ab3147
b69ab3148describe('generatorContains', () => {
b69ab3149 let lastYield = 0;
b69ab3150 function* gen() {
b69ab3151 lastYield = 3;
b69ab3152 yield 3;
b69ab3153 lastYield = 5;
b69ab3154 yield 5;
b69ab3155 }
b69ab3156
b69ab3157 it('supports testing a value', () => {
b69ab3158 expect(generatorContains(gen(), 3)).toBeTruthy();
b69ab3159 expect(lastYield).toBe(3);
b69ab3160 expect(generatorContains(gen(), 5)).toBeTruthy();
b69ab3161 expect(lastYield).toBe(5);
b69ab3162 expect(generatorContains(gen(), 2)).toBeFalsy();
b69ab3163 expect(lastYield).toBe(5);
b69ab3164 });
b69ab3165
b69ab3166 it('supports testing via a function', () => {
b69ab3167 expect(generatorContains(gen(), v => v > 2)).toBeTruthy();
b69ab3168 expect(lastYield).toBe(3);
b69ab3169 expect(generatorContains(gen(), v => v > 4)).toBeTruthy();
b69ab3170 expect(lastYield).toBe(5);
b69ab3171 expect(generatorContains(gen(), v => v > 6)).toBeFalsy();
b69ab3172 expect(lastYield).toBe(5);
b69ab3173 });
b69ab3174});
b69ab3175
b69ab3176describe('truncate', () => {
b69ab3177 it('does not truncate strings within the maxLength constraint', () => {
b69ab3178 expect(truncate('abc', 3)).toBe('abc');
b69ab3179 expect(truncate('def', 4)).toBe('def');
b69ab3180 });
b69ab3181
b69ab3182 it('truncates long strings', () => {
b69ab3183 expect(truncate('abc', 2)).toBe('a…');
b69ab3184 expect(truncate('def', 0)).toBe('…');
b69ab3185 });
b69ab3186});
b69ab3187
b69ab3188describe('partition', () => {
b69ab3189 it('partitions an array into two arrays based on a predicate', () => {
b69ab3190 expect(partition([1, 2, 3], n => n % 2 === 0)).toEqual([[2], [1, 3]]);
b69ab3191 expect(partition([1, 2, 3], () => true)).toEqual([[1, 2, 3], []]);
b69ab3192 expect(partition([1, 2, 3], () => false)).toEqual([[], [1, 2, 3]]);
b69ab3193 });
b69ab3194});
b69ab3195
b69ab3196describe('group', () => {
b69ab3197 it('groups an array into multiple subarrays based on a key', () => {
b69ab3198 expect(group(['a', 'ab', 'abc', 'ef', 'g'], e => e.length)).toEqual({
b69ab3199 1: ['a', 'g'],
b69ab31100 2: ['ab', 'ef'],
b69ab31101 3: ['abc'],
b69ab31102 });
b69ab31103 });
b69ab31104});