Skip to content

Commit

Permalink
WIP | Client API Tests (#142)
Browse files Browse the repository at this point in the history
* Adding debug config for VSCode

* adding tests for createPackageDetails

* adding null safe accessors using lodash get

* Testing undefined values for package properties
  • Loading branch information
dgautsch authored Oct 22, 2018
1 parent 6e4d0aa commit 7e3af81
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
29 changes: 29 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha Tests",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test"
],
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/lib/start.js"
}
]
}
13 changes: 7 additions & 6 deletions lib/storage/base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const _ = require('lodash')

class Base {
getJSON (key) {
Expand All @@ -8,16 +9,16 @@ class Base {
if (json.name === '' || json.name === undefined) {
return
}
const currentVersion = json['dist-tags'].latest || ''
const author = json.versions[currentVersion].author || ''
const tarball = json.versions[currentVersion].dist
const currentVersion = _.get(json, ['dist-tags', 'latest'], '')
const author = _.get(json, ['versions', currentVersion, 'author'], '')
const tarball = _.get(json, ['versions', currentVersion, 'dist'])

return {
name: json.name || '',
name: _.get(json, 'name'),
currentVersion,
author,
description: json.description || 'no description',
readme: json.readme || '',
description: _.get(json, 'description', 'no description'),
readme: _.get(json, 'readme'),
tarball
}
}
Expand Down
53 changes: 53 additions & 0 deletions test/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const Base = require('../lib/storage/base')
const expect = require('chai').expect

describe('storage', () => {
describe('base storage methods', () => {
let mockJson = {
name: 'foo',
'dist-tags': {
latest: 1
},
versions: {
1: {
author: 'bar',
dist: 'baz'
}
}
}
let base
let packageDetails

beforeEach(() => {
base = new Base()
})

afterEach(() => {
mockJson.name = 'foo'
mockJson['dist-tags'].latest = 1
})

it('should escape if name property is undefined or empty string', () => {
mockJson.name = ''
expect(base).to.be.an.instanceof(Base)
expect(base.createPackageDetails(mockJson)).to.be.an('undefined')
})

it('should return a package object', () => {
packageDetails = base.createPackageDetails(mockJson)
expect(base).to.be.an.instanceof(Base)
expect(packageDetails).to.be.an('object')
expect(packageDetails.name).to.equal('foo')
expect(packageDetails.currentVersion).to.equal(1)
expect(packageDetails.author).to.equal('bar')
expect(packageDetails.tarball).to.equal('baz')
})

it('should return an empty string if latest is null', () => {
delete mockJson['dist-tags'].latest
packageDetails = base.createPackageDetails(mockJson)
expect(base).to.be.an.instanceof(Base)
expect(packageDetails.currentVersion).to.equal('')
})
})
})

0 comments on commit 7e3af81

Please sign in to comment.