Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robsontenorio committed Mar 29, 2018
1 parent a2f3fbd commit 35aa691
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 23 deletions.
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -40,7 +40,6 @@
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"codecov": "^3.0.0",
"jest": "^22.4.3",
"moxios": "^0.4.0"
"jest": "^22.4.3"
}
}
6 changes: 3 additions & 3 deletions src/Builder.js
Expand Up @@ -31,10 +31,10 @@ export default class Builder {

where (key, value) {
if (key === undefined || value === undefined)
throw new Error('The "key" and "value" are required on where() method')
throw new Error('The KEY and VALUE are required on where() method.')

if (Array.isArray(value) || value instanceof Object)
throw new Error('The "value" must be primitive on where() method')
throw new Error('The VALUE must be primitive on where() method.')

this.filters.filter[key] = value

Expand All @@ -43,7 +43,7 @@ export default class Builder {

whereIn (key, array) {
if (!Array.isArray(array))
throw new Error('The second argument on whereIn() method must be an array')
throw new Error('The second argument on whereIn() method must be an array.')

this.filters.filter[key] = array.join(',')

Expand Down
30 changes: 16 additions & 14 deletions src/StaticModel.js
@@ -1,71 +1,73 @@
/**
* Handle static calls for all methods.
*
* Instead `let users = new User().with('country').get()`
* You cand do `let users = User.with('conutry').get()`
*
* Provide static calls for all methods.
*
* Instead this: let users = new User().with('country').get()
* We can do this: let users = User.with('conutry').get()
*
*/

export default class StaticModel {

static instance () {
return new this
}

static custom (resource) {
let self = typeof this === 'object' ? this : new this
let self = this.instance()
self.custom(resource)

return self
}

static include (...args) {
let self = typeof this === 'object' ? this : new this
let self = this.instance()
self.include(...args)

return self
}

static where (field, value) {
let self = typeof this === 'object' ? this : new this
let self = this.instance()
self.where(field, value)

return self
}

static whereIn (field, array) {
let self = typeof this === 'object' ? this : new this
let self = this.instance()
self.whereIn(field, array)

return self
}

static append (...args) {
let self = typeof this === 'object' ? this : new this
let self = this.instance()
self.append(...args)

return self
}

static orderBy (...args) {
let self = typeof this === 'object' ? this : new this
let self = this.instance()
self.orderBy(...args)

return self
}

static first () {
let self = typeof this === 'object' ? this : new this
let self = this.instance()

return self.first()
}

static find (id) {
let self = typeof this === 'object' ? this : new this
let self = this.instance()

return self.find(id)
}

static get () {
let self = typeof this === 'object' ? this : new this
let self = this.instance()

return self.get()
}
Expand Down
7 changes: 7 additions & 0 deletions tests/model.test.js
Expand Up @@ -48,6 +48,13 @@ describe('Model methods', () => {
expect(post).toBeInstanceOf(Post)
})

test('first() method returns a empty object when no items have found', async () => {
axiosMock.onGet('http://localhost/posts').reply(200, [])

const post = await Post.first()
expect(post).toEqual({})
})

test('find() method returns a object as instance of such Model', async () => {
axiosMock.onGet('http://localhost/posts/1').reply(200, postResponse)

Expand Down
48 changes: 48 additions & 0 deletions tests/static.test.js
Expand Up @@ -43,4 +43,52 @@ describe('Static calls', () => {

expect(post._builder.sorts).toEqual(['created_at', '-visits'])
})

test('where() sets properly the builder', () => {
let post = Post.where('id', 1)

expect(post._builder.filters.filter).toEqual({ id: 1 })

post = Post.where('id', 1).where('title', 'Cool')

expect(post._builder.filters.filter).toEqual({ id: 1, title: 'Cool' })
})

test('where() throws a exception when doest not have params or only first param', () => {
let errorModel = () => {
const post = Post.where()
}

expect(errorModel).toThrow('The KEY and VALUE are required on where() method.')

errorModel = () => {
const post = Post.where('id')
}

expect(errorModel).toThrow('The KEY and VALUE are required on where() method.')
})

test('where() throws a exception when second parameter is not primitive', () => {
let errorModel = () => {
const post = Post.where('id', ['foo'])
}

expect(errorModel).toThrow('The VALUE must be primitive on where() method.')
})

test('whereIn() sets properly the builder', () => {
let post = Post.whereIn('status', ['ACTIVE', 'ARCHIVED'])

expect(post._builder.filters.filter).toEqual({ status: 'ACTIVE,ARCHIVED' })
})

test('whereIn() throws a exception when second parameter is not a array', () => {
let errorModel = () => {
const post = Post.whereIn('id', 'foo')
}

expect(errorModel).toThrow('The second argument on whereIn() method must be an array.')
})


})
4 changes: 0 additions & 4 deletions yarn.lock
Expand Up @@ -2565,10 +2565,6 @@ mixin-deep@^1.2.0:
dependencies:
minimist "0.0.8"

moxios@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/moxios/-/moxios-0.4.0.tgz#fc0da2c65477d725ca6b9679d58370ed0c52f53b"

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down

0 comments on commit 35aa691

Please sign in to comment.