Skip to content

Commit

Permalink
Remove ignore tags and up to 100 coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris Daniel committed Dec 23, 2019
1 parent 587c07e commit 81d2681
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 37 deletions.
97 changes: 80 additions & 17 deletions src/__tests__/index.test.js
Expand Up @@ -79,7 +79,7 @@ beforeEach(() => {
})

describe('ChunksWebpackPlugin', () => {
it('should init the constructor function', () => {
it('Initialize the constructor', () => {
expect(chunksWebpackPlugin.options).toMatchObject({
outputPath: '/dist/templates',
fileExtension: '.html',
Expand All @@ -91,7 +91,7 @@ describe('ChunksWebpackPlugin', () => {
})
})

it('should init the constructor function without options', () => {
it('Initialize the constructor without options', () => {
const instance = new ChunksWebpackPlugin()
expect(instance.options).toMatchObject({
outputPath: 'default',
Expand All @@ -104,7 +104,7 @@ describe('ChunksWebpackPlugin', () => {
})
})

it('should init the apply function', () => {
it('Initialize the apply function', () => {
compilerWebpack.hooks.emit.tap = jest.fn()

chunksWebpackPlugin.apply(compilerWebpack)
Expand All @@ -113,15 +113,43 @@ describe('ChunksWebpackPlugin', () => {
expect(compilerWebpack.hooks.emit.tap).toHaveBeenCalled()
})

it('should init the hookEmit function', () => {
it('Initialize the hookEmit function', () => {
chunksWebpackPlugin.createHtmlChunksFiles = jest.fn()

chunksWebpackPlugin.hookEmit(compilationWebpack)

expect(chunksWebpackPlugin.createHtmlChunksFiles).toHaveBeenCalled()
})

it('should init the hookEmit function with wrong returns of customFormatTags', () => {
it('Initialize the hookEmit function without chunks', () => {
chunksWebpackPlugin.sortsChunksByType = jest.fn()

compilationWebpack.chunkGroups[0].chunks = []
chunksWebpackPlugin.hookEmit(compilationWebpack)

expect(chunksWebpackPlugin.sortsChunksByType).not.toHaveBeenCalled()
})

it('Initialize the hookEmit function without generating chunk files', () => {
chunksWebpackPlugin.createHtmlChunksFiles = jest.fn()

chunksWebpackPlugin.options.generateChunksFiles = false
chunksWebpackPlugin.hookEmit(compilationWebpack)

expect(chunksWebpackPlugin.createHtmlChunksFiles).not.toHaveBeenCalled()
})

it('Initialize the hookEmit function without generating chunk manifest', () => {
chunksWebpackPlugin.createHtmlChunksFiles = jest.fn()
chunksWebpackPlugin.updateManifest = jest.fn()

chunksWebpackPlugin.options.generateChunksManifest = false
chunksWebpackPlugin.hookEmit(compilationWebpack)

expect(chunksWebpackPlugin.updateManifest).not.toHaveBeenCalled()
})

it('Initialize the hookEmit function with wrong returns of customFormatTags', () => {
chunksWebpackPlugin.createHtmlChunksFiles = jest.fn()
utils.setError = jest.fn()

Expand All @@ -132,7 +160,7 @@ describe('ChunksWebpackPlugin', () => {
expect(chunksWebpackPlugin.createHtmlChunksFiles).toHaveBeenCalled()
})

it('should init the hookEmit function with wrong declaration of customFormatTags', () => {
it('Initialize the hookEmit function with wrong declaration of customFormatTags', () => {
chunksWebpackPlugin.createHtmlChunksFiles = jest.fn()
chunksWebpackPlugin.generateTags = jest.fn()

Expand All @@ -142,7 +170,7 @@ describe('ChunksWebpackPlugin', () => {
expect(chunksWebpackPlugin.generateTags).toHaveBeenCalled()
})

it('should init the updateManifest function', () => {
it('Initialize the updateManifest function', () => {
updateManifest()

expect(chunksWebpackPlugin.manifest).toMatchObject({
Expand All @@ -153,41 +181,48 @@ describe('ChunksWebpackPlugin', () => {
})
})

it('should init the getPublicPath function', () => {
it('Initialize the getPublicPath function', () => {
const publicPath = chunksWebpackPlugin.getPublicPath(compilationWebpack)

expect(publicPath).toBe('/dist/')
})

it('should init the getPublicPath function with empty value', () => {
it('Initialize the getPublicPath function with empty value', () => {
compilationWebpack.options.output.publicPath = false
const publicPath = chunksWebpackPlugin.getPublicPath(compilationWebpack)

expect(publicPath).toBe('')
})

it('should init the getOutputPath function', () => {
// it('Initialize the getPublicPath function with publicPath not ending with slash', () => {
// compilationWebpack.options.output.publicPath = false
// const publicPath = chunksWebpackPlugin.getPublicPath(compilationWebpack)

// expect(publicPath).toBe('')
// })

it('Initialize the getOutputPath function', () => {
const outputPath = chunksWebpackPlugin.getOutputPath(compilationWebpack)

expect(outputPath).toBe('/dist/templates')
})

it('should init the getOutputPath function with default outputPath', () => {
it('Initialize the getOutputPath function with default outputPath', () => {
chunksWebpackPlugin.options.outputPath = 'default'
const outputPath = chunksWebpackPlugin.getOutputPath(compilationWebpack)

expect(outputPath).toBe('/dist/')
})

it('should init the getOutputPath function with default outputPath and without value', () => {
it('Initialize the getOutputPath function with default outputPath and without value', () => {
compilationWebpack.options.output.path = false
chunksWebpackPlugin.options.outputPath = 'default'
const outputPath = chunksWebpackPlugin.getOutputPath(compilationWebpack)

expect(outputPath).toBe('')
})

it('should init the getOutputPath function with wrong outputPath', () => {
it('Initialize the getOutputPath function with wrong outputPath', () => {
utils.setError = jest.fn()

chunksWebpackPlugin.options.outputPath = ''
Expand All @@ -196,14 +231,27 @@ describe('ChunksWebpackPlugin', () => {
expect(utils.setError).toHaveBeenCalled()
})

it('should init the sortsChunksByType function', () => {
it('Initialize the sortsChunksByType function', () => {
expect(chunksSorted).toMatchObject({
styles: ['/dist/css/vendors~app-a~app-b.css'],
scripts: ['/dist/js/vendors~app-a~app-b.js']
})
})

it('should init the generateTags function', () => {
it('Initialize the sortsChunksByType function with scripts only', () => {
let chunksSorted = chunksWebpackPlugin.sortsChunksByType({
chunks: [{
files: [
'css/vendors~app-a~app-b.css'
]
}],
publicPath: chunksWebpackPlugin.getPublicPath(compilationWebpack)
})

expect(chunksSorted.scripts.length).toEqual(0)
})

it('Initialize the generateTags function', () => {
const tags = chunksWebpackPlugin.generateTags(chunksSorted)

expect(tags).toMatchObject({
Expand All @@ -212,7 +260,7 @@ describe('ChunksWebpackPlugin', () => {
})
})

it('should init the createHtmlChunksFiles function', () => {
it('Initialize the createHtmlChunksFiles function', () => {
utils.writeFile = jest.fn()

chunksWebpackPlugin.createHtmlChunksFiles({
Expand All @@ -227,7 +275,22 @@ describe('ChunksWebpackPlugin', () => {
expect(utils.writeFile).toHaveBeenCalled()
})

it('should init the createChunksManifestFile function', () => {
it('Initialize the createHtmlChunksFiles function without styles and scripts', () => {
utils.writeFile = jest.fn()

chunksWebpackPlugin.createHtmlChunksFiles({
entry: 'app-a',
tagsHTML: {
styles: '',
scripts: ''
},
outputPath: '/dist/'
})

expect(utils.writeFile).not.toHaveBeenCalled()
})

it('Initialize the createChunksManifestFile function', () => {
updateManifest()

chunksWebpackPlugin.createChunksManifestFile({
Expand Down
12 changes: 10 additions & 2 deletions src/__tests__/utils.test.js
Expand Up @@ -4,13 +4,21 @@ import utils from '../utils'
import fse from 'fs-extra'

describe('Utils', () => {
it('should init the setError function', () => {
it('Initialize the setError function', () => {
expect(() => {
utils.setError('message')
}).toThrow(new Error('message'))
})

it('should init the writeFile function', () => {
it('Initialize the isAbsolutePath function', () => {
expect(utils.isAbsolutePath('dist')).toBe(false)
})

it('Initialize the getFileExtension function', () => {
expect(utils.getFileExtension('css/vendors~app-a~app-b.css')).toBe('css')
})

it('Initialize the writeFile function', () => {
fse.outputFileSync = jest.fn()

utils.writeFile({
Expand Down
26 changes: 8 additions & 18 deletions src/index.js
Expand Up @@ -50,7 +50,6 @@ module.exports = class ChunksWebpackPlugin {

compilation.chunkGroups.forEach(chunkGroup => {
// Check if chunkGroup contains chunks
/* istanbul ignore else */
if (chunkGroup.chunks.length) {
const entryName = chunkGroup.options.name
let chunksSorted = this.sortsChunksByType({
Expand All @@ -59,7 +58,6 @@ module.exports = class ChunksWebpackPlugin {
})

// Check if chunks files generation is enabled
/* istanbul ignore else */
if (this.options.generateChunksFiles) {
let tagsHTML = null

Expand All @@ -85,7 +83,6 @@ module.exports = class ChunksWebpackPlugin {
}

// Check if manifest option is enabled
/* istanbul ignore else */
if (this.options.generateChunksManifest) {
this.updateManifest({
entryName: entryName,
Expand All @@ -96,7 +93,6 @@ module.exports = class ChunksWebpackPlugin {
})

// Check if manifest option is enabled
/* istanbul ignore else */
if (this.options.generateChunksManifest) {
this.createChunksManifestFile({ compilation, outputPath })
}
Expand Down Expand Up @@ -126,12 +122,8 @@ module.exports = class ChunksWebpackPlugin {
getPublicPath (compilation) {
let publicPath = compilation.options.output.publicPath || ''

/* istanbul ignore else */
if (publicPath) {
/* istanbul ignore else */
if (publicPath.substr(-1) !== '/') {
publicPath = `${publicPath}/`
}
if (publicPath && publicPath.substr(-1) !== '/') {
publicPath = `${publicPath}/`
}

return publicPath
Expand Down Expand Up @@ -175,15 +167,15 @@ module.exports = class ChunksWebpackPlugin {
'styles': [],
'scripts': []
}
let extensionKeys = {
css: 'styles',
js: 'scripts'
}

chunks.forEach(chunk => {
chunk.files.forEach(file => {
/* istanbul ignore else */
if (utils.getFileExtension(file) === 'css') {
files['styles'].push(`${publicPath}${file}`)
} else if (utils.getFileExtension(file) === 'js') {
files['scripts'].push(`${publicPath}${file}`)
}
let extension = utils.getFileExtension(file)
files[extensionKeys[extension]].push(`${publicPath}${file}`)
})
})

Expand Down Expand Up @@ -226,14 +218,12 @@ module.exports = class ChunksWebpackPlugin {
tagsHTML,
outputPath
}) {
/* istanbul ignore else */
if (tagsHTML.scripts.length) {
utils.writeFile({
outputPath: `${outputPath}/${entry}-scripts${this.options.fileExtension}`,
output: tagsHTML.scripts
})
}
/* istanbul ignore else */
if (tagsHTML.styles.length) {
utils.writeFile({
outputPath: `${outputPath}/${entry}-styles${this.options.fileExtension}`,
Expand Down

0 comments on commit 81d2681

Please sign in to comment.