Skip to content

Commit

Permalink
test: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Aug 30, 2020
1 parent 898c7bf commit a33d507
Showing 1 changed file with 17 additions and 32 deletions.
49 changes: 17 additions & 32 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

const fs = require('fs')
const path = require('path')
const should = require('should')
const expect = require('chai').expect
const Vinyl = require('vinyl')
const inlineSource = require('..')

const getFile = (filePath, contents) => {
filePath = filePath.replace(/\\/g, '/')
if (!path.extname(filePath)) filePath += '.html'
return new Vinyl({
base: path.dirname(filePath),
path: filePath,
Expand All @@ -23,66 +25,49 @@ const getExpected = filePath => {
return getFile(path.join(__dirname, 'expected', filePath))
}

const compare = (stream, fixtureName, expectedName, done) => {
stream.on('error', err => {
should.exist(err)
done(err)
})

const compare = (done, fixtureName, expectedName, options = {}) => {
const stream = inlineSource(options)
stream.on('error', err => done(err))
stream.on('data', file => {
should.exist(file)
should.exist(file.contents)

should.equal(
String(file.contents),
expect(String(file.contents)).to.equal(
String(getExpected(expectedName).contents)
)
done()
})

stream.write(getFixture(fixtureName))
stream.end()
}

describe('gulp-inline-source', () => {
it('Should inline <script> tag', done => {
compare(inlineSource(), 'script.html', 'inlined-script.html', done)
compare(done, 'script', 'inlined-script')
})

it('Should inline <script> tag with ES6 source', done => {
compare(inlineSource(), 'script-es6.html', 'inlined-script.html', done)
compare(done, 'script-es6', 'inlined-script')
})

it('Should inline <link> tag', done => {
compare(inlineSource(), 'link.html', 'inlined-link.html', done)
compare(done, 'link', 'inlined-link')
})

it('Should inline <img> tag with SVG source', done => {
compare(inlineSource(), 'image-svg.html', 'inlined-image-svg.html', done)
compare(done, 'image-svg', 'inlined-image-svg')
})

it('Should inline <img> tag with PNG source', done => {
compare(inlineSource(), 'image-png.html', 'inlined-image-png.html', done)
compare(done, 'image-png', 'inlined-image-png')
})

it('works with type and media attributes', done => {
compare(
inlineSource(),
'with-attributes.html',
'inlined-with-attributes.html',
done
)
it('Should work with type and media attributes', done => {
compare(done, 'with-attributes', 'inlined-with-attributes')
})

it('works with relative paths', done => {
compare(inlineSource(), 'script-relative.html', 'inlined-script.html', done)
it('Should work with relative paths', done => {
compare(done, 'script-relative', 'inlined-script')
})

it('Should inline assets without minification', done => {
const stream = inlineSource({
compress: false
})

compare(stream, 'nominify.html', 'inlined-nominify.html', done)
compare(done, 'nominify', 'inlined-nominify', { compress: false })
})
})

0 comments on commit a33d507

Please sign in to comment.