-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds service resource with associated tests
- Loading branch information
1 parent
682f1bb
commit a95fdf1
Showing
5 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import mockAxios from 'axios'; | ||
|
||
import Service from './service'; | ||
|
||
it('will set assigned filter', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.assigned()).toHaveProperty('filters', { | ||
assigned: true, | ||
}); | ||
}); | ||
|
||
it('will set assigned filter to false', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.assigned(false)).toHaveProperty('filters', { | ||
assigned: false, | ||
}); | ||
}); | ||
|
||
it('will set location filter using a number', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.at(1)).toHaveProperty('filters', { | ||
location: 1, | ||
}); | ||
}); | ||
|
||
it('will set location filter using a string', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.at('identifier')).toHaveProperty('filters', { | ||
location: 'identifier', | ||
}); | ||
}); | ||
|
||
it('will set user filter using a number', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.by(1)).toHaveProperty('filters', { | ||
user: 1, | ||
}); | ||
}); | ||
|
||
it('will set user filter using a string', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.by('identifier')).toHaveProperty('filters', { | ||
user: 'identifier', | ||
}); | ||
}); | ||
|
||
it('will set category filter using a number', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.in(1)).toHaveProperty('filters', { | ||
category: 1, | ||
}); | ||
}); | ||
|
||
it('will set category filter using a string', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.in('identifier')).toHaveProperty('filters', { | ||
category: 'identifier', | ||
}); | ||
}); | ||
|
||
it('will set the page we are on', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.on(4)).toHaveProperty('page', 4); | ||
}); | ||
|
||
it('will set the limit given', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.take(5)).toHaveProperty('limit', 5); | ||
}); | ||
|
||
it('will set the sortable filter', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
expect(resource.sortBy('name,-created')).toHaveProperty('sortable', 'name,-created'); | ||
}); | ||
|
||
it('can string all filterable options together', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
const expected = expect( | ||
resource | ||
.assigned() | ||
.at(1) | ||
.by(2) | ||
.in(3) | ||
.sortBy('created') | ||
.take(5) | ||
.on(1), | ||
); | ||
|
||
expected.toHaveProperty('filters', { | ||
assigned: true, | ||
category: 3, | ||
location: 1, | ||
user: 2, | ||
}); | ||
expected.toHaveProperty('sortable', 'created'); | ||
expected.toHaveProperty('limit', 5); | ||
expected.toHaveProperty('page', 1); | ||
}); | ||
|
||
it('can get services without additional parameters', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
await resource.get(); | ||
|
||
expect(mockAxios.get).toHaveBeenCalledTimes(1); | ||
expect(mockAxios.get).toHaveBeenCalledWith('services', { params: {} }); | ||
}); | ||
|
||
it('can get services with additional parameters', async () => { | ||
const resource = new Service(mockAxios); | ||
|
||
await resource | ||
.assigned() | ||
.at(1) | ||
.by(2) | ||
.in(3) | ||
.sortBy('created') | ||
.take(5) | ||
.on(1) | ||
.get(); | ||
|
||
expect(mockAxios.get).toHaveBeenCalledTimes(1); | ||
expect(mockAxios.get).toHaveBeenCalledWith('services', { | ||
params: { | ||
filters: { | ||
assigned: true, | ||
category: 3, | ||
location: 1, | ||
user: 2, | ||
}, | ||
limit: 5, | ||
page: 1, | ||
sort: 'created', | ||
}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { AxiosInstance } from 'axios'; | ||
|
||
import { Filterable, ServiceFilter } from '../types/filters'; | ||
import { ServiceParameters } from '../types/parameters'; | ||
import { ServiceResource } from '../types/resources'; | ||
|
||
export default class Service implements ServiceResource { | ||
protected client: AxiosInstance; | ||
protected filters: ServiceFilter; | ||
protected page: number | null; | ||
protected sortable: string | null; | ||
protected limit: number | null; | ||
|
||
constructor(client: AxiosInstance) { | ||
this.client = client; | ||
this.filters = {}; | ||
this.page = null; | ||
this.sortable = null; | ||
this.limit = null; | ||
} | ||
|
||
public assigned(assigned: boolean = true): this { | ||
this.filters.assigned = assigned; | ||
|
||
return this; | ||
} | ||
|
||
public at(location: number | string): this { | ||
this.filters.location = location; | ||
|
||
return this; | ||
} | ||
|
||
public by(user: number | string): this { | ||
this.filters.user = user; | ||
|
||
return this; | ||
} | ||
|
||
public async get(): Promise<any> { | ||
const parameters = this.params(); | ||
const params: Filterable<ServiceFilter> = {}; | ||
|
||
if (Object.keys(parameters).length) { | ||
params.filters = parameters; | ||
} | ||
|
||
if (this.limit) { | ||
params.limit = this.limit; | ||
} | ||
|
||
if (this.page) { | ||
params.page = this.page; | ||
} | ||
|
||
if (this.sortable) { | ||
params.sort = this.sortable; | ||
} | ||
|
||
return await this.client.get('services', { params }); | ||
} | ||
|
||
public in(category: number | string): this { | ||
this.filters.category = category; | ||
|
||
return this; | ||
} | ||
|
||
public on(page: number): this { | ||
this.page = page; | ||
|
||
return this; | ||
} | ||
|
||
public sortBy(sortable: string): this { | ||
this.sortable = sortable; | ||
|
||
return this; | ||
} | ||
|
||
public take(limit: number): this { | ||
this.limit = limit; | ||
|
||
return this; | ||
} | ||
|
||
protected params(): ServiceParameters { | ||
const params: ServiceParameters = {}; | ||
|
||
if (typeof this.filters.assigned !== 'undefined') { | ||
params.assigned = this.filters.assigned; | ||
} | ||
|
||
if (typeof this.filters.category !== 'undefined') { | ||
params.category = this.filters.category; | ||
} | ||
|
||
if (typeof this.filters.location !== 'undefined') { | ||
params.location = this.filters.location; | ||
} | ||
|
||
if (typeof this.filters.user !== 'undefined') { | ||
params.user = this.filters.user; | ||
} | ||
|
||
return params; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters