2
0
mirror of https://github.com/iconify/iconify.git synced 2025-04-11 02:21:51 +00:00

Remove Jasmine tests from core

This commit is contained in:
Vjacheslav Trushkin 2022-03-16 10:08:49 +02:00
parent 16146d553e
commit b985c4e69e
18 changed files with 2 additions and 3949 deletions

View File

@ -3,7 +3,6 @@ module.exports = {
browser: true,
es6: true,
node: true,
jasmine: true,
},
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
globals: {
@ -11,7 +10,7 @@ module.exports = {
SharedArrayBuffer: 'readonly',
},
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'jasmine'],
plugins: ['@typescript-eslint'],
rules: {
'no-mixed-spaces-and-tabs': ['off'],
'no-unused-vars': ['off'],

View File

@ -20,8 +20,6 @@
"cross-env": "^7.0.3",
"cross-fetch": "^3.1.5",
"eslint": "^8.7.0",
"eslint-plugin-jasmine": "^4.1.3",
"jasmine": "^4.0.2",
"jest": "^27.4.7",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.3",
@ -2942,16 +2940,6 @@
"url": "https://opencollective.com/eslint"
}
},
"node_modules/eslint-plugin-jasmine": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/eslint-plugin-jasmine/-/eslint-plugin-jasmine-4.1.3.tgz",
"integrity": "sha512-q8j8KnLH/4uwmPELFZvEyfEcuCuGxXScJaRdqHjOjz064GcfX6aoFbzy5VohZ5QYk2+WvoqMoqDSb9nRLf89GQ==",
"dev": true,
"engines": {
"node": ">=8",
"npm": ">=6"
}
},
"node_modules/eslint-scope": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-5.1.1.tgz",
@ -3831,25 +3819,6 @@
"node": ">=8"
}
},
"node_modules/jasmine": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/jasmine/-/jasmine-4.0.2.tgz",
"integrity": "sha512-YsrgxJQEggxzByYe4j68eQLOiQeSrPDYGv4sHhGBp3c6HHdq+uPXeAQ73kOAQpdLZ3/0zN7x/TZTloqeE1/qIA==",
"dev": true,
"dependencies": {
"glob": "^7.1.6",
"jasmine-core": "^4.0.0"
},
"bin": {
"jasmine": "bin/jasmine.js"
}
},
"node_modules/jasmine-core": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-4.0.0.tgz",
"integrity": "sha512-tq24OCqHElgU9KDpb/8O21r1IfotgjIzalfW9eCmRR40LZpvwXT68iariIyayMwi0m98RDt16aljdbwK0sBMmQ==",
"dev": true
},
"node_modules/jest": {
"version": "27.4.7",
"resolved": "https://registry.npmjs.org/jest/-/jest-27.4.7.tgz",
@ -8658,12 +8627,6 @@
}
}
},
"eslint-plugin-jasmine": {
"version": "4.1.3",
"resolved": "https://registry.npmjs.org/eslint-plugin-jasmine/-/eslint-plugin-jasmine-4.1.3.tgz",
"integrity": "sha512-q8j8KnLH/4uwmPELFZvEyfEcuCuGxXScJaRdqHjOjz064GcfX6aoFbzy5VohZ5QYk2+WvoqMoqDSb9nRLf89GQ==",
"dev": true
},
"eslint-scope": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-5.1.1.tgz",
@ -9299,22 +9262,6 @@
"istanbul-lib-report": "^3.0.0"
}
},
"jasmine": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/jasmine/-/jasmine-4.0.2.tgz",
"integrity": "sha512-YsrgxJQEggxzByYe4j68eQLOiQeSrPDYGv4sHhGBp3c6HHdq+uPXeAQ73kOAQpdLZ3/0zN7x/TZTloqeE1/qIA==",
"dev": true,
"requires": {
"glob": "^7.1.6",
"jasmine-core": "^4.0.0"
}
},
"jasmine-core": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/jasmine-core/-/jasmine-core-4.0.0.tgz",
"integrity": "sha512-tq24OCqHElgU9KDpb/8O21r1IfotgjIzalfW9eCmRR40LZpvwXT68iariIyayMwi0m98RDt16aljdbwK0sBMmQ==",
"dev": true
},
"jest": {
"version": "27.4.7",
"resolved": "https://registry.npmjs.org/jest/-/jest-27.4.7.tgz",

View File

@ -17,8 +17,7 @@
"prebuild": "npm run lint && npm run clean",
"build": "unbuild",
"test:jest": "jest --runInBand",
"test:jasmine": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jasmine",
"test": "npm run test:jest && npm run test:jasmine"
"test": "npm run test:jest"
},
"exports": {
"./*": "./*",
@ -111,8 +110,6 @@
"cross-env": "^7.0.3",
"cross-fetch": "^3.1.5",
"eslint": "^8.7.0",
"eslint-plugin-jasmine": "^4.1.3",
"jasmine": "^4.0.2",
"jest": "^27.4.7",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.3",

View File

@ -1,537 +0,0 @@
import {
callbacks,
updateCallbacks,
storeCallback,
} from '@iconify/core/lib/api/callbacks';
import { sortIcons } from '@iconify/core/lib/icon/sort';
import { getStorage, addIconSet } from '@iconify/core/lib/storage/storage';
describe('Testing API callbacks', () => {
let prefixCounter = 0;
function nextPrefix() {
prefixCounter++;
return 'api-cb-test-' + (prefixCounter < 10 ? '0' : '') + prefixCounter;
}
it('Simple callback', (done) => {
const provider = 'iconify';
const prefix = nextPrefix();
let counter = 0;
const storage = getStorage(provider, prefix);
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
counter++;
switch (counter) {
case 1:
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
name: 'icon2',
},
]);
expect(callbacks[provider][prefix].length).toBe(1);
// Add icon2 and trigger update
addIconSet(storage, {
prefix: prefix,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(provider, prefix);
return;
case 2:
// Second run - icon2 should be added, completing callback
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([]);
expect(callbacks[provider][prefix].length).toBe(0);
done();
}
},
sortIcons([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
{
provider,
prefix,
name: 'icon3',
},
]),
[
{
provider,
prefix,
},
]
);
// Test callbacks
expect(callbacks[provider][prefix].length).toBe(1);
// Test update - should do nothing
updateCallbacks(provider, prefix);
// Wait for tick because updateCallbacks will use one
setTimeout(() => {
// Callback should not have been called yet
expect(counter).toBe(0);
// Add few icons and run updateCallbacks
addIconSet(storage, {
prefix: prefix,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(provider, prefix);
});
});
it('Callback that should not be stored', () => {
const provider = '';
const prefix = nextPrefix();
const storage = getStorage(provider, prefix);
addIconSet(storage, {
prefix,
icons: {
icon1: {
body: '<path d="" />',
},
icon2: {
body: '<path d="" />',
},
},
not_found: ['icon3'],
});
storeCallback(
() => {
throw new Error('This code should not be executed!');
},
sortIcons([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
{
provider,
prefix,
name: 'icon3',
},
]),
[
{
provider,
prefix,
},
]
);
// callbacks should not have been initialised
expect(callbacks[prefix]).toBeUndefined();
});
it('Cancel callback', (done) => {
const provider = 'foo';
const prefix = nextPrefix();
let counter = 0;
const storage = getStorage(provider, prefix);
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
counter++;
expect(counter).toBe(1);
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
name: 'icon2',
},
]);
expect(callbacks[provider][prefix].length).toBe(1);
// Add icon2 and trigger update
addIconSet(storage, {
prefix: prefix,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(provider, prefix);
// Unsubscribe and set timer to call done()
unsubscribe();
expect(callbacks[provider][prefix].length).toBe(0);
setTimeout(done);
},
sortIcons([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
{
provider,
prefix,
name: 'icon3',
},
]),
[
{
provider,
prefix,
},
]
);
// Test callbacks
expect(callbacks[provider][prefix].length).toBe(1);
// Test update - should do nothing
updateCallbacks(provider, prefix);
// Wait for tick because updateCallbacks will use one
setTimeout(() => {
// Callback should not have been called yet
expect(counter).toBe(0);
// Add few icons and run updateCallbacks
addIconSet(storage, {
prefix: prefix,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(provider, prefix);
});
});
it('Multiple prefixes', (done) => {
const provider = '';
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
let counter = 0;
const storage1 = getStorage(provider, prefix1);
const storage2 = getStorage(provider, prefix2);
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
counter++;
switch (counter) {
case 1:
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
{
provider,
prefix: prefix1,
name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider,
prefix: prefix1,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider,
prefix: prefix2,
name: 'icon2',
},
]);
expect(callbacks[provider][prefix1].length).toBe(0);
expect(callbacks[provider][prefix2].length).toBe(1);
// Add icon2 and trigger update
addIconSet(storage2, {
prefix: prefix2,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(provider, prefix2);
break;
case 2:
// Second run - icon2 should be loaded
expect(callbacks[provider][prefix1].length).toBe(0);
expect(callbacks[provider][prefix2].length).toBe(0);
done();
break;
default:
done('Callback was called ' + counter + ' times.');
}
},
sortIcons([
{
provider,
prefix: prefix1,
name: 'icon1',
},
{
provider,
prefix: prefix2,
name: 'icon2',
},
{
provider,
prefix: prefix1,
name: 'icon3',
},
]),
[
{ provider, prefix: prefix1 },
{ provider, prefix: prefix2 },
]
);
// Test callbacks
expect(callbacks[provider][prefix1].length).toBe(1);
expect(callbacks[provider][prefix2].length).toBe(1);
// Test update - should do nothing
updateCallbacks(provider, prefix1);
// Wait for tick because updateCallbacks will use one
setTimeout(() => {
// Callback should not have been called yet
expect(counter).toBe(0);
// Add few icons and run updateCallbacks
addIconSet(storage1, {
prefix: prefix1,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(provider, prefix1);
});
});
it('Multiple providers', (done) => {
const provider1 = nextPrefix();
const provider2 = nextPrefix();
const prefix1 = nextPrefix();
const prefix2 = nextPrefix();
let counter = 0;
const storage1 = getStorage(provider1, prefix1);
const storage2 = getStorage(provider2, prefix2);
const abort = storeCallback(
(loaded, missing, pending, unsubscribe) => {
expect(unsubscribe).toBe(abort);
counter++;
switch (counter) {
case 1:
// First run - icon1 should be loaded, icon3 should be missing
expect(loaded).toEqual([
{
provider: provider1,
prefix: prefix1,
name: 'icon1',
},
]);
expect(missing).toEqual([
{
provider: provider1,
prefix: prefix1,
name: 'icon3',
},
]);
expect(pending).toEqual([
{
provider: provider2,
prefix: prefix2,
name: 'icon2',
},
]);
expect(callbacks[provider1][prefix1].length).toBe(0);
expect(callbacks[provider2][prefix2].length).toBe(1);
// Make sure providers/prefixes aren't mixed
expect(callbacks[provider1][prefix2]).toBeUndefined();
expect(callbacks[provider2][prefix1]).toBeUndefined();
// Add icon2 and trigger update
addIconSet(storage2, {
prefix: prefix2,
icons: {
icon2: {
body: '<g></g>',
},
},
});
updateCallbacks(provider2, prefix2);
break;
case 2:
// Second run - icon2 should be loaded
expect(callbacks[provider1][prefix1].length).toBe(0);
expect(callbacks[provider2][prefix2].length).toBe(0);
// Make sure providers/prefixes aren't mixed
expect(callbacks[provider1][prefix2]).toBeUndefined();
expect(callbacks[provider2][prefix1]).toBeUndefined();
done();
break;
default:
done('Callback was called ' + counter + ' times.');
}
},
sortIcons([
{
provider: provider1,
prefix: prefix1,
name: 'icon1',
},
{
provider: provider2,
prefix: prefix2,
name: 'icon2',
},
{
provider: provider1,
prefix: prefix1,
name: 'icon3',
},
]),
[
{ provider: provider1, prefix: prefix1 },
{ provider: provider2, prefix: prefix2 },
]
);
// Test callbacks
expect(callbacks[provider1][prefix1].length).toBe(1);
expect(callbacks[provider2][prefix2].length).toBe(1);
expect(callbacks[provider1][prefix2]).toBeUndefined();
expect(callbacks[provider2][prefix1]).toBeUndefined();
// Test update - should do nothing
updateCallbacks(provider1, prefix1);
// Wait for tick because updateCallbacks will use one
setTimeout(() => {
// Callback should not have been called yet
expect(counter).toBe(0);
// Add few icons and run updateCallbacks
addIconSet(storage1, {
prefix: prefix1,
icons: {
icon1: {
body: '<g></g>',
},
},
not_found: ['icon3'],
});
updateCallbacks(provider1, prefix1);
});
});
});

View File

@ -1,82 +0,0 @@
import crossFetch from 'cross-fetch';
import { sendAPIQuery } from '@iconify/core/lib/api/query';
import { setAPIModule } from '@iconify/core/lib/api/modules';
import { fetchAPIModule, setFetch } from '@iconify/core/lib/api/modules/fetch';
import { addAPIProvider } from '@iconify/core/lib/api/config';
import { mockAPIModule } from '@iconify/core/lib/api/modules/mock';
describe('Testing live API with fetch', () => {
let counter = 0;
function nextProvider() {
return 'fetch-' + counter++;
}
const host = 'https://api.iconify.design';
// Set fetch module
beforeEach(() => {
setFetch(crossFetch);
setAPIModule('', fetchAPIModule);
});
afterAll(() => {
setAPIModule('', mockAPIModule);
});
it('Missing API configuration', (done) => {
const provider = nextProvider();
sendAPIQuery(
provider,
{
type: 'custom',
provider,
uri: '/collections',
},
(data, error) => {
expect(error).toBe(424);
expect(data).toBeUndefined();
done();
}
);
});
it('Custom request with provider', (done) => {
const provider = nextProvider();
expect(
addAPIProvider(provider, {
resources: [host],
})
).toBe(true);
sendAPIQuery(
provider,
{
type: 'custom',
provider,
uri: '/collections',
},
(data, error) => {
expect(error).toBeUndefined();
expect(typeof data).toBe('object');
done();
}
);
});
it('Custom request with host', (done) => {
sendAPIQuery(
{
resources: [host],
},
{
type: 'custom',
uri: '/collections',
},
(data, error) => {
expect(error).toBeUndefined();
expect(typeof data).toBe('object');
done();
}
);
});
});

View File

@ -1,651 +0,0 @@
import { addAPIProvider } from '@iconify/core/lib/api/config';
import { setAPIModule } from '@iconify/core/lib/api/modules';
import { loadIcons, isPending } from '@iconify/core/lib/api/icons';
describe('Testing API loadIcons', () => {
let prefixCounter = 0;
function nextPrefix() {
prefixCounter++;
return (
'api-load-test-' + (prefixCounter < 10 ? '0' : '') + prefixCounter
);
}
it('Loading few icons', (done) => {
const provider = nextPrefix();
const prefix = nextPrefix();
let asyncCounter = 0;
// Set config
addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'],
});
// Icon loader
const prepareQuery = (provider, prefix, icons) => {
const item = {
type: 'icons',
provider,
prefix,
icons,
};
// This callback should be called first
expect(asyncCounter).toBe(1);
asyncCounter++;
// Test input and return as one item
const expected = {
type: 'icons',
provider,
prefix,
icons: ['icon1', 'icon2'],
};
expect(item).toEqual(expected);
return [item];
};
const sendQuery = (host, params, callback) => {
// This callback should be called after prepareQuery
expect(asyncCounter).toBe(2);
asyncCounter++;
expect(params.type).toBe('icons');
// Test input
expect(host).toBe('https://api1.local');
const expected = {
type: 'icons',
provider,
prefix,
icons: ['icon1', 'icon2'],
};
expect(params).toEqual(expected);
// Send data
callback('success', {
prefix,
icons: {
icon1: {
body: '<path d="" />',
},
icon2: {
body: '<path d="" />',
},
},
});
// Counter should not have increased after status.done() call becuse parsing result should be done on next tick
expect(asyncCounter).toBe(3);
};
setAPIModule(provider, {
prepare: prepareQuery,
send: sendQuery,
});
// Load icons
loadIcons(
[
// as icon
{
provider,
prefix,
name: 'icon1',
},
// as string
provider + ':' + prefix + ':icon2',
],
(loaded, missing, pending) => {
// This callback should be called last
expect(asyncCounter).toBe(3);
asyncCounter++;
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([]);
expect(pending).toEqual([]);
expect(
isPending({
provider,
prefix,
name: 'icon1',
})
).toBe(false);
expect(isPending({ provider, prefix, name: 'icon3' })).toBe(
false
);
done();
}
);
// Test isPending
expect(isPending({ provider, prefix, name: 'icon1' })).toBe(true);
expect(isPending({ provider, prefix, name: 'icon3' })).toBe(false);
// Make sure asyncCounter wasn't increased because loading shoud happen on next tick
expect(asyncCounter).toBe(0);
asyncCounter++;
});
it('Split results', (done) => {
const provider = nextPrefix();
const prefix = nextPrefix();
// Set config
addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'],
});
// Icon loader
const prepareQuery = (provider, prefix, icons) => {
// Split all icons in multiple queries, one icon per query
const results = [];
icons.forEach((icon) => {
const item = {
type: 'icons',
provider,
prefix,
icons: [icon],
};
results.push(item);
});
expect(results.length).toBe(2);
return results;
};
let queryCounter = 0;
const sendQuery = (host, params, callback) => {
// Test input
expect(host).toBe('https://api1.local');
expect(params.type).toBe('icons');
if (params.type !== 'icons') {
return;
}
// Icon names should match queryCounter: 'icon1' on first run, 'icon2' on second run
queryCounter++;
const expected = {
type: 'icons',
provider,
prefix,
icons: ['icon' + queryCounter],
};
expect(params).toEqual(expected);
// Send only requested icons
const icons = Object.create(null);
params.icons.forEach((icon) => {
icons[icon] = {
body: '<path d="" />',
};
});
callback('success', {
prefix,
icons,
// Test mismatched provider: should be ignored because provider name is not affected by actual API response
provider: nextPrefix(),
});
};
setAPIModule(provider, {
prepare: prepareQuery,
send: sendQuery,
});
// Load icons
let callbackCalled = false;
loadIcons(
[
provider + ':' + prefix + ':icon1',
provider + ':' + prefix + ':icon2',
],
(loaded, missing, pending) => {
// Callback should be called only once because results should be sent in same tick
expect(callbackCalled).toBe(false);
callbackCalled = true;
// Test data
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([]);
expect(pending).toEqual([]);
done();
}
);
});
it('Fail on default host', (done) => {
const provider = nextPrefix();
const prefix = nextPrefix();
// Set config
addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test
});
// Icon loader
const prepareQuery = (provider, prefix, icons) => {
const item = {
type: 'icons',
provider,
prefix,
icons,
};
return [item];
};
let queryCounter = 0;
const sendQuery = (host, params, callback) => {
queryCounter++;
params;
switch (queryCounter) {
case 1:
// First call on api1
expect(host).toBe('https://api1.local');
// Do nothing - fake failed response
break;
case 2:
// First call on api2
expect(host).toBe('https://api2.local');
// Return result
callback('success', {
prefix,
icons: {
icon1: {
body: '<path d="" />',
},
icon2: {
body: '<path d="" />',
},
},
});
break;
default:
done(
`Unexpected additional call to sendQuery for host ${host}.`
);
}
};
setAPIModule(provider, {
prepare: prepareQuery,
send: sendQuery,
});
// Load icons
let callbackCalled = false;
loadIcons(
[
provider + ':' + prefix + ':icon1',
provider + ':' + prefix + ':icon2',
],
(loaded, missing, pending) => {
// Callback should be called only once
expect(callbackCalled).toBe(false);
callbackCalled = true;
// Test data
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([]);
expect(pending).toEqual([]);
done();
}
);
});
it('Fail on default host, multiple queries', (done) => {
const provider = nextPrefix();
const prefix = nextPrefix();
// Set config
addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test
});
// Icon loader
const prepareQuery = (provider, prefix, icons) => {
const item = {
type: 'icons',
provider,
prefix,
icons,
};
return [item];
};
let queryCounter = 0;
const sendQuery = (host, params, callback) => {
queryCounter++;
expect(params.type).toBe('icons');
if (params.type !== 'icons') {
return;
}
switch (queryCounter) {
case 1:
// First call on api1
expect(params.icons).toEqual(['icon1', 'icon2']);
expect(host).toBe('https://api1.local');
// Do nothing - fake failed response
break;
case 2:
// First call on api2
expect(params.icons).toEqual(['icon1', 'icon2']);
expect(host).toBe('https://api2.local');
// Return result
callback('success', {
prefix,
icons: {
icon1: {
body: '<path d="" />',
},
icon2: {
body: '<path d="" />',
},
},
});
break;
case 3:
// Second call, should have api2 as default
expect(params.icons).toEqual(['icon3', 'icon4']);
expect(host).toBe('https://api2.local');
// Return result
callback('success', {
prefix,
icons: {
icon3: {
body: '<path d="" />',
},
icon4: {
body: '<path d="" />',
},
},
});
break;
default:
done(
`Unexpected additional call to sendQuery for host ${host}.`
);
}
};
setAPIModule(provider, {
prepare: prepareQuery,
send: sendQuery,
});
// Load icons
let callbackCalled = false;
loadIcons(
[
provider + ':' + prefix + ':icon1',
provider + ':' + prefix + ':icon2',
],
(loaded, missing, pending) => {
// Callback should be called only once
expect(callbackCalled).toBe(false);
callbackCalled = true;
// Test data
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([]);
expect(pending).toEqual([]);
// Send another query on next tick
setTimeout(() => {
let callbackCalled = false;
loadIcons(
[
provider + ':' + prefix + ':icon3',
provider + ':' + prefix + ':icon4',
],
(loaded, missing, pending) => {
// Callback should be called only once
expect(callbackCalled).toBe(false);
callbackCalled = true;
// Test data
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon3',
},
{
provider,
prefix,
name: 'icon4',
},
]);
expect(missing).toEqual([]);
expect(pending).toEqual([]);
done();
}
);
});
}
);
});
it('Fail on default host, multiple queries with different prefixes', (done) => {
const provider = nextPrefix();
const prefix = nextPrefix();
const prefix2 = nextPrefix();
// Set config
addAPIProvider(provider, {
resources: ['https://api1.local', 'https://api2.local'],
rotate: 100, // 100ms to speed up test
});
// Icon loader
const prepareQuery = (provider, prefix, icons) => {
const item = {
type: 'icons',
provider,
prefix,
icons,
};
return [item];
};
let queryCounter = 0;
const sendQuery = (host, params, callback) => {
queryCounter++;
expect(params.type).toBe('icons');
if (params.type !== 'icons') {
return;
}
switch (queryCounter) {
case 1:
// First call on api1
expect(params.prefix).toBe(prefix);
expect(params.icons).toEqual(['icon1', 'icon2']);
expect(host).toBe('https://api1.local');
// Do nothing - fake failed response
break;
case 2:
// First call on api2
expect(params.prefix).toBe(prefix);
expect(params.icons).toEqual(['icon1', 'icon2']);
expect(host).toBe('https://api2.local');
// Return result
callback('success', {
prefix: params.prefix,
icons: {
icon1: {
body: '<path d="" />',
},
icon2: {
body: '<path d="" />',
},
},
});
break;
case 3:
// Second call, should have api2 as default
expect(params.prefix).toBe(prefix2);
expect(params.icons).toEqual(['icon2', 'icon4']);
expect(host).toBe('https://api2.local');
// Return result
callback('success', {
prefix: params.prefix,
icons: {
icon2: {
body: '<path d="" />',
},
icon4: {
body: '<path d="" />',
},
},
});
break;
default:
done(
`Unexpected additional call to sendQuery for host ${host}.`
);
}
};
setAPIModule(provider, {
prepare: prepareQuery,
send: sendQuery,
});
// Load icons
let callbackCalled = false;
loadIcons(
[
provider + ':' + prefix + ':icon1',
provider + ':' + prefix + ':icon2',
],
(loaded, missing, pending) => {
// Callback should be called only once
expect(callbackCalled).toBe(false);
callbackCalled = true;
// Test data
expect(loaded).toEqual([
{
provider,
prefix,
name: 'icon1',
},
{
provider,
prefix,
name: 'icon2',
},
]);
expect(missing).toEqual([]);
expect(pending).toEqual([]);
// Send another query on next tick for different prefix that shares configuration
setTimeout(() => {
let callbackCalled = false;
loadIcons(
[
provider + ':' + prefix2 + ':icon2',
provider + ':' + prefix2 + ':icon4',
],
(loaded, missing, pending) => {
// Callback should be called only once
expect(callbackCalled).toBe(false);
callbackCalled = true;
// Test data
expect(loaded).toEqual([
{
provider,
prefix: prefix2,
name: 'icon2',
},
{
provider,
prefix: prefix2,
name: 'icon4',
},
]);
expect(missing).toEqual([]);
expect(pending).toEqual([]);
done();
}
);
});
}
);
});
});

View File

@ -1,32 +0,0 @@
import { mergeParams } from '@iconify/core/lib/api/params';
describe('Testing mergeParams', () => {
it('mergeParams()', () => {
// Nothing
expect(mergeParams('/foo', {})).toBe('/foo');
// Simple variables
expect(
mergeParams('/foo', {
foo: 1,
bar: 'baz',
baz: true,
})
).toBe('/foo?foo=1&bar=baz&baz=true');
// More parameters to existing query
expect(
mergeParams('/foo?bar=baz', {
foo: false,
})
).toBe('/foo?bar=baz&foo=false');
// Escaping characters
expect(
mergeParams('/foo', {
'2&2': '1=1',
'3 z': '?3',
})
).toBe('/foo?2%262=1%3D1&3%20z=%3F3');
});
});

View File

@ -1,165 +0,0 @@
import {
mockAPIModule,
mockAPIData,
iconsStorage,
} from '@iconify/core/lib/api/modules/mock';
describe('Testing mock API module prepare function', () => {
let prefixCounter = 0;
function nextPrefix() {
prefixCounter++;
return (
'api-mock-prepare-' +
(prefixCounter < 10 ? '0' : '') +
prefixCounter
);
}
const prepare = mockAPIModule.prepare;
it('Setting data for all icons', () => {
const provider = nextPrefix();
const prefix = nextPrefix();
const item = {
type: 'icons',
provider,
prefix,
response: 404,
};
mockAPIData(item);
// Make sure item is stored correctly
expect(typeof iconsStorage[provider]).toBe('object');
expect(iconsStorage[provider][prefix]).toEqual([item]);
// Find item for icons
const result = prepare(provider, prefix, ['foo', 'bar', 'baz']);
expect(result).toEqual([
{
type: 'icons',
provider,
prefix,
icons: ['foo', 'bar', 'baz'],
index: 0,
},
]);
});
it('Setting multiple entries', () => {
const provider = nextPrefix();
const prefix = nextPrefix();
const item1 = {
type: 'icons',
provider,
prefix,
response: 404,
icons: ['foo', 'bar'],
};
const item2 = {
type: 'icons',
provider,
prefix,
response: 404,
icons: 'baz',
};
const item3 = {
type: 'icons',
provider,
prefix,
response: {
prefix,
icons: {
test10: {
body: '<g />',
},
},
},
};
mockAPIData(item1);
mockAPIData(item2);
mockAPIData(item3);
// Make sure item is stored correctly
expect(typeof iconsStorage[provider]).toBe('object');
expect(iconsStorage[provider][prefix]).toEqual([item1, item2, item3]);
// Find items for icons
const result = prepare(provider, prefix, [
'foo',
'baz',
'bar',
'test1',
'test10',
'test2',
]);
expect(result).toEqual([
// Unknown icons first
{
type: 'icons',
provider,
prefix,
icons: ['test1', 'test2'],
},
{
type: 'icons',
provider,
prefix,
icons: ['foo', 'bar'],
index: 0,
},
{
type: 'icons',
provider,
prefix,
icons: ['baz'],
index: 1,
},
{
type: 'icons',
provider,
prefix,
icons: ['test10'],
index: 2,
},
]);
});
it('Without catch-all query', () => {
const provider = nextPrefix();
const prefix = nextPrefix();
const item = {
type: 'icons',
provider,
prefix,
response: 404,
icons: ['foo'],
};
mockAPIData(item);
// Make sure item is stored correctly
expect(typeof iconsStorage[provider]).toBe('object');
expect(iconsStorage[provider][prefix]).toEqual([item]);
// Find item for icons
const result = prepare(provider, prefix, ['foo', 'bar', 'baz']);
expect(result).toEqual([
// Missing icons first
{
type: 'icons',
provider,
prefix,
icons: ['bar', 'baz'],
},
{
type: 'icons',
provider,
prefix,
icons: ['foo'],
index: 0,
},
]);
});
});

View File

@ -1,354 +0,0 @@
import { addAPIProvider } from '@iconify/core/lib/api/config';
import { setAPIModule } from '@iconify/core/lib/api/modules';
import { loadIcons } from '@iconify/core/lib/api/icons';
import { mockAPIModule, mockAPIData } from '@iconify/core/lib/api/modules/mock';
import { getStorage, iconExists } from '@iconify/core/lib/storage/storage';
import { sendAPIQuery } from '@iconify/core/lib/api/query';
describe('Testing mock API module', () => {
let prefixCounter = 0;
function nextPrefix() {
prefixCounter++;
return 'api-mock-' + (prefixCounter < 10 ? '0' : '') + prefixCounter;
}
// Set API module for provider
const provider = nextPrefix();
beforeEach(() => {
addAPIProvider(provider, {
resources: ['https://api1.local'],
});
setAPIModule(provider, mockAPIModule);
});
// Tests
it('404 response', (done) => {
const prefix = nextPrefix();
mockAPIData({
type: 'icons',
provider,
prefix,
icons: ['test1', 'test2'],
response: 404,
});
let isSync = true;
loadIcons(
[
{
provider,
prefix,
name: 'test1',
},
],
(loaded, missing, pending) => {
expect(isSync).toBe(false);
expect(loaded).toEqual([]);
expect(pending).toEqual([]);
expect(missing).toEqual([
{
provider,
prefix,
name: 'test1',
},
]);
done();
}
);
isSync = false;
});
it('Load few icons', (done) => {
const prefix = nextPrefix();
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
prefix,
icons: {
test10: {
body: '<g />',
},
test11: {
body: '<g />',
},
},
},
});
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
prefix,
icons: {
test20: {
body: '<g />',
},
test21: {
body: '<g />',
},
},
},
});
let isSync = true;
loadIcons(
[
{
provider,
prefix,
name: 'test10',
},
{
provider,
prefix,
name: 'test20',
},
],
(loaded, missing, pending) => {
expect(isSync).toBe(false);
// All icons should have been loaded because API waits one tick before sending response, during which both queries are processed
expect(loaded).toEqual([
{
provider,
prefix,
name: 'test10',
},
{
provider,
prefix,
name: 'test20',
},
]);
expect(pending).toEqual([]);
expect(missing).toEqual([]);
done();
}
);
isSync = false;
});
it('Load in batches and testing delay', (done) => {
const prefix = nextPrefix();
let next;
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
prefix,
icons: {
test10: {
body: '<g />',
},
test11: {
body: '<g />',
},
},
},
});
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
prefix,
icons: {
test20: {
body: '<g />',
},
test21: {
body: '<g />',
},
},
},
delay: (callback) => {
next = callback;
},
});
let callbackCounter = 0;
loadIcons(
[
{
provider,
prefix,
name: 'test10',
},
{
provider,
prefix,
name: 'test20',
},
],
(loaded, missing, pending) => {
callbackCounter++;
switch (callbackCounter) {
case 1:
// First load: only 'test10'
expect(loaded).toEqual([
{
provider,
prefix,
name: 'test10',
},
]);
expect(pending).toEqual([
{
provider,
prefix,
name: 'test20',
},
]);
// Send second response
expect(typeof next).toBe('function');
next();
break;
case 2:
// All icons should have been loaded
expect(loaded).toEqual([
{
provider,
prefix,
name: 'test10',
},
{
provider,
prefix,
name: 'test20',
},
]);
expect(missing).toEqual([]);
done();
break;
default:
done('Callback was called more times than expected');
}
}
);
});
// This is useful for testing component where loadIcons() cannot be accessed
it('Using timer in callback for second test', (done) => {
const prefix = nextPrefix();
const name = 'test1';
// Mock data
mockAPIData({
type: 'icons',
provider,
prefix,
response: {
prefix,
icons: {
[name]: {
body: '<g />',
},
},
},
delay: (next) => {
// Icon should not be loaded yet
const storage = getStorage(provider, prefix);
expect(iconExists(storage, name)).toBe(false);
// Set data
next();
// Icon should be loaded now
expect(iconExists(storage, name)).toBe(true);
done();
},
});
// Load icons
loadIcons([
{
provider,
prefix,
name,
},
]);
});
it('Custom query', (done) => {
mockAPIData({
type: 'custom',
provider,
uri: '/test',
response: {
foo: true,
},
});
let isSync = true;
sendAPIQuery(
provider,
{
type: 'custom',
provider,
uri: '/test',
},
(data, error) => {
expect(error).toBeUndefined();
expect(data).toEqual({
foo: true,
});
expect(isSync).toBe(false);
done();
}
);
isSync = false;
});
it('Custom query with host', (done) => {
const host = 'http://' + nextPrefix();
setAPIModule(host, mockAPIModule);
mockAPIData({
type: 'host',
host,
uri: '/test',
response: {
foo: 2,
},
});
let isSync = true;
sendAPIQuery(
{
resources: [host],
},
{
type: 'custom',
uri: '/test',
},
(data, error) => {
expect(error).toBeUndefined();
expect(data).toEqual({
foo: 2,
});
expect(isSync).toBe(false);
done();
}
);
isSync = false;
});
});

View File

@ -1,60 +0,0 @@
import { addAPIProvider, getAPIConfig } from '@iconify/core/lib/api/config';
import { setAPIModule, getAPIModule } from '@iconify/core/lib/api/modules';
describe('Testing API modules', () => {
let prefixCounter = 0;
function nextPrefix() {
prefixCounter++;
return (
'api-mod-test-' + (prefixCounter < 10 ? '0' : '') + prefixCounter
);
}
const prepareQuery = (provider, prefix, icons) => {
const item = {
type: 'icons',
provider,
prefix,
icons,
};
return [item];
};
const sendQuery = () => {
throw new Error('Unexpected API call');
};
it('Empty module', () => {
const provider = nextPrefix();
// Set config
addAPIProvider(provider, {
resources: ['https://localhost:3000'],
maxURL: 500,
});
// Set fake module
setAPIModule(provider, {
prepare: prepareQuery,
send: sendQuery,
});
// Get config
const config = getAPIConfig(provider);
expect(config).not.toBeUndefined();
// Check setAPIConfig
expect(config.resources).toEqual(['https://localhost:3000']);
// Check getAPIModule()
const item = getAPIModule(provider);
expect(item).not.toBeUndefined();
expect(item.prepare).toBe(prepareQuery);
expect(item.send).toBe(sendQuery);
// Get module for different provider to make sure it is different
const provider2 = nextPrefix();
const item2 = getAPIModule(provider2);
expect(item2).not.toBe(item);
});
});

View File

@ -1,85 +0,0 @@
import { setAPIModule } from '@iconify/core/lib/api/modules';
import { loadIcons } from '@iconify/core/lib/api/icons';
import { mockAPIModule, mockAPIData } from '@iconify/core/lib/api/modules/mock';
import { allowSimpleNames } from '@iconify/core/lib/storage/functions';
describe('Testing simple names with API module', () => {
// Set API config and allow simple names
beforeEach(() => {
allowSimpleNames(true);
setAPIModule('', mockAPIModule);
});
afterAll(() => {
allowSimpleNames(false);
});
it('Loading icons without prefix', (done) => {
mockAPIData({
type: 'icons',
provider: '',
prefix: '',
response: {
prefix: '',
icons: {
test100: {
body: '<g />',
},
test101: {
body: '<g />',
},
},
},
});
mockAPIData({
type: 'icons',
provider: '',
prefix: 'test200',
response: {
prefix: 'test200',
icons: {
foo: {
body: '<g />',
},
bar: {
body: '<g />',
},
},
},
});
loadIcons(
[
{
provider: '',
prefix: '',
name: 'test100',
},
{
provider: '',
prefix: 'test200',
name: 'foo',
},
],
(loaded, missing, pending) => {
// 'test100' should be missing because it does not have a prefix
expect(loaded).toEqual([
{
provider: '',
prefix: 'test200',
name: 'foo',
},
]);
expect(pending).toEqual([]);
expect(missing).toEqual([
{
provider: '',
prefix: '',
name: 'test100',
},
]);
done();
}
);
});
});

View File

@ -1,219 +0,0 @@
import { count, config, loadCache } from '@iconify/core/lib/browser-storage';
import {
nextPrefix,
createCache,
reset,
cachePrefix,
cacheVersion,
versionKey,
countKey,
} from './fakeCache.mjs';
describe('Testing mocked localStorage', () => {
const provider = '';
it('No usable cache', () => {
reset({});
// Config before tests
expect(config).toEqual({
local: true,
session: true,
});
expect(count).toEqual({
local: 0,
session: 0,
});
// Attempt to load
loadCache();
// Everything should be disabled
expect(config).toEqual({
local: false,
session: false,
});
// Nothing should have loaded
expect(count).toEqual({
local: 0,
session: 0,
});
});
it('Empty localStorage', () => {
reset({
localStorage: createCache(),
});
// Config before tests
expect(config).toEqual({
local: true,
session: true,
});
expect(count).toEqual({
local: 0,
session: 0,
});
// Attempt to load
loadCache();
// sessionStorage should be disabled
expect(config).toEqual({
local: true,
session: false,
});
// Nothing should have loaded
expect(count).toEqual({
local: 0,
session: 0,
});
});
it('Restricted localStorage', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one item
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
cache.setItem(
cachePrefix + '0',
JSON.stringify({
cached: Date.now(),
provider,
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
})
);
// Prevent reading and writing
cache.canRead = false;
cache.canWrite = false;
// Set cache and test it
reset({
localStorage: cache,
sessionStorage: cache,
});
// Config before tests
expect(config).toEqual({
local: true,
session: true,
});
expect(count).toEqual({
local: 0,
session: 0,
});
// Attempt to load
loadCache();
// Everything should be disabled because read-only mock throws errors
expect(config).toEqual({
local: false,
session: false,
});
// Nothing should have loaded
expect(count).toEqual({
local: 0,
session: 0,
});
});
it('localStorage with one item', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
cache.setItem(
cachePrefix + '0',
JSON.stringify({
cached: Date.now(),
provider,
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
})
);
// Set cache and test it
reset({
localStorage: cache,
});
// Config before tests
expect(config).toEqual({
local: true,
session: true,
});
expect(count).toEqual({
local: 0,
session: 0,
});
// Attempt to load
loadCache();
// sessionStorage should be disabled
expect(config).toEqual({
local: true,
session: false,
});
// One item should be in localStorage
expect(count).toEqual({
local: 1,
session: 0,
});
});
it('localStorage and sessionStorage', () => {
reset({
localStorage: createCache(),
sessionStorage: createCache(),
});
// Config before tests
expect(config).toEqual({
local: true,
session: true,
});
expect(count).toEqual({
local: 0,
session: 0,
});
// Attempt to load
loadCache();
// Everything should be working
expect(config).toEqual({
local: true,
session: true,
});
// Empty storage
expect(count).toEqual({
local: 0,
session: 0,
});
});
});

View File

@ -1,119 +0,0 @@
import {
mock,
count,
config,
emptyList,
} from '@iconify/core/lib/browser-storage';
/**
* Get next icon set prefix for testing
*/
let prefixCounter = 0;
export function nextPrefix() {
return 'fake-storage-' + prefixCounter++;
}
// Cache version. Bump when structure changes
export const cacheVersion = 'iconify2';
// Cache keys
export const cachePrefix = 'iconify';
export const countKey = cachePrefix + '-count';
export const versionKey = cachePrefix + '-version';
/**
* Cache expiration
*/
export const hour = 3600000;
export const cacheExpiration = 168; // In hours
/**
* Storage class
*/
export class Storage {
canRead = true;
canWrite = true;
items = Object.create(null);
/**
* Get number of items
*/
get length() {
if (!this.canRead) {
throw new Error('Restricted storage');
}
return Object.keys(this.items).length;
}
/**
* Get item
*
* @param name
*/
getItem(name) {
if (!this.canRead) {
throw new Error('Restricted storage');
}
return this.items[name] === void 0 ? null : this.items[name];
}
/**
* Set item
*
* @param name
* @param value
*/
setItem(name, value) {
if (!this.canWrite) {
throw new Error('Read-only storage');
}
this.items[name] = value;
}
/**
* Remove item
*
* @param name
*/
removeItem(name) {
if (!this.canWrite) {
throw new Error('Read-only storage');
}
delete this.items[name];
}
/**
* Clear everything
*/
clear() {
if (!this.canWrite) {
throw new Error('Read-only storage');
}
this.items = Object.create(null);
}
}
/**
* Create fake storage, assign localStorage type
*/
export function createCache() {
return new Storage();
}
/**
* Reset test
*
* @param fakeWindow
*/
export function reset(fakeWindow) {
// Replace window
mock(fakeWindow);
// Reset all data
for (const key in config) {
const attr = key;
config[attr] = true;
count[attr] = 0;
emptyList[attr] = [];
}
}

View File

@ -1,491 +0,0 @@
import {
loadCache,
count,
config,
emptyList,
} from '@iconify/core/lib/browser-storage';
import { getStorage, iconExists } from '@iconify/core/lib/storage/storage';
import {
nextPrefix,
createCache,
reset,
cachePrefix,
cacheVersion,
versionKey,
countKey,
hour,
cacheExpiration,
} from './fakeCache.mjs';
describe('Testing loading from localStorage', () => {
const provider = '';
it('Valid icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
// Load localStorage
loadCache();
// Icon should exist now
expect(iconExists(icons, 'foo')).toBe(true);
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 1,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
});
it('Different provider', () => {
const provider = nextPrefix();
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
// Check default provider
const icons2 = getStorage('', prefix);
expect(iconExists(icons2, 'foo')).toBe(false);
// Load localStorage
loadCache();
// Icon should exist now
expect(iconExists(icons, 'foo')).toBe(true);
expect(iconExists(icons2, 'foo')).toBe(false);
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 1,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
});
it('Expired icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
const item = {
// Expiration date
cached: Math.floor(Date.now() / hour) - cacheExpiration - 1,
provider,
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
// Load localStorage
loadCache();
// Icon should not have loaded
expect(iconExists(icons, 'foo')).toBe(false);
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 0,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
});
it('Bad icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '1');
cache.setItem(
cachePrefix + '0',
JSON.stringify({
cached: Math.floor(Date.now() / hour),
provider,
data: {
prefix: prefix,
icons: {
foo: {
// Missing 'body' property
width: 20,
},
},
},
})
);
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
// Load localStorage
loadCache();
// Icon should not have loaded
expect(iconExists(icons, 'foo')).toBe(false);
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 0,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
});
it('Wrong counter', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '0'); // Should be at least "1"
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
// Load localStorage
loadCache();
// Icon should not have loaded
expect(iconExists(icons, 'foo')).toBe(false);
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 0,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
});
it('Missing entries at the end', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '5');
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '0', JSON.stringify(item));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
// Load localStorage
loadCache();
// Icon should exist now
expect(iconExists(icons, 'foo')).toBe(true);
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 1,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
});
it('Missing entries', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add two icon sets
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '5');
// Missing: 0, 2, 3
const item1 = {
cached: Math.floor(Date.now() / hour),
provider,
data: {
prefix: prefix,
icons: {
foo1: {
body: '<g></g>',
},
},
},
};
const item4 = {
cached: Math.floor(Date.now() / hour),
provider,
data: {
prefix: prefix,
icons: {
foo4: {
body: '<g></g>',
},
},
},
};
cache.setItem(cachePrefix + '1', JSON.stringify(item1));
cache.setItem(cachePrefix + '4', JSON.stringify(item4));
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo1')).toBe(false);
expect(iconExists(icons, 'foo4')).toBe(false);
// Load localStorage
loadCache();
// Icons should exist now
expect(iconExists(icons, 'foo1')).toBe(true);
expect(iconExists(icons, 'foo4')).toBe(true);
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 5,
session: 0,
});
expect(emptyList).toEqual({
local: [3, 2, 0], // reserse order
session: [],
});
});
it('Using both storage options', () => {
const prefix = nextPrefix();
const cache1 = createCache();
const cache2 = createCache();
// Add few icon sets
cache1.setItem(versionKey, cacheVersion);
cache2.setItem(versionKey, cacheVersion);
cache1.setItem(countKey, '6');
cache2.setItem(countKey, '3');
// Create 5 items
const icons = [];
const items = [];
for (let i = 0; i < 6; i++) {
const icon = {
prefix: prefix,
icons: {
['foo' + i]: {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
icons.push(icon);
items.push(item);
}
// Add items 1,3,5 to localStorage
[1, 3, 5].forEach((index) => {
cache1.setItem(cachePrefix + index, JSON.stringify(items[index]));
});
// Add items 0 and 2 to sessionStorage
[0, 2].forEach((index) => {
cache2.setItem(cachePrefix + index, JSON.stringify(items[index]));
});
// Set cache
reset({
localStorage: cache1,
sessionStorage: cache2,
});
// Check icon storage
const iconsStorage = getStorage(provider, prefix);
for (let i = 0; i < 6; i++) {
expect(iconExists(iconsStorage, 'foo' + i)).toBe(false);
}
// Load localStorage
loadCache();
// Icons should exist now, except for number 4
for (let i = 0; i < 6; i++) {
expect(iconExists(iconsStorage, 'foo' + i)).toBe(i !== 4);
}
// Check data
expect(config).toEqual({
local: true,
session: true,
});
expect(count).toEqual({
local: 6,
session: 3,
});
expect(emptyList).toEqual({
local: [4, 2, 0],
session: [1],
});
});
});

View File

@ -1,660 +0,0 @@
import {
loadCache,
storeCache,
count,
config,
emptyList,
} from '@iconify/core/lib/browser-storage';
import { getStorage, iconExists } from '@iconify/core/lib/storage/storage';
import {
nextPrefix,
createCache,
reset,
cachePrefix,
cacheVersion,
versionKey,
countKey,
hour,
cacheExpiration,
} from './fakeCache.mjs';
describe('Testing saving to localStorage', () => {
const provider = '';
it('One icon set', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add one icon set
const icon = {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo')).toBe(false);
// Save item
storeCache(provider, icon);
// Storing in cache should not add item to storage
expect(iconExists(icons, 'foo')).toBe(false);
// Check data that should have been updated because storeCache()
// should call load function before first execution
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 1,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Check cache
expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item));
expect(cache.getItem(countKey)).toBe('1');
expect(cache.getItem(versionKey)).toBe(cacheVersion);
});
it('Multiple icon sets', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add icon sets
const icon0 = {
prefix: prefix,
icons: {
foo0: {
body: '<g></g>',
},
},
};
const item0 = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon0,
};
const icon1 = {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
};
const item1 = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon1,
};
// Set cache
reset({
localStorage: cache,
});
// Save items
storeCache(provider, icon0);
storeCache(provider, icon1);
// Check data that should have been updated because storeCache()
// should call load function before first execution
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 2,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Check cache
expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item0));
expect(cache.getItem(cachePrefix + '1')).toBe(JSON.stringify(item1));
expect(cache.getItem(countKey)).toBe('2');
expect(cache.getItem(versionKey)).toBe(cacheVersion);
});
it('Adding icon set on unused spot', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add icon sets
const icon0 = {
prefix: prefix,
icons: {
foo0: {
body: '<g></g>',
},
},
};
const item0 = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon0,
};
const icon1 = {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
};
const item1 = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon1,
};
// Add item
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '2');
cache.setItem(cachePrefix + '1', JSON.stringify(item1));
// Set cache
reset({
localStorage: cache,
});
// Load data
loadCache();
// Check data
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 2,
session: 0,
});
expect(emptyList).toEqual({
local: [0],
session: [],
});
// Save items
storeCache(provider, icon0);
// Check data
expect(count).toEqual({
local: 2,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Check cache
expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item0));
expect(cache.getItem(cachePrefix + '1')).toBe(JSON.stringify(item1));
expect(cache.getItem(countKey)).toBe('2');
expect(cache.getItem(versionKey)).toBe(cacheVersion);
});
it('Adding multiple icon sets to existing data', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add icon sets
const icons = [];
const items = [];
for (let i = 0; i < 12; i++) {
const icon = {
prefix: prefix,
icons: {
['foo' + i]: {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
// Make items 2 and 4 expire
if (i === 2 || i === 4) {
item.cached -= cacheExpiration + 1;
}
// Change expiration for items 6 and 8 to almost expire
if (i === 6 || i === 8) {
item.cached -= cacheExpiration - 1;
}
icons.push(icon);
items.push(item);
// Skip items 1, 5, 9+
if (i !== 1 && i !== 5 && i < 9) {
cache.setItem(cachePrefix + i, JSON.stringify(item));
}
}
cache.setItem(versionKey, cacheVersion);
cache.setItem(countKey, '10');
// Set cache
reset({
sessionStorage: cache,
});
// Load data
loadCache();
// Check data
expect(config).toEqual({
local: false,
session: true,
});
expect(count).toEqual({
local: 0,
session: 9, // item 9 was missing
});
expect(emptyList).toEqual({
local: [],
// mix of expired and skipped items
// reverse order, 9 should not be there because it is last item
session: [5, 4, 2, 1],
});
expect(cache.getItem(countKey)).toBe('9');
// Check cached items
[0, 3, 6, 7, 8].forEach((index) => {
expect(cache.getItem(cachePrefix + index)).toBe(
JSON.stringify(items[index])
);
});
// Check expired items - should have been deleted
// Also check items that weren't supposed to be added
[2, 4, 1, 5, 9, 10, 11, 12, 13].forEach((index) => {
expect(cache.getItem(cachePrefix + index)).toBeNull();
});
// Add item 5
storeCache(provider, icons[5]);
expect(count).toEqual({
local: 0,
session: 9,
});
expect(emptyList).toEqual({
local: [],
session: [4, 2, 1],
});
expect(cache.getItem(countKey)).toBe('9');
// Add items 4, 2, 1
const list = [4, 2, 1];
list.slice(0).forEach((index) => {
expect(list.shift()).toBe(index);
storeCache(provider, icons[index]);
expect(count).toEqual({
local: 0,
session: 9,
});
expect(emptyList).toEqual({
local: [],
session: list,
});
expect(cache.getItem(countKey)).toBe('9');
});
// Add item 10
storeCache(provider, icons[10]);
expect(count).toEqual({
local: 0,
session: 10,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
expect(cache.getItem(countKey)).toBe('10');
// Add item 11
storeCache(provider, icons[11]);
expect(count).toEqual({
local: 0,
session: 11,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
expect(cache.getItem(countKey)).toBe('11');
});
it('Overwrite outdated data', () => {
const prefix = nextPrefix();
const cache = createCache();
// Add data in old format
cache.setItem(versionKey, '1.0.6');
cache.setItem(countKey, '3');
for (let i = 0; i < 3; i++) {
cache.setItem(
cachePrefix + i,
JSON.stringify({
prefix: prefix,
icons: {
['foo' + i]: {
body: '<g></g>',
},
},
})
);
}
// Set cache
reset({
localStorage: cache,
});
// Check icon storage
const icons = getStorage(provider, prefix);
expect(iconExists(icons, 'foo1')).toBe(false);
// Load cache
loadCache();
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 0,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Add one icon set
const icon = {
prefix: prefix,
icons: {
foo: {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
// Save item
storeCache(provider, icon);
// Storing in cache should not add item to storage
expect(iconExists(icons, 'foo')).toBe(false);
// Check data that should have been updated because storeCache()
// should call load function before first execution
expect(config).toEqual({
local: true,
session: false,
});
expect(count).toEqual({
local: 1,
session: 0,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Check cache
expect(cache.getItem(cachePrefix + '0')).toBe(JSON.stringify(item));
expect(cache.getItem(countKey)).toBe('1');
expect(cache.getItem(versionKey)).toBe(cacheVersion);
});
it('Using both storage options', () => {
const prefix = nextPrefix();
const cache1 = createCache();
const cache2 = createCache();
// Add icon sets to localStorage
cache1.setItem(versionKey, cacheVersion);
cache1.setItem(countKey, '3');
[0, 1, 2].forEach((index) => {
const icon = {
prefix: prefix,
icons: {
['foo' + index]: {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
cache1.setItem(cachePrefix + index, JSON.stringify(item));
});
// Add icon sets to sessionStorage
cache2.setItem(versionKey, cacheVersion);
cache2.setItem(countKey, '4');
[0, 1, 2, 3].forEach((index) => {
const icon = {
prefix: prefix,
icons: {
['bar' + index]: {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
cache2.setItem(cachePrefix + index, JSON.stringify(item));
});
// Set cache
reset({
localStorage: cache1,
sessionStorage: cache2,
});
// Load data
loadCache();
// Check data
expect(config).toEqual({
local: true,
session: true,
});
expect(count).toEqual({
local: 3,
session: 4,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Check icon storage
const iconsStorage = getStorage(provider, prefix);
for (let i = 0; i < count.local; i++) {
expect(iconExists(iconsStorage, 'foo' + i)).toBe(true);
}
for (let i = 0; i < count.session; i++) {
expect(iconExists(iconsStorage, 'bar' + i)).toBe(true);
}
// Add new item to localStorage
const icon = {
prefix: prefix,
icons: {
'new-icon': {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
storeCache(provider, icon);
// Check data
expect(count).toEqual({
local: 4, // +1
session: 4,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Check cache
expect(cache1.getItem(cachePrefix + '3')).toBe(JSON.stringify(item));
});
it('Using both storage options, but localStorage is read only', () => {
const prefix = nextPrefix();
const cache1 = createCache();
const cache2 = createCache();
// Add icon sets to localStorage
cache1.setItem(versionKey, cacheVersion);
cache1.setItem(countKey, '3');
[0, 1, 2].forEach((index) => {
const icon = {
prefix: prefix,
icons: {
['foo' + index]: {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
cache1.setItem(cachePrefix + index, JSON.stringify(item));
});
// Add icon sets to sessionStorage
cache2.setItem(versionKey, cacheVersion);
cache2.setItem(countKey, '4');
[0, 1, 2, 3].forEach((index) => {
const icon = {
prefix: prefix,
icons: {
['bar' + index]: {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
cache2.setItem(cachePrefix + index, JSON.stringify(item));
});
// Set cache
reset({
localStorage: cache1,
sessionStorage: cache2,
});
// Load data
loadCache();
// Check data
expect(config).toEqual({
local: true,
session: true,
});
expect(count).toEqual({
local: 3,
session: 4,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Check icon storage
const iconsStorage = getStorage(provider, prefix);
for (let i = 0; i < count.local; i++) {
expect(iconExists(iconsStorage, 'foo' + i)).toBe(true);
}
for (let i = 0; i < count.session; i++) {
expect(iconExists(iconsStorage, 'bar' + i)).toBe(true);
}
// Set localStorage to read-only
cache1.canWrite = false;
// Add new item to localStorage
const icon = {
prefix: prefix,
icons: {
'new-icon': {
body: '<g></g>',
},
},
};
const item = {
cached: Math.floor(Date.now() / hour),
provider,
data: icon,
};
storeCache(provider, icon);
// Check data
expect(count).toEqual({
local: 3,
session: 5,
});
expect(emptyList).toEqual({
local: [],
session: [],
});
// Check cache
expect(cache2.getItem(cachePrefix + '4')).toBe(JSON.stringify(item));
});
});

View File

@ -1,152 +0,0 @@
import { fullIcon } from '@iconify/utils/lib/icon';
import { listIcons } from '@iconify/core/lib/storage/storage';
import {
iconExists,
getIcon,
addIcon,
addCollection,
allowSimpleNames,
} from '@iconify/core/lib/storage/functions';
describe('Testing IconifyStorageFunctions', () => {
let count = 0;
function nextProvider() {
return 'storage-test-' + count++;
}
it('Storage functions', () => {
const provider = nextProvider();
const testName = `@${provider}:foo:bar`;
// Empty
expect(iconExists(testName)).toBe(false);
expect(getIcon(testName)).toBeNull();
expect(listIcons(provider)).toEqual([]);
// Add and test one icon
expect(
addIcon(testName, {
body: '<g />',
})
).toBe(true);
expect(iconExists(testName)).toBe(true);
expect(listIcons(provider)).toEqual([testName]);
});
it('Invalid icon name', () => {
const testName = 'storage' + count++;
// Reset module
allowSimpleNames(false);
// Empty
expect(iconExists(testName)).toBe(false);
expect(getIcon(testName)).toBeNull();
// Add and test one icon (icon should not be added)
expect(
addIcon(testName, {
body: '<g />',
})
).toBe(false);
expect(iconExists(testName)).toBe(false);
});
it('Invalid icon set', () => {
// Reset module
allowSimpleNames(false);
// Icon set without prefix (should work only when simple names are allowed, tested later in this file)
expect(
addCollection({
prefix: '',
icons: {
foo: {
body: '<g />',
},
},
})
).toBe(false);
});
it('Simple icon name', () => {
const testName = 'storage' + count++;
// Enable empty storage
allowSimpleNames(true);
// Empty
expect(iconExists(testName)).toBe(false);
expect(getIcon(testName)).toBeNull();
// Add and test one icon
expect(
addIcon(testName, {
body: '<g />',
})
).toBe(true);
expect(iconExists(testName)).toBe(true);
// Reset config after test
allowSimpleNames(false);
});
it('Collection with simple icon name', () => {
const n = count++;
const n2 = count++;
let name;
// Enable empty storage
allowSimpleNames(true);
// Add icon set
const name1 = 'test' + n;
const prefix2 = `prefixed${n}`;
const name2 = `icon${n2}`;
expect(
addCollection({
prefix: '',
icons: {
[name1]: {
body: '<g data-icon="basic-icon" />',
},
[`${prefix2}-${name2}`]: {
body: '<g data-icon="prefixed-icon" />',
},
},
})
).toBe(true);
// Test 'test'
name = name1;
expect(iconExists(name)).toBe(true);
expect(getIcon(name)).toEqual(
fullIcon({
body: '<g data-icon="basic-icon" />',
})
);
// Test prefixed icon, using ':' separator
name = `${prefix2}:${name2}`;
expect(listIcons('', prefix2)).toEqual([name]);
expect(iconExists(name)).toBe(true);
expect(getIcon(name)).toEqual(
fullIcon({
body: '<g data-icon="prefixed-icon" />',
})
);
// Test prefixed icon, using '-' separator
name = `${prefix2}-${name2}`;
expect(iconExists(name)).toBe(true);
expect(getIcon(name)).toEqual(
fullIcon({
body: '<g data-icon="prefixed-icon" />',
})
);
// Reset config after test
allowSimpleNames(false);
});
});

View File

@ -1,276 +0,0 @@
import {
newStorage,
addIconToStorage,
iconExists,
getIconFromStorage,
addIconSet,
getStorage,
listIcons,
} from '@iconify/core/lib/storage/storage';
describe('Testing storage', () => {
it('Adding icon', () => {
const storage = newStorage('', 'foo');
// Add one icon
addIconToStorage(storage, 'test', {
body: '<path d="" />',
width: 20,
height: 16,
});
addIconToStorage(storage, 'not-really-missing', {
body: '<path d="" />',
width: 24,
height: 24,
});
// Add another icon with reserved keyword as name
addIconToStorage(storage, 'constructor', {
body: '<g></g>',
width: 24,
height: 24,
rotate: 1,
});
// Mark 'not-really-missing' as missing
storage.missing['not-really-missing'] = Date.now();
// Add invalid icon
addIconToStorage(storage, 'invalid', {});
// Should not include 'invalid'
expect(Object.keys(storage.icons)).toEqual([
'test',
'not-really-missing',
'constructor',
]);
// Test iconExists
expect(iconExists(storage, 'test')).toBe(true);
expect(iconExists(storage, 'constructor')).toBe(true);
expect(iconExists(storage, 'invalid')).toBe(false);
expect(iconExists(storage, 'missing')).toBe(false);
expect(iconExists(storage, 'not-really-missing')).toBe(true);
// Test getIcon
let expected = {
body: '<path d="" />',
width: 20,
height: 16,
top: 0,
left: 0,
hFlip: false,
vFlip: false,
rotate: 0,
};
const icon = getIconFromStorage(storage, 'test');
expect(icon).toEqual(expected);
// Test icon mutation
let thrown = false;
try {
icon.width = 12;
} catch (err) {
thrown = true;
}
expect(thrown).toBe(true);
expected = {
body: '<g></g>',
width: 24,
height: 24,
top: 0,
left: 0,
hFlip: false,
vFlip: false,
rotate: 1,
};
expect(getIconFromStorage(storage, 'constructor')).toEqual(expected);
expect(getIconFromStorage(storage, 'invalid')).toBeNull();
expect(getIconFromStorage(storage, 'missing')).toBeNull();
});
it('Adding simple icon set', () => {
const storage = newStorage('', 'foo');
// Add two icons
expect(
addIconSet(storage, {
prefix: 'foo',
icons: {
icon1: {
body: '<path d="icon1" />',
width: 20,
},
icon2: {
body: '<path d="icon2" />',
width: 24,
},
},
height: 24,
})
).toEqual(['icon1', 'icon2']);
expect(Object.keys(storage.icons)).toEqual(['icon1', 'icon2']);
// Test iconExists
expect(iconExists(storage, 'icon1')).toBe(true);
expect(iconExists(storage, 'icon2')).toBe(true);
expect(iconExists(storage, 'invalid')).toBe(false);
expect(iconExists(storage, 'missing')).toBe(false);
// Test getIcon
let expected = {
body: '<path d="icon1" />',
width: 20,
height: 24,
top: 0,
left: 0,
hFlip: false,
vFlip: false,
rotate: 0,
};
expect(getIconFromStorage(storage, 'icon1')).toEqual(expected);
expected = {
body: '<path d="icon2" />',
width: 24,
height: 24,
top: 0,
left: 0,
hFlip: false,
vFlip: false,
rotate: 0,
};
expect(getIconFromStorage(storage, 'icon2')).toEqual(expected);
expect(getIconFromStorage(storage, 'invalid')).toBeNull();
expect(getIconFromStorage(storage, 'missing')).toBeNull();
});
it('Icon set with aliases that use transformations', () => {
const storage = newStorage('iconify', 'arty-animated');
const iconBody =
'<g stroke="currentColor" stroke-width="16" stroke-linecap="round" stroke-linejoin="round" fill="none" fill-rule="evenodd"><path d="M40 64l48-48" class="animation-delay-0 animation-duration-10 animate-stroke stroke-length-102"/><path d="M40 64l48 48" class="animation-delay-0 animation-duration-10 animate-stroke stroke-length-102"/></g>';
expect(
addIconSet(storage, {
prefix: 'arty-animated',
icons: {
'16-chevron-left': {
body: iconBody,
},
},
aliases: {
'16-chevron-right': {
parent: '16-chevron-left',
hFlip: true,
},
},
width: 128,
height: 128,
})
).toEqual(['16-chevron-left', '16-chevron-right']);
expect(Object.keys(storage.icons)).toEqual([
'16-chevron-left',
'16-chevron-right',
]);
// Test icon
let expected = {
body: iconBody,
width: 128,
height: 128,
top: 0,
left: 0,
hFlip: false,
vFlip: false,
rotate: 0,
};
expect(getIconFromStorage(storage, '16-chevron-left')).toEqual(
expected
);
// Test alias
expected = {
body: iconBody,
width: 128,
height: 128,
top: 0,
left: 0,
hFlip: true,
vFlip: false,
rotate: 0,
};
expect(getIconFromStorage(storage, '16-chevron-right')).toEqual(
expected
);
});
it('List icons in a global storage', () => {
const provider = 'test-provider';
const prefix = 'global-storage-test';
const storage1 = getStorage('', prefix);
const storage2 = getStorage(provider, prefix);
// List icons
expect(listIcons('', prefix)).toEqual([]);
expect(listIcons(provider, prefix)).toEqual([]);
// Add one icon without provider
addIconToStorage(storage1, 'test', {
body: '<path d="" />',
width: 20,
height: 16,
});
// List icons
expect(listIcons('', prefix)).toEqual([prefix + ':test']);
expect(listIcons(provider, prefix)).toEqual([]);
// Add icon set without provider
expect(
addIconSet(storage1, {
prefix,
icons: {
'16-chevron-left': {
body: '<path d="" />',
},
},
aliases: {
'16-chevron-right': {
parent: '16-chevron-left',
hFlip: true,
},
},
width: 128,
height: 128,
})
).toEqual(['16-chevron-left', '16-chevron-right']);
// List icons
expect(listIcons('', prefix)).toEqual([
prefix + ':test',
prefix + ':16-chevron-left',
prefix + ':16-chevron-right',
]);
expect(listIcons(provider, prefix)).toEqual([]);
// Add one icon with provider
addIconToStorage(storage2, 'test2', {
body: '<path d="" />',
width: 20,
height: 16,
});
// List icons
expect(listIcons('', prefix)).toEqual([
prefix + ':test',
prefix + ':16-chevron-left',
prefix + ':16-chevron-right',
]);
expect(listIcons(provider, prefix)).toEqual([
'@' + provider + ':' + prefix + ':test2',
]);
});
});

View File

@ -1,7 +0,0 @@
{
"spec_dir": "spec",
"spec_files": ["**/*[sS]pec.?(m)js"],
"helpers": ["helpers/**/*.?(m)js"],
"stopSpecOnExpectationFailure": false,
"random": true
}