Skip to content

Commit 6b00cb8

Browse files
authored
Merge pull request #14 from arniu/test
Add test
2 parents d09b362 + b0c98f8 commit 6b00cb8

File tree

10 files changed

+119
-39
lines changed

10 files changed

+119
-39
lines changed

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: node_js
2+
3+
os:
4+
- linux
5+
6+
node_js:
7+
- 'node'
8+
- '12'
9+
- '10'
10+
11+
notifications:
12+
email:
13+
on_success: never
14+
on_failure: always

babel.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current'
8+
}
9+
}
10+
]
11+
]
12+
}

generate-icon.js renamed to generate.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
const fs = require('fs')
44
const path = require('path')
55

6-
const debug = console.log
7-
86
/**
97
* @param {any} value
108
* @return {string}
@@ -31,25 +29,36 @@ function resolvePath (file) {
3129
return `${__dirname}/node_modules/ionicons/dist/${file}`
3230
}
3331

34-
;(function () {
35-
const recipes = [
32+
const FILES = (exports.FILES = {
33+
GLYPH_MAP: 'glyph/map.json',
34+
FONT_FILE: 'fonts/Ionicons.ttf'
35+
})
36+
37+
function generate () {
38+
const workflows = [
3639
{
37-
file: 'glyph/map.json',
38-
source: 'scss/ionicons-icons.scss',
40+
file: FILES.GLYPH_MAP,
41+
from: 'scss/ionicons-icons.scss',
3942
steps: [String, parseGlyphs, stringify]
4043
},
4144
{
42-
file: 'fonts/Ionicons.ttf',
43-
source: 'fonts/ionicons.ttf',
45+
file: FILES.FONT_FILE,
46+
from: 'fonts/ionicons.ttf',
4447
steps: []
4548
}
46-
].map(({ file, source, steps }) =>
49+
].map(({ file, from, steps }) =>
4750
[resolvePath, fs.readFileSync, ...steps]
48-
.reduce((prev, next) => prev.then(next), Promise.resolve(source))
49-
.then(data => fs.writeFileSync(path.resolve(__dirname, file), data))
51+
.reduce((prev, next) => prev.then(next), Promise.resolve(from))
52+
.then(it => fs.writeFileSync(path.resolve(__dirname, file), it))
53+
.then(() => console.log(` Generated ${file} ...`))
5054
)
5155

52-
Promise.all(recipes)
53-
.then(() => debug('done!'))
54-
.catch(e => debug(e.message))
55-
})()
56+
Promise.all(workflows)
57+
.then(() => console.log('All is done'))
58+
.catch(e => console.log(e.message))
59+
}
60+
61+
// only run as script
62+
if (require.main === module) {
63+
generate()
64+
}

glyph/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import glyphs from './map.json'
22

3+
export const UNKNOWN_ICON = ''
4+
35
/**
4-
* Get glyph
6+
* Try glyph
57
*
68
* @param {Array.<?string>} iconNames
7-
* @param {string} prefix
9+
* @param {function(string): string} mapper
810
* @return {string}
911
*/
10-
export default function (iconNames, prefix) {
12+
export function tryGlyph (iconNames, mapper) {
1113
const code = iconNames.reduce((prev, name) => {
12-
return prev || glyphs[name in glyphs ? name : `${prefix}-${name}`]
14+
return prev || glyphs[name in glyphs ? name : mapper(name)]
1315
}, undefined)
1416

15-
return code ? String.fromCharCode(code) : ''
17+
return code ? String.fromCharCode(code) : UNKNOWN_ICON
1618
}
19+
20+
/**
21+
* @typedef
22+
*/

glyph/map.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,4 +695,4 @@
695695
"md-wifi": 62376,
696696
"md-wine": 62377,
697697
"md-woman": 62378
698-
}
698+
}

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react'
22
import { StyleSheet, Text, Platform } from 'react-native'
3-
import tryGlyph from './glyph'
3+
import { tryGlyph } from './glyph'
44

55
/**
66
* @typedef {(string|undefined)} IconName
@@ -14,8 +14,8 @@ import tryGlyph from './glyph'
1414
* @return {string}
1515
*/
1616
const getGlyph = Platform.select({
17-
ios: (_android, ios, name) => tryGlyph([ios, name], 'ios'),
18-
default: (android, _ios, name) => tryGlyph([android, name], 'md')
17+
ios: (_android, ios, name) => tryGlyph([ios, name], x => `ios-${x}`),
18+
default: (android, _ios, name) => tryGlyph([android, name], x => `md-${x}`)
1919
})
2020

2121
class Icon extends React.PureComponent {
@@ -53,7 +53,7 @@ class Icon extends React.PureComponent {
5353
return (
5454
<Text
5555
{...textProps}
56-
style={[styles.default, fontStyle, style]}
56+
style={[fontStyle, style, styles.icon]}
5757
ref={this._setRef}
5858
>
5959
{getGlyph(android, ios, name)}
@@ -71,7 +71,7 @@ Icon.defaultProps = {
7171
export default Icon
7272

7373
const styles = StyleSheet.create({
74-
default: {
74+
icon: {
7575
fontFamily: 'Ionicons',
7676
fontWeight: 'normal',
7777
fontStyle: 'normal'

package.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@
55
"main": "index.js",
66
"typings": "index.d.ts",
77
"scripts": {
8-
"prepublishOnly": "node generate-icon",
9-
"lint": "prettier --write '**/*.{js,ts,md}'; standard --fix"
8+
"prepublishOnly": "test",
9+
"generate": "node generate",
10+
"standard": "standard --fix",
11+
"prettier": "prettier --write '**/*.{js,json,ts,md}'",
12+
"pretest": "npm run generate; npm run prettier; npm run standard",
13+
"test": "jest"
1014
},
1115
"husky": {
1216
"hooks": {
1317
"pre-commit": "lint-staged"
1418
}
1519
},
1620
"lint-staged": {
17-
"*.{ts,md}": [
21+
"*.{js,json,ts,md}": [
1822
"prettier --write",
1923
"git add"
2024
],
2125
"*.js": [
22-
"prettier --write",
2326
"standard --fix",
2427
"git add"
2528
]
@@ -29,9 +32,9 @@
2932
"singleQuote": true
3033
},
3134
"standard": {
32-
"ignore": [
33-
"*.flow.js"
34-
]
35+
"env": {
36+
"jest": true
37+
}
3538
},
3639
"repository": {
3740
"type": "git",
@@ -54,8 +57,12 @@
5457
"react-native": "*"
5558
},
5659
"devDependencies": {
60+
"@babel/core": "^7.7.7",
61+
"@babel/preset-env": "^7.7.7",
62+
"babel-jest": "^24.9.0",
5763
"husky": "^3.0.9",
5864
"ionicons": "^4.6.3",
65+
"jest": "^24.9.0",
5966
"lint-staged": "^9.4.2",
6067
"prettier": "^1.18.2",
6168
"standard": "^14.3.1"

react-native-ionicons.podspec

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
44

55
Pod::Spec.new do |s|
66

7-
s.name = package[:name]
8-
s.version = package[:version]
9-
s.summary = package[:description]
10-
s.author = package[:author]
11-
s.license = package[:license]
12-
s.homepage = package[:homepage]
7+
s.name = package["name"]
8+
s.version = package["version"]
9+
s.summary = package["description"]
10+
s.author = package["author"]
11+
s.license = package["license"]
12+
s.homepage = package["homepage"]
1313
s.platforms = { :ios => "9.0", :tvos => "9.0" }
1414
s.source = { :git => "https://github.com/arniu/react-native-ionicons.git", :tag => "v#{s.version}" }
1515
s.resources = "fonts/*.ttf"

test/generate.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fs from 'fs'
2+
import { resolve } from 'path'
3+
import { FILES } from '../generate'
4+
5+
it(`should exist: ${FILES.FONT_FILE}`, () => {
6+
const path = resolve(__dirname, '..', FILES.FONT_FILE)
7+
expect(fs.existsSync(path)).toBeTruthy()
8+
})
9+
10+
it(`should exist: ${FILES.GLYPH_MAP}`, () => {
11+
const path = resolve(__dirname, '..', FILES.GLYPH_MAP)
12+
expect(fs.existsSync(path)).toBeTruthy()
13+
})

test/glyph.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { UNKNOWN_ICON, tryGlyph } from '../glyph'
2+
3+
describe('should work on ios devices', () => {
4+
const mapper = x => `ios-${x}`
5+
6+
it('should have `add` icon', () => {
7+
const icon = tryGlyph(['add', null], mapper)
8+
expect(icon).not.toBe(UNKNOWN_ICON)
9+
})
10+
})
11+
12+
describe('should work on android devices', () => {
13+
const mapper = x => `md-${x}`
14+
15+
it('should have `add` icon', () => {
16+
const icon = tryGlyph(['add', null], mapper)
17+
expect(icon).not.toBe(UNKNOWN_ICON)
18+
})
19+
})

0 commit comments

Comments
 (0)