insomnia/packages/insomnia-app/app/common/__tests__/misc.test.js

196 lines
5.2 KiB
JavaScript
Raw Normal View History

import * as misc from '../misc';
2018-06-25 17:42:50 +00:00
import { globalBeforeEach } from '../../__jest__/before-each';
describe('hasAuthHeader()', () => {
beforeEach(globalBeforeEach);
it('finds valid header', () => {
const yes = misc.hasAuthHeader([
2018-06-25 17:42:50 +00:00
{ name: 'foo', value: 'bar' },
{ name: 'authorization', value: 'foo' },
]);
expect(yes).toEqual(true);
});
it('finds valid header case insensitive', () => {
const yes = misc.hasAuthHeader([
2018-06-25 17:42:50 +00:00
{ name: 'foo', value: 'bar' },
{ name: 'AuthOrizAtiOn', value: 'foo' },
]);
expect(yes).toEqual(true);
});
});
describe('generateId()', () => {
beforeEach(globalBeforeEach);
it('generates a valid ID', () => {
const id = misc.generateId('foo');
expect(id).toMatch(/^foo_[a-z0-9]{32}$/);
});
it('generates without prefix', () => {
const id = misc.generateId();
expect(id).toMatch(/^[a-z0-9]{32}$/);
});
});
describe('filterHeaders()', () => {
beforeEach(globalBeforeEach);
it('handles bad headers', () => {
expect(misc.filterHeaders(null, null)).toEqual([]);
expect(misc.filterHeaders([], null)).toEqual([]);
expect(misc.filterHeaders(['bad'], null)).toEqual([]);
expect(misc.filterHeaders(['bad'], 'good')).toEqual([]);
expect(misc.filterHeaders(null, 'good')).toEqual([]);
2018-10-17 16:42:33 +00:00
expect(misc.filterHeaders([{ name: 'good', value: 'valid' }], null)).toEqual([]);
expect(misc.filterHeaders([{ name: 'good', value: 'valid' }], 'good')).toEqual([
{ name: 'good', value: 'valid' },
2018-10-17 16:42:33 +00:00
]);
});
});
describe('keyedDebounce()', () => {
beforeEach(async () => {
await globalBeforeEach();
jest.useFakeTimers();
});
it('debounces correctly', () => {
const resultsList = [];
const fn = misc.keyedDebounce(results => {
resultsList.push(results);
}, 100);
fn('foo', 'bar');
fn('baz', 'bar');
fn('foo', 'bar2');
fn('foo', 'bar3');
fn('multi', 'foo', 'bar', 'baz');
expect(setTimeout.mock.calls.length).toBe(5);
expect(resultsList).toEqual([]);
jest.runAllTimers();
2018-06-25 17:42:50 +00:00
expect(resultsList).toEqual([
{
foo: ['bar3'],
baz: ['bar'],
multi: ['foo', 'bar', 'baz'],
},
2018-06-25 17:42:50 +00:00
]);
});
});
describe('debounce()', () => {
beforeEach(async () => {
await globalBeforeEach();
jest.useFakeTimers();
});
it('debounces correctly', () => {
const resultList = [];
const fn = misc.debounce((...args) => {
resultList.push(args);
}, 100);
fn('foo');
fn('foo');
fn('multi', 'foo', 'bar', 'baz');
fn('baz', 'bar');
fn('foo', 'bar3');
expect(setTimeout.mock.calls.length).toBe(5);
expect(resultList).toEqual([]);
jest.runAllTimers();
expect(resultList).toEqual([['foo', 'bar3']]);
});
});
describe('fuzzyMatch()', () => {
beforeEach(globalBeforeEach);
it('can get a positive fuzzy match on a single field', () => {
expect(misc.fuzzyMatch('test', 'testing')).toEqual({
score: -3,
indexes: [0, 1, 2, 3],
target: 'testing',
});
expect(misc.fuzzyMatch('tst', 'testing')).toEqual({
score: -2004,
indexes: [0, 2, 3],
target: 'testing',
});
});
it('can get a negative fuzzy match on a single field', () => {
expect(misc.fuzzyMatch('foo', undefined)).toBeNull();
expect(misc.fuzzyMatch('foo', 'bar')).toBeNull();
});
});
describe('fuzzyMatchAll()', () => {
beforeEach(globalBeforeEach);
it('can get a positive fuzzy match on multiple fields', () => {
expect(misc.fuzzyMatchAll('', [undefined])).toEqual(null);
expect(misc.fuzzyMatchAll('', ['testing'])).toEqual(null);
expect(misc.fuzzyMatchAll(' ', ['testing'])).toEqual(null);
expect(misc.fuzzyMatchAll('test', ['testing', 'foo'])).toEqual({
score: -3,
indexes: [0, 1, 2, 3],
target: 'testing foo',
});
2018-10-17 16:42:33 +00:00
expect(misc.fuzzyMatchAll('test foo', ['testing', 'foo'], { splitSpace: true })).toEqual({
score: 0,
indexes: [0, 1, 2, 3, 0, 1, 2],
target: 'testing foo',
});
expect(misc.fuzzyMatchAll('tst', ['testing'])).toEqual({
score: -2004,
indexes: [0, 2, 3],
target: 'testing',
});
2018-06-25 17:42:50 +00:00
expect(
misc.fuzzyMatch('tst this ou', 'testing this out', {
splitSpace: true,
loose: true,
}),
).toEqual({
score: -20,
indexes: [0, 2, 3, 8, 9, 10, 11, 13, 14],
target: 'testing this out',
});
});
it('can get a negative fuzzy match on multiple fields', () => {
expect(misc.fuzzyMatchAll('foo', [undefined])).toEqual(null);
expect(misc.fuzzyMatchAll('foo', ['bar'])).toEqual(null);
2018-10-17 16:42:33 +00:00
expect(misc.fuzzyMatchAll('wrong this ou', ['testing', 'this', 'out'])).toEqual(null);
});
});
Version Control (beta) (#1439) * VCS proof of concept underway! * Stuff * Some things * Replace deprecated Electron makeSingleInstance * Rename `window` variables so not to be confused with window object * Don't unnecessarily update request when URL does not change * Regenerate package-lock * Fix tests + ESLint * Publish - insomnia-app@1.0.49 - insomnia-cookies@0.0.12 - insomnia-httpsnippet@1.16.18 - insomnia-importers@2.0.13 - insomnia-libcurl@0.0.23 - insomnia-prettify@0.1.7 - insomnia-url@0.1.6 - insomnia-xpath@1.0.9 - insomnia-plugin-base64@1.0.6 - insomnia-plugin-cookie-jar@1.0.8 - insomnia-plugin-core-themes@1.0.5 - insomnia-plugin-default-headers@1.1.9 - insomnia-plugin-file@1.0.7 - insomnia-plugin-hash@1.0.7 - insomnia-plugin-jsonpath@1.0.12 - insomnia-plugin-now@1.0.11 - insomnia-plugin-os@1.0.13 - insomnia-plugin-prompt@1.1.9 - insomnia-plugin-request@1.0.18 - insomnia-plugin-response@1.0.16 - insomnia-plugin-uuid@1.0.10 * Broken but w/e * Some tweaks * Big refactor. Create local snapshots and push done * POC merging and a lot of improvements * Lots of work done on initial UI/UX * Fix old tests * Atomic writes and size-based batches * Update StageEntry definition once again to be better * Factor out GraphQL query logic * Merge algorithm, history modal, other minor things * Fix test * Merge, checkout, revert w/ user changes now work * Force UI to refresh when switching branches changes active request * Rough draft pull() and some cleanup * E2EE stuff and some refactoring * Add ability to share project with team and fixed tests * VCS now created in root component and better remote project handling * Remove unused definition * Publish - insomnia-account@0.0.2 - insomnia-app@1.1.1 - insomnia-cookies@0.0.14 - insomnia-httpsnippet@1.16.20 - insomnia-importers@2.0.15 - insomnia-libcurl@0.0.25 - insomnia-prettify@0.1.9 - insomnia-sync@0.0.2 - insomnia-url@0.1.8 - insomnia-xpath@1.0.11 - insomnia-plugin-base64@1.0.8 - insomnia-plugin-cookie-jar@1.0.10 - insomnia-plugin-core-themes@1.0.7 - insomnia-plugin-file@1.0.9 - insomnia-plugin-hash@1.0.9 - insomnia-plugin-jsonpath@1.0.14 - insomnia-plugin-now@1.0.13 - insomnia-plugin-os@1.0.15 - insomnia-plugin-prompt@1.1.11 - insomnia-plugin-request@1.0.20 - insomnia-plugin-response@1.0.18 - insomnia-plugin-uuid@1.0.12 * Move some deps around * Fix Flow errors * Update package.json * Fix eslint errors * Fix tests * Update deps * bootstrap insomnia-sync * TRy fixing appveyor * Try something else * Bump lerna * try powershell * Try again * Fix imports * Fixed errors * sync types refactor * Show remote projects in workspace dropdown * Improved pulling of non-local workspaces * Loading indicators and some tweaks * Clean up sync staging modal * Some sync improvements: - No longer store stage - Upgrade Electron - Sync UI/UX improvements * Fix snyc tests * Upgraded deps and hot loader tweaks (it's broken for some reason) * Fix tests * Branches dialog, network refactoring, some tweaks * Fixed merging when other branch is empty * A bunch of small fixes from real testing * Fixed pull merge logic * Fix tests * Some bug fixes * A few small tweaks * Conflict resolution and other improvements * Fix tests * Add revert changes * Deal with duplicate projects per workspace * Some tweaks and accessibility improvements * Tooltip accessibility * Fix API endpoint * Fix tests * Remove jest dep from insomnia-importers
2019-04-18 00:50:03 +00:00
describe('chunkArray()', () => {
it('works with exact divisor', () => {
const chunks = misc.chunkArray([1, 2, 3, 4, 5, 6], 3);
expect(chunks).toEqual([[1, 2, 3], [4, 5, 6]]);
});
it('works with weird divisor', () => {
const chunks = misc.chunkArray([1, 2, 3, 4, 5, 6], 4);
expect(chunks).toEqual([[1, 2, 3, 4], [5, 6]]);
});
it('works with empty', () => {
const chunks = misc.chunkArray([], 4);
expect(chunks).toEqual([]);
});
it('works with less than one chunk', () => {
const chunks = misc.chunkArray([1, 2], 4);
expect(chunks).toEqual([[1, 2]]);
});
});