Skip to content

Commit b82eb97

Browse files
author
pemrouz
committed
test: add tests
1 parent b62b01a commit b82eb97

File tree

9 files changed

+97
-2
lines changed

9 files changed

+97
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules
2+
.nyc_output
3+
coverage

export

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ var write = require('fs').writeFileSync
1212
var root = resolve(process.cwd(), process.argv[2] || 'dist/resources')
1313
, resources = beautify(str(glob(root+'/**/!(test|index).{js,css}')
1414
.map(resource)
15-
.reduce(to.obj('name'), {})))
15+
.reduce(byname, {})))
1616
.replace(/"require\((.*?)\)"/g, 'require($1)')
1717

1818
write(resolve(root, 'index.js'), 'module.exports = ' + resources)
1919

20+
function byname(p, v) {
21+
p[v[0]] = v[1]
22+
return p
23+
}
24+
2025
function resource(path) {
2126
var last = basename(path)
2227
, extn = extname(path)
@@ -41,7 +46,7 @@ function resource(path) {
4146
res = { name: name, body: body }
4247
}
4348

44-
return res
49+
return [name, res]
4550
}
4651

4752
function rel(path) {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
"type": "git",
1212
"url": "git://github.com/rijs/export.git"
1313
},
14+
"scripts": {
15+
"test": "tap ./test.js"
16+
},
1417
"license": "pemrouz.mit-license.org",
1518
"bin": "./export"
1619
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:host { display: 'flex' }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = function fn(){}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
name: 'component-object'
3+
, body: function object(){}
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
name: 'foo'
3+
, body: 'bar'
4+
}

test-cases/dist/resources/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
"component-function.css": {
3+
"name": "component-function.css",
4+
"body": ":host { display: 'flex' }"
5+
},
6+
"component-function": {
7+
"name": "component-function",
8+
"body": require('./component-function/component-function.js').default || require('./component-function/component-function.js'),
9+
"headers": {
10+
"needs": "[css]"
11+
}
12+
},
13+
"component-object": require('./component-object/component-object.js').default || require('./component-object/component-object.js'),
14+
"foo": require('./data/foo.js').default || require('./data/foo.js')
15+
}

test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require('utilise')
2+
const fs = require('fs')
3+
, t = require('tap')
4+
, spawn = require('child_process').spawnSync
5+
, resolve = require('path').resolve
6+
, base = './test-cases/dist/resources'
7+
8+
fs.existsSync(`${base}/index.js`) && fs.unlinkSync(`${base}/index.js`)
9+
spawn('sh', ['-c', `./export ${base}`], { stdio: 'inherit' })
10+
11+
const resources = require(base)
12+
13+
t.same(keys(resources), [
14+
'component-function.css'
15+
, 'component-function'
16+
, 'component-object'
17+
, 'foo'
18+
]
19+
, 'total resources'
20+
)
21+
22+
t.same(resources['component-function.css'], {
23+
name: 'component-function.css'
24+
, body: ":host { display: 'flex' }"
25+
}
26+
, 'css'
27+
)
28+
29+
t.same(resources['foo'], {
30+
name: 'foo'
31+
, body: 'bar'
32+
}
33+
, 'data')
34+
35+
t.same(resources['component-function'].name
36+
, 'component-function'
37+
, 'component-function: has name'
38+
)
39+
40+
t.same(typeof resources['component-function'].body
41+
, 'function'
42+
, 'component-function: body is function'
43+
)
44+
45+
t.same(resources['component-function'].headers
46+
, { needs: '[css]' }
47+
, 'component-function: set needs headers'
48+
)
49+
50+
t.same(resources['component-object'].name
51+
, 'component-object'
52+
, 'component-object: has name'
53+
)
54+
55+
t.same(typeof resources['component-object'].body
56+
, 'function'
57+
, 'component-object: body is function'
58+
)
59+
60+
t.end()

0 commit comments

Comments
 (0)