2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-22 14:48:24 +00:00

Replace jest done function with promises in utils tests

This commit is contained in:
Vjacheslav Trushkin 2022-09-07 20:00:33 +03:00
parent 33b3e42834
commit 2dc12bd462
3 changed files with 679 additions and 608 deletions

View File

@ -46,26 +46,10 @@ describe('Testing validating alias', () => {
}); });
}); });
test('Null', (done) => { test('Null', () => {
try { return new Promise((fulfill, reject) => {
validateIconSet({ try {
prefix: 'foo', validateIconSet({
icons: {
bar: {
body: '<g />',
},
},
aliases: null,
});
done('Expected to throw error when aliases is null');
} catch (err) {
//
}
// Fix it
expect(
validateIconSet(
{
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
@ -73,48 +57,44 @@ describe('Testing validating alias', () => {
}, },
}, },
aliases: null, aliases: null,
}, });
{ fix: true } reject('Expected to throw error when aliases is null');
) return;
).toEqual({ } catch {
prefix: 'foo', //
icons: { }
bar: {
body: '<g />',
},
},
});
done(); // Fix it
}); expect(
validateIconSet(
test('Invalid parent', (done) => { {
try { prefix: 'foo',
const result = validateIconSet({ icons: {
bar: {
body: '<g />',
},
},
aliases: null,
},
{ fix: true }
)
).toEqual({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
body: '<g />', body: '<g />',
}, },
}, },
aliases: {
baz: {
parent: 'missing',
},
},
}); });
done(
'Expected to throw error when alias has missing parent, got ' +
JSON.stringify(result)
);
} catch (err) {
//
}
// Fix it fulfill(true);
expect( });
validateIconSet( });
{
test('Invalid parent', () => {
return new Promise((fulfill, reject) => {
try {
const result = validateIconSet({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
@ -126,51 +106,51 @@ describe('Testing validating alias', () => {
parent: 'missing', parent: 'missing',
}, },
}, },
}, });
{ fix: true } reject(
) 'Expected to throw error when alias has missing parent, got ' +
).toEqual({ JSON.stringify(result)
prefix: 'foo', );
icons: { return;
bar: { } catch {
body: '<g />', //
}, }
},
});
done(); // Fix it
}); expect(
validateIconSet(
test('Invalid parent, 2 levels', (done) => { {
try { prefix: 'foo',
const result = validateIconSet({ icons: {
bar: {
body: '<g />',
},
},
aliases: {
baz: {
parent: 'missing',
},
},
},
{ fix: true }
)
).toEqual({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
body: '<g />', body: '<g />',
}, },
}, },
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 fulfill(true);
expect( });
validateIconSet( });
{
test('Invalid parent, 2 levels', () => {
return new Promise((fulfill, reject) => {
try {
const result = validateIconSet({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
@ -185,51 +165,54 @@ describe('Testing validating alias', () => {
parent: 'baz', parent: 'baz',
}, },
}, },
}, });
{ fix: true } reject(
) 'Expected to throw error when alias has missing parent, got ' +
).toEqual({ JSON.stringify(result)
prefix: 'foo', );
icons: { return;
bar: { } catch {
body: '<g />', //
}, }
},
});
done(); // Fix it
}); expect(
validateIconSet(
test('Invalid parent, 2 levels, reverse order', (done) => { {
try { prefix: 'foo',
const result = validateIconSet({ icons: {
bar: {
body: '<g />',
},
},
aliases: {
baz: {
parent: 'missing',
},
baz2: {
parent: 'baz',
},
},
},
{ fix: true }
)
).toEqual({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
body: '<g />', body: '<g />',
}, },
}, },
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 fulfill(true);
expect( });
validateIconSet( });
{
test('Invalid parent, 2 levels, reverse order', () => {
return new Promise((fulfill, reject) => {
try {
const result = validateIconSet({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
@ -244,54 +227,54 @@ describe('Testing validating alias', () => {
parent: 'missing', parent: 'missing',
}, },
}, },
}, });
{ fix: true } reject(
) 'Expected to throw error when alias has missing parent, got ' +
).toEqual({ JSON.stringify(result)
prefix: 'foo', );
icons: { return;
bar: { } catch {
body: '<g />', //
}, }
},
});
done(); // Fix it
}); expect(
validateIconSet(
test('Parent loop', (done) => { {
try { prefix: 'foo',
const result = validateIconSet({ icons: {
bar: {
body: '<g />',
},
},
aliases: {
baz: {
parent: 'baz2',
},
baz2: {
parent: 'missing',
},
},
},
{ fix: true }
)
).toEqual({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
body: '<g />', body: '<g />',
}, },
}, },
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 fulfill(true);
expect( });
validateIconSet( });
{
test('Parent loop', () => {
return new Promise((fulfill, reject) => {
try {
const result = validateIconSet({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
@ -309,23 +292,55 @@ describe('Testing validating alias', () => {
parent: 'bar', parent: 'bar',
}, },
}, },
}, });
{ fix: true } reject(
) 'Expected to throw error when alias has missing parent, got ' +
).toEqual({ JSON.stringify(result)
prefix: 'foo', );
icons: { return;
bar: { } catch {
body: '<g />', //
}, }
},
aliases: {
baz3: {
parent: 'bar',
},
},
});
done(); // Fix it
expect(
validateIconSet(
{
prefix: 'foo',
icons: {
bar: {
body: '<g />',
},
},
aliases: {
baz: {
parent: 'baz2',
},
baz2: {
parent: 'baz',
},
baz3: {
parent: 'bar',
},
},
},
{ fix: true }
)
).toEqual({
prefix: 'foo',
icons: {
bar: {
body: '<g />',
},
},
aliases: {
baz3: {
parent: 'bar',
},
},
});
fulfill(true);
});
}); });
}); });

View File

@ -54,45 +54,25 @@ describe('Testing validating icon', () => {
}); });
// Required string // Required string
test('body', (done) => { test('body', () => {
// Missing body return new Promise((fulfill, reject) => {
try { // Missing body
validateIconSet({ try {
prefix: 'foo', validateIconSet({
icons: {
bar: {
width: 16,
},
},
});
done('Expected to throw error when body is missing');
} catch (err) {
//
}
try {
validateIconSet(
{
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
width: 16, width: 16,
}, },
}, },
}, });
{ reject('Expected to throw error when body is missing');
fix: true, return;
} } catch {
); //
done( }
'Expected to throw error when body is missing and cannot be fixed'
);
} catch (err) {
//
}
try { try {
expect(
validateIconSet( validateIconSet(
{ {
prefix: 'foo', prefix: 'foo',
@ -100,140 +80,86 @@ describe('Testing validating icon', () => {
bar: { bar: {
width: 16, width: 16,
}, },
baz: {
body: '<g />',
},
}, },
}, },
{ {
fix: true, fix: true,
} }
) );
).toEqual({ reject(
prefix: 'foo', 'Expected to throw error when body is missing and cannot be fixed'
icons: { );
baz: { return;
body: '<g />', } catch {
}, //
}, }
});
} catch (err) {
done(
'Expected to not throw error when body is missing, but icon set can be fixed'
);
//
}
validationValues.forEach((item, value) => {
// Validate without fixing
try { try {
validateIconSet({ expect(
validateIconSet(
{
prefix: 'foo',
icons: {
bar: {
width: 16,
},
baz: {
body: '<g />',
},
},
},
{
fix: true,
}
)
).toEqual({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { baz: {
body: value, body: '<g />',
}, },
}, },
}); });
} catch {
if (item.type !== 'string') { reject(
done(`Expected to throw error when body is ${item.text}`); 'Expected to not throw error when body is missing, but icon set can be fixed'
}
} 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,
}
); );
return;
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 not fail because another icon is valid)
try {
validateIconSet(
{
prefix: 'foo',
icons: {
bar: {
body: value,
},
baz: {
body: '<g />',
},
},
},
{
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) => { validationValues.forEach((item, value) => {
// Validate without fixing
try { try {
validateIconSet({ validateIconSet({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
body: '<g />', body: value,
[prop]: value,
}, },
}, },
}); });
if (item.type !== 'number') { if (item.type !== 'string') {
done( reject(
`Expected to throw error when ${prop} is ${item.text}` `Expected to throw error when body is ${item.text}`
); );
return;
} }
} catch (err) { } catch {
if (item.type === 'number') { if (item.type === 'string') {
done(`Expected to pass when ${prop} is ${item.text}`); reject(`Expected to pass when body is ${item.text}`);
return;
} }
} }
});
// Fix // Attempt to fix (will fail because icon set is empty after failing icon is removed)
validationValues.forEach((item, value) => {
try { try {
const result = validateIconSet( validateIconSet(
{ {
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
body: '<g />', body: value,
[prop]: value,
}, },
}, },
}, },
@ -242,37 +168,212 @@ describe('Testing validating icon', () => {
} }
); );
const icon = if (item.type !== 'string') {
item.type === 'number' reject(
? { `Expected to throw error when body is ${item.text}`
body: '<g />', );
[prop]: value, return;
} }
: { } catch {
// [prop] should be deleted if (item.type === 'string') {
body: '<g />', reject(`Expected to pass when body is ${item.text}`);
}; return;
}
}
expect(result).toEqual({ // Attempt to fix (will not fail because another icon is valid)
prefix: 'foo', try {
icons: { validateIconSet(
bar: icon, {
prefix: 'foo',
icons: {
bar: {
body: value,
},
baz: {
body: '<g />',
},
},
}, },
}); {
} catch (err) { fix: true,
done( }
`Expected to not throw error when ${prop} is being fixed`
); );
} 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: '<g />',
[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: '<g />',
[prop]: value,
},
},
},
{
fix: true,
}
);
const icon =
item.type === 'number'
? {
body: '<g />',
[prop]: value,
}
: {
// [prop] should be deleted
body: '<g />',
};
expect(result).toEqual({
prefix: 'foo',
icons: {
bar: icon,
},
});
} catch {
reject(
`Expected to not throw error when ${prop} is being fixed`
);
return;
}
});
fulfill(true);
});
}); });
}); });
// Boolean // Boolean
['hFlip', 'vFlip', 'hidden'].forEach((prop) => { ['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: '<g />',
[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: '<g />',
[prop]: value,
},
},
},
{
fix: true,
}
);
const icon =
item.type === 'boolean'
? {
body: '<g />',
[prop]: value,
}
: {
// [prop] should be deleted
body: '<g />',
};
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) => { validationValues.forEach((item, value) => {
// Validate // Validate
try { try {
@ -281,19 +382,21 @@ describe('Testing validating icon', () => {
icons: { icons: {
bar: { bar: {
body: '<g />', body: '<g />',
[prop]: value, foo: value,
}, },
}, },
}); });
if (item.type !== 'boolean') { if (item.type === 'object') {
done( reject(
`Expected to throw error when ${prop} is ${item.text}` `Expected to throw error when value is ${item.text}`
); );
return;
} }
} catch (err) { } catch {
if (item.type === 'boolean') { if (item.type !== 'object') {
done(`Expected to pass when ${prop} is ${item.text}`); reject(`Expected to pass when value is ${item.text}`);
return;
} }
} }
}); });
@ -307,7 +410,7 @@ describe('Testing validating icon', () => {
icons: { icons: {
bar: { bar: {
body: '<g />', body: '<g />',
[prop]: value, foo: value,
}, },
}, },
}, },
@ -317,13 +420,13 @@ describe('Testing validating icon', () => {
); );
const icon = const icon =
item.type === 'boolean' item.type !== 'object'
? { ? {
body: '<g />', body: '<g />',
[prop]: value, foo: value,
} }
: { : {
// [prop] should be deleted // should be deleted
body: '<g />', body: '<g />',
}; };
@ -333,82 +436,15 @@ describe('Testing validating icon', () => {
bar: icon, bar: icon,
}, },
}); });
} catch (err) { } catch {
done( reject(
`Expected to not throw error when ${prop} is being fixed` `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: '<g />',
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: '<g />',
foo: value,
},
},
},
{
fix: true,
}
);
const icon =
item.type !== 'object'
? {
body: '<g />',
foo: value,
}
: {
// should be deleted
body: '<g />',
};
expect(result).toEqual({
prefix: 'foo',
icons: {
bar: icon,
},
});
} catch (err) {
done(`Expected to not throw error when value is being fixed`);
}
});
done();
});
}); });

View File

@ -1,36 +1,42 @@
import { validateIconSet } from '../lib/icon-set/validate'; import { validateIconSet } from '../lib/icon-set/validate';
describe('Testing validation', () => { describe('Testing validation', () => {
test('Not object', (done) => { test('Not object', () => {
try { return new Promise((fulfill, reject) => {
validateIconSet(void 0); try {
done('Expected to throw error on undefined'); validateIconSet(void 0);
} catch (err) { reject('Expected to throw error on undefined');
// return;
} } catch {
//
}
try { try {
validateIconSet({}); validateIconSet({});
done('Expected to throw error on empty object'); reject('Expected to throw error on empty object');
} catch (err) { return;
// } catch {
} //
}
try { try {
validateIconSet(null); validateIconSet(null);
done('Expected to throw error on null'); reject('Expected to throw error on null');
} catch (err) { return;
// } catch {
} //
}
try { try {
validateIconSet([]); validateIconSet([]);
done('Expected to throw error on array'); reject('Expected to throw error on array');
} catch (err) { return;
// } catch {
} //
}
done(); fulfill(true);
});
}); });
test('Valid set', () => { test('Valid set', () => {
@ -53,40 +59,64 @@ describe('Testing validation', () => {
}); });
}); });
test('Missing stuff', (done) => { test('Missing stuff', () => {
try { return new Promise((fulfill, reject) => {
validateIconSet({ try {
prefix: 'foo', validateIconSet({
}); prefix: 'foo',
done('Expected to throw error when icons are missing'); });
} catch (err) { reject('Expected to throw error when icons are missing');
// return;
} } catch {
//
}
try { try {
validateIconSet({ validateIconSet({
prefix: 'foo', prefix: 'foo',
icons: {}, icons: {},
}); });
done('Expected to throw error when icons are empty'); reject('Expected to throw error when icons are empty');
} catch (err) { return;
// } catch {
} //
}
try { try {
validateIconSet([]); validateIconSet([]);
done('Expected to throw error on array'); reject('Expected to throw error on array');
} catch (err) { return;
// } catch {
} //
}
done(); fulfill(true);
});
}); });
test('Characters', (done) => { test('Characters', () => {
// Correct icon set return new Promise((fulfill, reject) => {
expect( // Correct icon set
validateIconSet({ expect(
validateIconSet({
prefix: 'foo',
icons: {
bar: {
body: '<g />',
},
},
aliases: {
baz: {
parent: 'bar',
hFlip: true,
},
},
chars: {
e00: 'bar',
e01: 'baz',
},
})
).toEqual({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
@ -103,49 +133,11 @@ describe('Testing validation', () => {
e00: 'bar', e00: 'bar',
e01: 'baz', e01: 'baz',
}, },
})
).toEqual({
prefix: 'foo',
icons: {
bar: {
body: '<g />',
},
},
aliases: {
baz: {
parent: 'bar',
hFlip: true,
},
},
chars: {
e00: 'bar',
e01: 'baz',
},
});
// Missing icon
try {
validateIconSet({
prefix: 'foo',
icons: {
bar: {
body: '<g />',
},
},
chars: {
e01: 'baz',
},
}); });
done(
'Expected to throw error when character points to missing icon'
);
} catch (err) {
//
}
expect( // Missing icon
validateIconSet( try {
{ validateIconSet({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
@ -155,39 +147,42 @@ describe('Testing validation', () => {
chars: { chars: {
e01: 'baz', e01: 'baz',
}, },
}, });
{ fix: true } reject(
) 'Expected to throw error when character points to missing icon'
).toEqual({ );
prefix: 'foo', return;
icons: { } catch {
bar: { //
body: '<g />', }
},
},
});
// Bad character expect(
try { validateIconSet(
validateIconSet({ {
prefix: 'foo',
icons: {
bar: {
body: '<g />',
},
},
chars: {
e01: 'baz',
},
},
{ fix: true }
)
).toEqual({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
body: '<g />', body: '<g />',
}, },
}, },
chars: {
test: 'bar',
},
}); });
done('Expected to throw error when character is invalid');
} catch (err) {
//
}
expect( // Bad character
validateIconSet( try {
{ validateIconSet({
prefix: 'foo', prefix: 'foo',
icons: { icons: {
bar: { bar: {
@ -195,93 +190,118 @@ describe('Testing validation', () => {
}, },
}, },
chars: { chars: {
// Valid character test: 'bar',
'e000-f123': 'bar', },
// Multiple invalid characters });
'test': 'bar', reject('Expected to throw error when character is invalid');
'E0': 'bar', return;
} catch {
//
}
expect(
validateIconSet(
{
prefix: 'foo',
icons: {
bar: {
body: '<g />',
},
},
chars: {
// Valid character
'e000-f123': 'bar',
// Multiple invalid characters
'test': 'bar',
'E0': 'bar',
},
},
{ fix: true }
)
).toEqual({
prefix: 'foo',
icons: {
bar: {
body: '<g />',
}, },
}, },
{ fix: true } chars: {
) 'e000-f123': 'bar',
).toEqual({
prefix: 'foo',
icons: {
bar: {
body: '<g />',
}, },
}, });
chars: {
'e000-f123': 'bar',
},
});
done(); fulfill(true);
});
}); });
test('Invalid default values', (done) => { test('Invalid default values', () => {
try { return new Promise((fulfill, reject) => {
validateIconSet( try {
{ validateIconSet(
prefix: 'foo', {
icons: { prefix: 'foo',
icon1: { icons: {
body: '<path d="icon1" />', icon1: {
body: '<path d="icon1" />',
},
},
height: 24,
// Object
width: {
foo: 1,
}, },
}, },
height: 24, { fix: true }
// Object );
width: { reject('Expected to throw error for bad default properties');
foo: 1, return;
}, } catch {
}, //
{ fix: true } }
);
done('Expected to throw error for bad default properties');
} catch (err) {
//
}
try { try {
validateIconSet( validateIconSet(
{ {
prefix: 'foo', prefix: 'foo',
icons: { icons: {
icon1: { icon1: {
body: '<path d="icon1" />', body: '<path d="icon1" />',
},
}, },
height: 24,
// Object
left: null,
}, },
height: 24, { fix: true }
// Object );
left: null, reject('Expected to throw error for bad default properties');
}, return;
{ fix: true } } catch {
); //
done('Expected to throw error for bad default properties'); }
} catch (err) {
//
}
try { try {
validateIconSet( validateIconSet(
{ {
prefix: 'foo', prefix: 'foo',
icons: { icons: {
icon1: { icon1: {
body: '<path d="icon1" />', body: '<path d="icon1" />',
},
}, },
height: 24,
// String
width: '32',
}, },
height: 24, { fix: true }
// String );
width: '32', reject('Expected to throw error for bad default properties');
}, return;
{ fix: true } } catch {
); //
done('Expected to throw error for bad default properties'); }
} catch (err) {
//
}
done(); fulfill(true);
});
}); });
}); });