add unit tests

This commit is contained in:
Adam Bradley
2018-05-16 10:53:38 -05:00
parent 778b255877
commit f1671ec6d8
3 changed files with 3403 additions and 2 deletions
+3227
View File
File diff suppressed because it is too large Load Diff
+19 -2
View File
@@ -20,7 +20,9 @@
"dev": "npm run build && sd concurrent \"stencil build --dev --config --es5 src/docs/stencil.config.js --watch\" \"stencil-dev-server\" ",
"mode.comparison": "node scripts/mode-comparison.js",
"generate": "python scripts/generate.py",
"release": "npm run build && np --any-branch"
"release": "npm run build && np --any-branch",
"test": "jest",
"test.watch": "jest --watch"
},
"devDependencies": {
"@stencil/core": "^0.9.0-0",
@@ -28,9 +30,11 @@
"@stencil/router": "^0.1.6",
"@stencil/sass": "^0.0.5",
"@stencil/utils": "latest",
"@types/jest": "^22.2.3",
"archiver": "^2.1.1",
"fs-extra": "4.0.2",
"highlight.js": "^9.12.0",
"jest": "^22.4.3",
"node-sass": "4.7.2",
"np": "2.17.0",
"svgo": "1.0.3"
@@ -64,5 +68,18 @@
"bugs": {
"url": "https://github.com/ionic-team/ionicons/issues"
},
"license": "MIT"
"license": "MIT",
"jest": {
"transform": {
"^.+\\.(js|ts|tsx)$": "<rootDir>/node_modules/@stencil/core/testing/jest.preprocessor.js"
},
"testRegex": "src/.*\\.spec\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"d.ts"
]
}
}
+157
View File
@@ -0,0 +1,157 @@
import * as icon from './icon';
describe('isValid', () => {
it('invalid onload attr', () => {
const el = { nodeType: 1, nodeName: 'svg', attributes: [{
value: 'onload'
}], childNodes: [] } as any;
expect(icon.isValid(el)).toBe(false);
});
it('invalid onclick attr', () => {
const el = { nodeType: 1, nodeName: 'svg', attributes: [{
value: 'OnClIcK'
}], childNodes: [] } as any;
expect(icon.isValid(el)).toBe(false);
});
it('invalid child SCRIPT elm', () => {
const el = { nodeType: 1, nodeName: 'svg', attributes: [], childNodes: [
{ nodeType: 1, nodeName: 'SCRIPT', attributes: [], childNodes: [] }
] } as any;
expect(icon.isValid(el)).toBe(false);
});
it('invalid script elm', () => {
const el = { nodeType: 1, nodeName: 'script', attributes: [], childNodes: [] } as any;
expect(icon.isValid(el)).toBe(false);
});
it('is valid circle elm', () => {
const el = { nodeType: 1, nodeName: 'circle', attributes: [], childNodes: [] } as any;
expect(icon.isValid(el)).toBe(true);
});
it('is valid SVG elm', () => {
const el = { nodeType: 1, nodeName: 'SVG', attributes: [], childNodes: [
{ nodeType: 1, nodeName: 'line', attributes: [], childNodes: [] }
] } as any;
expect(icon.isValid(el)).toBe(true);
});
it('is valid text node', () => {
const el = { nodeType: 3, nodeName: '#text' } as any;
expect(icon.isValid(el)).toBe(true);
});
});
describe('getUrl', () => {
let i: icon.Icon;
beforeEach(() => {
i = new icon.Icon();
i.resourcesUrl = '/build/ionicons/';
});
it('use icon prop, as name', () => {
i.icon = 'some-name';
expect(i.getUrl()).toBe('/build/ionicons/svg/md-some-name.svg');
});
it('use icon prop, as url', () => {
i.icon = './some.svg';
expect(i.getUrl()).toBe('./some.svg');
});
it('use name prop, set ios mode', () => {
i.name = 'some-name';
i.mode = 'ios';
expect(i.getUrl()).toBe('/build/ionicons/svg/ios-some-name.svg');
});
it('use name prop, set md mode', () => {
i.name = 'some-name';
i.mode = 'md';
expect(i.getUrl()).toBe('/build/ionicons/svg/md-some-name.svg');
});
it('use name prop, default md mode', () => {
i.name = 'some-name';
i.icon = 'some-icon';
expect(i.getUrl()).toBe('/build/ionicons/svg/md-some-name.svg');
});
it('use src prop', () => {
i.src = './some.svg';
i.name = 'some-name';
i.icon = 'some-icon';
expect(i.getUrl()).toBe('./some.svg');
});
});
describe('getSrc', () => {
it('both . and /', () => {
expect(icon.getSrc('./somefile.svg')).toBe('./somefile.svg');
});
it('url', () => {
expect(icon.getSrc('https://ionicons/somefile.svg')).toBe('https://ionicons/somefile.svg');
});
it('just a .', () => {
expect(icon.getSrc('somefile.svg')).toBe('somefile.svg');
});
it('just a /', () => {
expect(icon.getSrc('/somesvg')).toBe('/somesvg');
});
it('no . or /', () => {
expect(icon.getSrc('some-name')).toBe(null);
});
});
describe('getName', () => {
it('not allow special chars', () => {
expect(icon.getName('some\\name', 'io', '', '')).toBe(null);
expect(icon.getName('some$name', 'io', '', '')).toBe(null);
expect(icon.getName('some:name', 'io', '', '')).toBe(null);
expect(icon.getName('some.name', 'io', '', '')).toBe(null);
expect(icon.getName('some/name', 'io', '', '')).toBe(null);
});
it('use ios mode from mode property', () => {
expect(icon.getName('some-name', 'ios', '', '')).toBe('ios-some-name');
});
it('use md mode from mode property', () => {
expect(icon.getName('some-name', 'md', '', '')).toBe('md-some-name');
});
it('use ios mode prefixed', () => {
expect(icon.getName('ios-some-name', '', '', '')).toBe('ios-some-name');
});
it('use md mode prefixed', () => {
expect(icon.getName('md-some-name', '', '', '')).toBe('md-some-name');
});
it('default md name w/out mode, ios or md', () => {
expect(icon.getName('some-name', '', '', '')).toBe('md-some-name');
});
it('should not use name if no name, ios or md', () => {
expect(icon.getName('', '', '', '')).toBe(null);
});
});