Skip to content

Commit

Permalink
unit tests added for testing not found errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Mar 14, 2016
1 parent 023a375 commit df9dab8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
36 changes: 34 additions & 2 deletions test/web/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,33 @@ const defaultParams = {
cacheControl: 'max-age=14400',
accessControlAllowOrigin: '*',
},
status: 200,
},
}

const tests = [
{
name: 'should return error when the only file not found',
path: '/bundle/applyq@0.2.122222(index).js',
expected: {
status: 404,
fileContent: 'Not found.',
headers: {
contentType: 'text/plain; charset=utf-8',
},
},
},
{
name: 'should return error when one of the files not found',
path: '/bundle/applyq@0.2.1(index+not_exists).js',
expected: {
status: 404,
fileContent: 'Not found.',
headers: {
contentType: 'text/plain; charset=utf-8',
},
},
},
{
name: 'should bundle one file',
path: '/bundle/applyq@0.2.1(index).js',
Expand Down Expand Up @@ -150,6 +173,8 @@ describe('bundle', function () {

tests.forEach(function (opts) {
const test = R.merge(defaultParams, opts)
test.expected = R.merge(defaultParams.expected, opts.expected)

it(test.name, function (done) {
const server = hexi(express())

Expand Down Expand Up @@ -184,7 +209,10 @@ describe('bundle', function () {
},
])
.then(() => {
const req = supertest(server.express).get(test.path)
const req = supertest(server.express)
.get(test.path)

req.expect(test.expected.status)

Object.keys(test.expected.headers).forEach(function (headerKey) {
req.expect(
Expand All @@ -193,7 +221,11 @@ describe('bundle', function () {

req.end(function (err, res) {
expect(err).to.not.exist
compareToFile(test.expected.fileName, res.text)
if (test.expected.fileName) {
compareToFile(test.expected.fileName, res.text)
} else {
expect(test.expected.fileContent).eq(res.text)
}
done()
})
})
Expand Down
45 changes: 45 additions & 0 deletions test/web/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,49 @@ describe('raw', function () {
})
})
})

it('should return error when no file found', function (done) {
const server = hexi(express())

const bundleService = createBundleService({
maxAge: {
'default': '4h',
},
storagePath: path.resolve(__dirname, '../../.cdn-cache'),
})

return server.register([
{
register: require('hexi-cache'),
},
{
register: plugiator.noop('registry-store'),
},
{
register: registry,
options: {
defaultRegistry: {
url: 'https://registry.npmjs.org/',
},
},
},
{
register: raw,
options: {
bundleService,
},
},
])
.then(() => {
request(server.express)
.get('/raw/applyqqq@0.2.1/index.js')
.expect('content-type', 'text/plain; charset=utf-8')
.expect(404)
.end((err, res) => {
expect(err).to.not.exist
expect(res.text).to.eq('Not found: file "index.js" in package applyqqq@0.2.1')
done()
})
})
})
})

0 comments on commit df9dab8

Please sign in to comment.