From 2dc12bd4629fc818df8cf831e0de52d4d1155b34 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Wed, 7 Sep 2022 20:00:33 +0300 Subject: [PATCH] Replace jest done function with promises in utils tests --- packages/utils/tests/validate-alias-test.ts | 377 ++++++++------- packages/utils/tests/validate-icon-test.ts | 504 +++++++++++--------- packages/utils/tests/validate-test.ts | 406 ++++++++-------- 3 files changed, 679 insertions(+), 608 deletions(-) diff --git a/packages/utils/tests/validate-alias-test.ts b/packages/utils/tests/validate-alias-test.ts index 8241ea4..ce68133 100644 --- a/packages/utils/tests/validate-alias-test.ts +++ b/packages/utils/tests/validate-alias-test.ts @@ -46,26 +46,10 @@ describe('Testing validating alias', () => { }); }); - test('Null', (done) => { - try { - validateIconSet({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - aliases: null, - }); - done('Expected to throw error when aliases is null'); - } catch (err) { - // - } - - // Fix it - expect( - validateIconSet( - { + test('Null', () => { + return new Promise((fulfill, reject) => { + try { + validateIconSet({ prefix: 'foo', icons: { bar: { @@ -73,48 +57,44 @@ describe('Testing validating alias', () => { }, }, aliases: null, - }, - { fix: true } - ) - ).toEqual({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - }); + }); + reject('Expected to throw error when aliases is null'); + return; + } catch { + // + } - done(); - }); - - test('Invalid parent', (done) => { - try { - const result = validateIconSet({ + // Fix it + expect( + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + aliases: null, + }, + { fix: true } + ) + ).toEqual({ prefix: 'foo', icons: { bar: { body: '', }, }, - aliases: { - baz: { - parent: 'missing', - }, - }, }); - done( - 'Expected to throw error when alias has missing parent, got ' + - JSON.stringify(result) - ); - } catch (err) { - // - } - // Fix it - expect( - validateIconSet( - { + fulfill(true); + }); + }); + + test('Invalid parent', () => { + return new Promise((fulfill, reject) => { + try { + const result = validateIconSet({ prefix: 'foo', icons: { bar: { @@ -126,51 +106,51 @@ describe('Testing validating alias', () => { parent: 'missing', }, }, - }, - { fix: true } - ) - ).toEqual({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - }); + }); + reject( + 'Expected to throw error when alias has missing parent, got ' + + JSON.stringify(result) + ); + return; + } catch { + // + } - done(); - }); - - test('Invalid parent, 2 levels', (done) => { - try { - const result = validateIconSet({ + // Fix it + expect( + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + aliases: { + baz: { + parent: 'missing', + }, + }, + }, + { fix: true } + ) + ).toEqual({ prefix: 'foo', icons: { bar: { body: '', }, }, - aliases: { - baz: { - parent: 'missing', - }, - baz2: { - parent: 'baz', - }, - }, }); - done( - 'Expected to throw error when alias has missing parent, got ' + - JSON.stringify(result) - ); - } catch (err) { - // - } - // Fix it - expect( - validateIconSet( - { + fulfill(true); + }); + }); + + test('Invalid parent, 2 levels', () => { + return new Promise((fulfill, reject) => { + try { + const result = validateIconSet({ prefix: 'foo', icons: { bar: { @@ -185,51 +165,54 @@ describe('Testing validating alias', () => { parent: 'baz', }, }, - }, - { fix: true } - ) - ).toEqual({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - }); + }); + reject( + 'Expected to throw error when alias has missing parent, got ' + + JSON.stringify(result) + ); + return; + } catch { + // + } - done(); - }); - - test('Invalid parent, 2 levels, reverse order', (done) => { - try { - const result = validateIconSet({ + // Fix it + expect( + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + aliases: { + baz: { + parent: 'missing', + }, + baz2: { + parent: 'baz', + }, + }, + }, + { fix: true } + ) + ).toEqual({ prefix: 'foo', icons: { bar: { body: '', }, }, - aliases: { - baz: { - parent: 'baz2', - }, - baz2: { - parent: 'missing', - }, - }, }); - done( - 'Expected to throw error when alias has missing parent, got ' + - JSON.stringify(result) - ); - } catch (err) { - // - } - // Fix it - expect( - validateIconSet( - { + fulfill(true); + }); + }); + + test('Invalid parent, 2 levels, reverse order', () => { + return new Promise((fulfill, reject) => { + try { + const result = validateIconSet({ prefix: 'foo', icons: { bar: { @@ -244,54 +227,54 @@ describe('Testing validating alias', () => { parent: 'missing', }, }, - }, - { fix: true } - ) - ).toEqual({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - }); + }); + reject( + 'Expected to throw error when alias has missing parent, got ' + + JSON.stringify(result) + ); + return; + } catch { + // + } - done(); - }); - - test('Parent loop', (done) => { - try { - const result = validateIconSet({ + // Fix it + expect( + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + aliases: { + baz: { + parent: 'baz2', + }, + baz2: { + parent: 'missing', + }, + }, + }, + { fix: true } + ) + ).toEqual({ prefix: 'foo', icons: { bar: { body: '', }, }, - aliases: { - baz: { - parent: 'baz2', - }, - baz2: { - parent: 'baz', - }, - baz3: { - parent: 'bar', - }, - }, }); - done( - 'Expected to throw error when alias has missing parent, got ' + - JSON.stringify(result) - ); - } catch (err) { - // - } - // Fix it - expect( - validateIconSet( - { + fulfill(true); + }); + }); + + test('Parent loop', () => { + return new Promise((fulfill, reject) => { + try { + const result = validateIconSet({ prefix: 'foo', icons: { bar: { @@ -309,23 +292,55 @@ describe('Testing validating alias', () => { parent: 'bar', }, }, - }, - { fix: true } - ) - ).toEqual({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - aliases: { - baz3: { - parent: 'bar', - }, - }, - }); + }); + reject( + 'Expected to throw error when alias has missing parent, got ' + + JSON.stringify(result) + ); + return; + } catch { + // + } - done(); + // Fix it + expect( + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + aliases: { + baz: { + parent: 'baz2', + }, + baz2: { + parent: 'baz', + }, + baz3: { + parent: 'bar', + }, + }, + }, + { fix: true } + ) + ).toEqual({ + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + aliases: { + baz3: { + parent: 'bar', + }, + }, + }); + + fulfill(true); + }); }); }); diff --git a/packages/utils/tests/validate-icon-test.ts b/packages/utils/tests/validate-icon-test.ts index 777533c..241deeb 100644 --- a/packages/utils/tests/validate-icon-test.ts +++ b/packages/utils/tests/validate-icon-test.ts @@ -54,45 +54,25 @@ describe('Testing validating icon', () => { }); // Required string - test('body', (done) => { - // Missing body - try { - validateIconSet({ - prefix: 'foo', - icons: { - bar: { - width: 16, - }, - }, - }); - done('Expected to throw error when body is missing'); - } catch (err) { - // - } - - try { - validateIconSet( - { + test('body', () => { + return new Promise((fulfill, reject) => { + // Missing body + try { + validateIconSet({ prefix: 'foo', icons: { bar: { width: 16, }, }, - }, - { - fix: true, - } - ); - done( - 'Expected to throw error when body is missing and cannot be fixed' - ); - } catch (err) { - // - } + }); + reject('Expected to throw error when body is missing'); + return; + } catch { + // + } - try { - expect( + try { validateIconSet( { prefix: 'foo', @@ -100,140 +80,86 @@ describe('Testing validating icon', () => { bar: { width: 16, }, - baz: { - body: '', - }, }, }, { fix: true, } - ) - ).toEqual({ - prefix: 'foo', - icons: { - baz: { - body: '', - }, - }, - }); - } catch (err) { - done( - 'Expected to not throw error when body is missing, but icon set can be fixed' - ); - // - } + ); + reject( + 'Expected to throw error when body is missing and cannot be fixed' + ); + return; + } catch { + // + } - validationValues.forEach((item, value) => { - // Validate without fixing try { - validateIconSet({ + expect( + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + width: 16, + }, + baz: { + body: '', + }, + }, + }, + { + fix: true, + } + ) + ).toEqual({ prefix: 'foo', icons: { - bar: { - body: value, + baz: { + body: '', }, }, }); - - if (item.type !== 'string') { - done(`Expected to throw error when body is ${item.text}`); - } - } catch (err) { - if (item.type === 'string') { - done(`Expected to pass when body is ${item.text}`); - } - } - - // Attempt to fix (will fail because icon set is empty after failing icon is removed) - try { - validateIconSet( - { - prefix: 'foo', - icons: { - bar: { - body: value, - }, - }, - }, - { - fix: true, - } + } catch { + reject( + 'Expected to not throw error when body is missing, but icon set can be fixed' ); - - if (item.type !== 'string') { - done(`Expected to throw error when body is ${item.text}`); - } - } catch (err) { - if (item.type === 'string') { - done(`Expected to pass when body is ${item.text}`); - } + return; } - // Attempt to fix (will not fail because another icon is valid) - try { - validateIconSet( - { - prefix: 'foo', - icons: { - bar: { - body: value, - }, - baz: { - body: '', - }, - }, - }, - { - fix: true, - } - ); - } catch (err) { - done('Expected to pass when another icon is valid'); - } - }); - - done(); - }); - - // Numbers - ['width', 'height', 'left', 'top', 'rotate'].forEach((prop) => { - test(prop, (done) => { - // Validate without fixing validationValues.forEach((item, value) => { + // Validate without fixing try { validateIconSet({ prefix: 'foo', icons: { bar: { - body: '', - [prop]: value, + body: value, }, }, }); - if (item.type !== 'number') { - done( - `Expected to throw error when ${prop} is ${item.text}` + if (item.type !== 'string') { + reject( + `Expected to throw error when body is ${item.text}` ); + return; } - } catch (err) { - if (item.type === 'number') { - done(`Expected to pass when ${prop} is ${item.text}`); + } catch { + if (item.type === 'string') { + reject(`Expected to pass when body is ${item.text}`); + return; } } - }); - // Fix - validationValues.forEach((item, value) => { + // Attempt to fix (will fail because icon set is empty after failing icon is removed) try { - const result = validateIconSet( + validateIconSet( { prefix: 'foo', icons: { bar: { - body: '', - [prop]: value, + body: value, }, }, }, @@ -242,37 +168,212 @@ describe('Testing validating icon', () => { } ); - const icon = - item.type === 'number' - ? { - body: '', - [prop]: value, - } - : { - // [prop] should be deleted - body: '', - }; + if (item.type !== 'string') { + reject( + `Expected to throw error when body is ${item.text}` + ); + return; + } + } catch { + if (item.type === 'string') { + reject(`Expected to pass when body is ${item.text}`); + return; + } + } - expect(result).toEqual({ - prefix: 'foo', - icons: { - bar: icon, + // Attempt to fix (will not fail because another icon is valid) + try { + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: value, + }, + baz: { + body: '', + }, + }, }, - }); - } catch (err) { - done( - `Expected to not throw error when ${prop} is being fixed` + { + fix: true, + } ); + } catch { + reject('Expected to pass when another icon is valid'); + return; } }); - done(); + fulfill(true); + }); + }); + + // Numbers + ['width', 'height', 'left', 'top', 'rotate'].forEach((prop) => { + test(prop, () => { + return new Promise((fulfill, reject) => { + // Validate without fixing + validationValues.forEach((item, value) => { + try { + validateIconSet({ + prefix: 'foo', + icons: { + bar: { + body: '', + [prop]: value, + }, + }, + }); + + if (item.type !== 'number') { + reject( + `Expected to throw error when ${prop} is ${item.text}` + ); + return; + } + } catch { + if (item.type === 'number') { + reject( + `Expected to pass when ${prop} is ${item.text}` + ); + return; + } + } + }); + + // Fix + validationValues.forEach((item, value) => { + try { + const result = validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + [prop]: value, + }, + }, + }, + { + fix: true, + } + ); + + const icon = + item.type === 'number' + ? { + body: '', + [prop]: value, + } + : { + // [prop] should be deleted + body: '', + }; + + expect(result).toEqual({ + prefix: 'foo', + icons: { + bar: icon, + }, + }); + } catch { + reject( + `Expected to not throw error when ${prop} is being fixed` + ); + return; + } + }); + + fulfill(true); + }); }); }); // Boolean ['hFlip', 'vFlip', 'hidden'].forEach((prop) => { - test(prop, (done) => { + test(prop, () => { + return new Promise((fulfill, reject) => { + validationValues.forEach((item, value) => { + // Validate + try { + validateIconSet({ + prefix: 'foo', + icons: { + bar: { + body: '', + [prop]: value, + }, + }, + }); + + if (item.type !== 'boolean') { + reject( + `Expected to throw error when ${prop} is ${item.text}` + ); + return; + } + } catch { + if (item.type === 'boolean') { + reject( + `Expected to pass when ${prop} is ${item.text}` + ); + return; + } + } + }); + + // Fix + validationValues.forEach((item, value) => { + try { + const result = validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + [prop]: value, + }, + }, + }, + { + fix: true, + } + ); + + const icon = + item.type === 'boolean' + ? { + body: '', + [prop]: value, + } + : { + // [prop] should be deleted + body: '', + }; + + expect(result).toEqual({ + prefix: 'foo', + icons: { + bar: icon, + }, + }); + } catch { + reject( + `Expected to not throw error when ${prop} is being fixed` + ); + return; + } + }); + + fulfill(true); + }); + }); + }); + + // Unexpected field + test('foo', () => { + return new Promise((fulfill, reject) => { validationValues.forEach((item, value) => { // Validate try { @@ -281,19 +382,21 @@ describe('Testing validating icon', () => { icons: { bar: { body: '', - [prop]: value, + foo: value, }, }, }); - if (item.type !== 'boolean') { - done( - `Expected to throw error when ${prop} is ${item.text}` + if (item.type === 'object') { + reject( + `Expected to throw error when value is ${item.text}` ); + return; } - } catch (err) { - if (item.type === 'boolean') { - done(`Expected to pass when ${prop} is ${item.text}`); + } catch { + if (item.type !== 'object') { + reject(`Expected to pass when value is ${item.text}`); + return; } } }); @@ -307,7 +410,7 @@ describe('Testing validating icon', () => { icons: { bar: { body: '', - [prop]: value, + foo: value, }, }, }, @@ -317,13 +420,13 @@ describe('Testing validating icon', () => { ); const icon = - item.type === 'boolean' + item.type !== 'object' ? { body: '', - [prop]: value, + foo: value, } : { - // [prop] should be deleted + // should be deleted body: '', }; @@ -333,82 +436,15 @@ describe('Testing validating icon', () => { bar: icon, }, }); - } catch (err) { - done( - `Expected to not throw error when ${prop} is being fixed` + } catch { + reject( + `Expected to not throw error when value is being fixed` ); + return; } }); - done(); + fulfill(true); }); }); - - // Unexpected field - test('foo', (done) => { - validationValues.forEach((item, value) => { - // Validate - try { - validateIconSet({ - prefix: 'foo', - icons: { - bar: { - body: '', - foo: value, - }, - }, - }); - - if (item.type === 'object') { - done(`Expected to throw error when value is ${item.text}`); - } - } catch (err) { - if (item.type !== 'object') { - done(`Expected to pass when value is ${item.text}`); - } - } - }); - - // Fix - validationValues.forEach((item, value) => { - try { - const result = validateIconSet( - { - prefix: 'foo', - icons: { - bar: { - body: '', - foo: value, - }, - }, - }, - { - fix: true, - } - ); - - const icon = - item.type !== 'object' - ? { - body: '', - foo: value, - } - : { - // should be deleted - body: '', - }; - - expect(result).toEqual({ - prefix: 'foo', - icons: { - bar: icon, - }, - }); - } catch (err) { - done(`Expected to not throw error when value is being fixed`); - } - }); - - done(); - }); }); diff --git a/packages/utils/tests/validate-test.ts b/packages/utils/tests/validate-test.ts index f8a2b93..9a0a1ca 100644 --- a/packages/utils/tests/validate-test.ts +++ b/packages/utils/tests/validate-test.ts @@ -1,36 +1,42 @@ import { validateIconSet } from '../lib/icon-set/validate'; describe('Testing validation', () => { - test('Not object', (done) => { - try { - validateIconSet(void 0); - done('Expected to throw error on undefined'); - } catch (err) { - // - } + test('Not object', () => { + return new Promise((fulfill, reject) => { + try { + validateIconSet(void 0); + reject('Expected to throw error on undefined'); + return; + } catch { + // + } - try { - validateIconSet({}); - done('Expected to throw error on empty object'); - } catch (err) { - // - } + try { + validateIconSet({}); + reject('Expected to throw error on empty object'); + return; + } catch { + // + } - try { - validateIconSet(null); - done('Expected to throw error on null'); - } catch (err) { - // - } + try { + validateIconSet(null); + reject('Expected to throw error on null'); + return; + } catch { + // + } - try { - validateIconSet([]); - done('Expected to throw error on array'); - } catch (err) { - // - } + try { + validateIconSet([]); + reject('Expected to throw error on array'); + return; + } catch { + // + } - done(); + fulfill(true); + }); }); test('Valid set', () => { @@ -53,40 +59,64 @@ describe('Testing validation', () => { }); }); - test('Missing stuff', (done) => { - try { - validateIconSet({ - prefix: 'foo', - }); - done('Expected to throw error when icons are missing'); - } catch (err) { - // - } + test('Missing stuff', () => { + return new Promise((fulfill, reject) => { + try { + validateIconSet({ + prefix: 'foo', + }); + reject('Expected to throw error when icons are missing'); + return; + } catch { + // + } - try { - validateIconSet({ - prefix: 'foo', - icons: {}, - }); - done('Expected to throw error when icons are empty'); - } catch (err) { - // - } + try { + validateIconSet({ + prefix: 'foo', + icons: {}, + }); + reject('Expected to throw error when icons are empty'); + return; + } catch { + // + } - try { - validateIconSet([]); - done('Expected to throw error on array'); - } catch (err) { - // - } + try { + validateIconSet([]); + reject('Expected to throw error on array'); + return; + } catch { + // + } - done(); + fulfill(true); + }); }); - test('Characters', (done) => { - // Correct icon set - expect( - validateIconSet({ + test('Characters', () => { + return new Promise((fulfill, reject) => { + // Correct icon set + expect( + validateIconSet({ + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + aliases: { + baz: { + parent: 'bar', + hFlip: true, + }, + }, + chars: { + e00: 'bar', + e01: 'baz', + }, + }) + ).toEqual({ prefix: 'foo', icons: { bar: { @@ -103,49 +133,11 @@ describe('Testing validation', () => { e00: 'bar', e01: 'baz', }, - }) - ).toEqual({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - aliases: { - baz: { - parent: 'bar', - hFlip: true, - }, - }, - chars: { - e00: 'bar', - e01: 'baz', - }, - }); - - // Missing icon - try { - validateIconSet({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - chars: { - e01: 'baz', - }, }); - done( - 'Expected to throw error when character points to missing icon' - ); - } catch (err) { - // - } - expect( - validateIconSet( - { + // Missing icon + try { + validateIconSet({ prefix: 'foo', icons: { bar: { @@ -155,39 +147,42 @@ describe('Testing validation', () => { chars: { e01: 'baz', }, - }, - { fix: true } - ) - ).toEqual({ - prefix: 'foo', - icons: { - bar: { - body: '', - }, - }, - }); + }); + reject( + 'Expected to throw error when character points to missing icon' + ); + return; + } catch { + // + } - // Bad character - try { - validateIconSet({ + expect( + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + chars: { + e01: 'baz', + }, + }, + { fix: true } + ) + ).toEqual({ prefix: 'foo', icons: { bar: { body: '', }, }, - chars: { - test: 'bar', - }, }); - done('Expected to throw error when character is invalid'); - } catch (err) { - // - } - expect( - validateIconSet( - { + // Bad character + try { + validateIconSet({ prefix: 'foo', icons: { bar: { @@ -195,93 +190,118 @@ describe('Testing validation', () => { }, }, chars: { - // Valid character - 'e000-f123': 'bar', - // Multiple invalid characters - 'test': 'bar', - 'E0': 'bar', + test: 'bar', + }, + }); + reject('Expected to throw error when character is invalid'); + return; + } catch { + // + } + + expect( + validateIconSet( + { + prefix: 'foo', + icons: { + bar: { + body: '', + }, + }, + chars: { + // Valid character + 'e000-f123': 'bar', + // Multiple invalid characters + 'test': 'bar', + 'E0': 'bar', + }, + }, + { fix: true } + ) + ).toEqual({ + prefix: 'foo', + icons: { + bar: { + body: '', }, }, - { fix: true } - ) - ).toEqual({ - prefix: 'foo', - icons: { - bar: { - body: '', + chars: { + 'e000-f123': 'bar', }, - }, - chars: { - 'e000-f123': 'bar', - }, - }); + }); - done(); + fulfill(true); + }); }); - test('Invalid default values', (done) => { - try { - validateIconSet( - { - prefix: 'foo', - icons: { - icon1: { - body: '', + test('Invalid default values', () => { + return new Promise((fulfill, reject) => { + try { + validateIconSet( + { + prefix: 'foo', + icons: { + icon1: { + body: '', + }, + }, + height: 24, + // Object + width: { + foo: 1, }, }, - height: 24, - // Object - width: { - foo: 1, - }, - }, - { fix: true } - ); - done('Expected to throw error for bad default properties'); - } catch (err) { - // - } + { fix: true } + ); + reject('Expected to throw error for bad default properties'); + return; + } catch { + // + } - try { - validateIconSet( - { - prefix: 'foo', - icons: { - icon1: { - body: '', + try { + validateIconSet( + { + prefix: 'foo', + icons: { + icon1: { + body: '', + }, }, + height: 24, + // Object + left: null, }, - height: 24, - // Object - left: null, - }, - { fix: true } - ); - done('Expected to throw error for bad default properties'); - } catch (err) { - // - } + { fix: true } + ); + reject('Expected to throw error for bad default properties'); + return; + } catch { + // + } - try { - validateIconSet( - { - prefix: 'foo', - icons: { - icon1: { - body: '', + try { + validateIconSet( + { + prefix: 'foo', + icons: { + icon1: { + body: '', + }, }, + height: 24, + // String + width: '32', }, - height: 24, - // String - width: '32', - }, - { fix: true } - ); - done('Expected to throw error for bad default properties'); - } catch (err) { - // - } + { fix: true } + ); + reject('Expected to throw error for bad default properties'); + return; + } catch { + // + } - done(); + fulfill(true); + }); }); });