Added tests + fix

This commit is contained in:
Chen Asraf
2018-03-15 19:48:35 +02:00
parent ac5970a2d4
commit 88ccc8c64c
8 changed files with 4564 additions and 3 deletions

10
.babelrc Normal file
View File

@@ -0,0 +1,10 @@
{
"presets": [[
"env",
{
"targets": {
"node": "4"
}
}
]]
}

1
.gitignore vendored
View File

@@ -57,3 +57,4 @@ typings/
# dotenv environment variables file
.env
test/build

View File

@@ -34,6 +34,7 @@ module.exports = function chromeUrlLoader(contents) {
options.baseDir = options.baseDir || process.cwd()
options.baseDir = options.baseDir.endsWith('/') ? options.baseDir.slice(0, -1) : options.baseDir
options.publicDir = options.publicDir || ''
options.publicDir = options.publicDir.endsWith('/') ? options.publicDir.slice(0, -1) : options.publicDir
const relativeFilePath = this.resourcePath.replace(options.baseDir + '/', '')
@@ -43,7 +44,7 @@ module.exports = function chromeUrlLoader(contents) {
}
const fileName = path.basename(relativeFilePath)
const outputDir = path.join(this.options.context, options.publicDir)
const outputDir = path.join(this.context, options.publicDir)
const outputPath = path.join(outputDir, fileName)
const relativeDir = options.publicDir.split(path.sep).slice(1).join(path.sep)

View File

@@ -10,5 +10,18 @@
"dependencies": {
"loader-utils": "^1.1.0",
"schema-utils": "^0.4.5"
},
"scripts": {
"test": "jest"
},
"devDependencies": {
"babel-jest": "^23.0.0-alpha.0",
"babel-preset-env": "^1.6.1",
"jest": "^23.0.0-alpha.0",
"memory-fs": "^0.4.1",
"webpack": "^4.1.1"
},
"jest": {
"testEnvironment": "node"
}
}

35
test/compiler.js Normal file
View File

@@ -0,0 +1,35 @@
const path = require('path')
const webpack = require('webpack')
const memoryfs = require('memory-fs')
module.exports = (fixture, options = {}) => {
const compiler = webpack({
context: __dirname,
entry: `./${fixture}`,
output: {
path: path.resolve(__dirname),
filename: 'bundle.js',
},
module: {
rules: [{
test: /\.svg$/,
use: {
loader: path.resolve(__dirname, '../index.js'),
options: {
publicDir: 'build/static/svg'
}
}
}]
},
})
compiler.outputFileSystem = new memoryfs()
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) reject(err)
resolve(stats)
})
})
}

9
test/test.js Normal file
View File

@@ -0,0 +1,9 @@
const compiler = require('./compiler.js')
test('Inserts file path to output properly', (done) => {
compiler('test.svg').then((stats) => {
const output = stats.toJson().modules[0].source
expect(output).toBe(`module.exports = chrome.extension.getURL('static/svg/test.svg')`)
done()
})
})

1
test/test.svg Normal file
View File

@@ -0,0 +1 @@
<?xml version="1.0" standalone="no"?><!-- Generator: Gravit.io --><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="isolation:isolate" viewBox="0 0 12 4" width="12" height="4"><defs><clipPath id="_clipPath_hydSffmOoJOiUzgRtdngsyVWsB0fkp6C"><rect width="12" height="4"/></clipPath></defs><g clip-path="url(#_clipPath_hydSffmOoJOiUzgRtdngsyVWsB0fkp6C)"><g transform="matrix(1,0,0,1,0,0)"><text transform="matrix(1,0,0,1,0.129,3.207)" style="font-family:&quot;Open Sans&quot;;font-weight:400;font-size:3px;font-style:normal;fill:#000000;stroke:none;">©casraf</text></g></g></svg>

After

Width:  |  Height:  |  Size: 617 B

4495
yarn.lock

File diff suppressed because it is too large Load Diff