Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #21

Merged
merged 2 commits into from
Jun 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import isPathValid from 'is-valid-path'

const INVALID_PATH_ERROR = 'Invalid path'
const INVALID_JSON_PATH_ERROR = 'Not a .json file'

export const isAJsonPath = (path) => {
return /\.json$/.test(path)
}
Expand All @@ -16,11 +19,11 @@ export const isAJson = (json) => {
}

export const validateJsonPath = (jsonPath) => {
if (!isPathValid(jsonPath) || !isAJsonPath(jsonPath)) {
throw (Error('Is a invalid path'))
if (!isPathValid(jsonPath)) {
throw (Error(INVALID_PATH_ERROR))
}

if (!isAJsonPath(jsonPath)) {
throw (Error('Is not a JSON file'))
throw (Error(INVALID_JSON_PATH_ERROR))
}
}
37 changes: 22 additions & 15 deletions test/jq.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)
import { run } from '../src/jq'
import path from 'path'

const FIXTURES_PATH = path.join(__dirname, 'fixtures')
const ROOT_PATH = path.join(__dirname, '..')
const jsonPathFixture = path.join(FIXTURES_PATH, '1.json')
const jsPathFixture = path.join(FIXTURES_PATH, '1.js')
const asteriskPathFixture = path.join(ROOT_PATH, 'src', '*.js')
const filter = '. | map(select(.a == .id))'
import { run } from '../src/jq'

const PATH_ROOT = path.join(__dirname, '..')
const PATH_FIXTURES = path.join(__dirname, 'fixtures')
const PATH_JSON_FIXTURE = path.join(PATH_FIXTURES, '1.json')
const PATH_JS_FIXTURE = path.join(PATH_FIXTURES, '1.js')
const PATH_ASTERISK_FIXTURE = path.join(PATH_ROOT, 'src', '*.js')

const FILTER_VALID = '. | map(select(.a == .id))'
const FILTER_INVALID = 'invalid'

const ERROR_INVALID_FILTER = /Command failed: jq invalid/
const ERROR_INVALID_PATH = 'Invalid path'
const ERROR_INVALID_JSON_PATH = 'Not a .json file'

describe('jq core', () => {
it('should return a stdout object', () => {
return expect(
run(filter, jsonPathFixture)
).to.eventually.be.instanceof(Object)
run(FILTER_VALID, PATH_JSON_FIXTURE)
).to.eventually.be.fulfilled
})

it('should fail on a non valid filter', () => {
return expect(
run('lola', jsonPathFixture)
).to.eventually.be.rejectedWith(Error)
run(FILTER_INVALID, PATH_JSON_FIXTURE)
).to.eventually.be.rejectedWith(Error, ERROR_INVALID_FILTER)
})

it('should fail on a non valid path', () => {
return expect(
run(filter, asteriskPathFixture)
).to.eventually.be.rejectedWith(Error)
run(FILTER_VALID, PATH_ASTERISK_FIXTURE)
).to.eventually.be.rejectedWith(Error, ERROR_INVALID_PATH)
})

it('should fail on a non valid json', () => {
return expect(
run(filter, jsPathFixture)
).to.eventually.be.rejectedWith(Error)
run(FILTER_VALID, PATH_JS_FIXTURE)
).to.eventually.be.rejectedWith(Error, ERROR_INVALID_JSON_PATH)
})
})
8 changes: 4 additions & 4 deletions test/options.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)

import { run } from '../src/jq'
import jsonFixture from './fixtures/1.json'

const jsonFixtureToString = JSON.stringify(jsonFixture)
import FIXTURE_JSON from './fixtures/1.json'

describe('options', () => {
describe('--null-input', () => {
it('should run the filter inline with null-input', () => {
return expect(
run('.', jsonFixtureToString, { nullInput: true })
).to.eventually.become(jsonFixtureToString)
run('.', FIXTURE_JSON, { nullInput: true })
).to.eventually.become(FIXTURE_JSON)
})
})
})
21 changes: 12 additions & 9 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@ import chai, { expect } from 'chai'
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)
import path from 'path'
import jsonFixture from './fixtures/1.json'

import { isAJsonPath, isAJson } from '../src/utils'

const jsonFixtureToString = JSON.stringify(jsonFixture)
const FIXTURES_PATH = path.join(__dirname, 'fixtures')
const jsonPathFixture = path.join(FIXTURES_PATH, '1.json')
const jsPathFixture = path.join(FIXTURES_PATH, '1.js')
import FIXTURE_JSON from './fixtures/1.json'
const FIXTURE_VALID_JSON_STRING = JSON.stringify(FIXTURE_JSON)
const FIXTURE_INVALID_JSON_STRING = 'test_invalid'

const PATH_FIXTURES = path.join(__dirname, 'fixtures')
const PATH_JSON_FIXTURE = path.join(PATH_FIXTURES, '1.json')
const PATH_JS_FIXTURE = path.join(PATH_FIXTURES, '1.js')

describe('utils', () => {
describe('#isAJsonPath', () => {
it('should return true when u give a jsonpath', () => {
expect(isAJsonPath(jsonPathFixture)).to.be.equal(true)
expect(isAJsonPath(PATH_JSON_FIXTURE)).to.be.true
})

it('should return false when u give a non-jsonpath', () => {
expect(isAJsonPath(jsPathFixture)).to.be.equal(false)
expect(isAJsonPath(PATH_JS_FIXTURE)).to.be.false
})
})

describe('#isAJson', () => {
it('should return true when u give a json', () => {
expect(isAJson(jsonFixtureToString)).to.be.equal(true)
expect(isAJson(FIXTURE_VALID_JSON_STRING)).to.be.true
})

it('should return false when u give a non-json', () => {
expect(isAJson('lola')).to.be.equal(false)
expect(isAJson(FIXTURE_INVALID_JSON_STRING)).to.be.false
})
})
})