Skip to content

Commit

Permalink
Add test for defaults field scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrafael committed Jun 1, 2020
1 parent 74e7bc1 commit de5f8b4
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions test/unit/fields.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ describe('QueryString: Fields', function () {

context('when query fields are a simple string', function () {
it('should return a JSON with field params', function (done) {
const query = { fields: 'name,age,created_at' }
const query = {fields: 'name,age,created_at'}
verify(fields.fields(query, default_options))
done()
})
})

context('when query fields are an array of strings', function () {
it('should return a JSON with field params', function (done) {
const query = { fields: ['name,age', 'created_at'] }
const query = {fields: ['name,age', 'created_at']}
verify(fields.fields(query, default_options))
done()
})
})

context('when there are blank spaces between query fields', function () {
it('should return a JSON with field params, ignoring the blank space', function (done) {
const query = { fields: ' name , name, age , created_at' }
const query = {fields: ' name , name, age , created_at'}
verify(fields.fields(query, default_options))
done()
})
})

context('when there are null fields in query fields', function () {
it('should return a JSON with field params, ignoring the null fields', function (done) {
const query = { fields: ',,name,,,age,,,,,created_at,,' }
const query = {fields: ',,name,,,age,,,,,created_at,,'}
verify(fields.fields(query, default_options))
done()
})
})

context('when there are special characters in query fields', function () {
it('should return a JSON with field params, ignoring the special characteres', function (done) {
const query = { fields: ' ,,, ^ & * ( ´) @!n@a"m "e,$%ag" e",created _a t ' }
const query = {fields: ' ,,, ^ & * ( ´) @!n@a"m "e,$%ag" e",created _a t '}
verify(fields.fields(query, default_options))
done()

Expand All @@ -52,17 +52,21 @@ describe('QueryString: Fields', function () {
})
})

context('when use custom params without query', function () {
context('when use custom params', function () {
it('should return a JSON with custom params', function () {
const custom_options = { default: { fields: { name: 1, age: 1, _id: 0 } } }
const custom_options = {default: {fields: {name: 1, age: 1, _id: 0}}}
const result = fields.fields({}, custom_options)
expect(result).to.have.property('name')
expect(result).to.have.property('age')
expect(result).to.have.property('_id')
expect(result.name).to.eql(custom_options.default.fields.name)
expect(result.age).to.eql(custom_options.default.fields.age)
expect(result._id).to.eql(custom_options.default.fields._id)
})

it('should return a JSON with custom params and those of the query', function () {
const custom_options = {default: {fields: {name: 1, _id: 1}}}
const result = fields.fields({fields: 'age'}, custom_options)
expect(result.name).to.eql(custom_options.default.fields.name)
expect(result._id).to.eql(custom_options.default.fields._id)
expect(result.age).to.eql(1)
})
})
})
Expand Down

0 comments on commit de5f8b4

Please sign in to comment.