Skip to content

Commit

Permalink
Adds service resource with associated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Nov 15, 2018
1 parent 682f1bb commit a95fdf1
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 0 deletions.
148 changes: 148 additions & 0 deletions src/resources/service.test.ts
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',
},
});
});
108 changes: 108 additions & 0 deletions src/resources/service.ts
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;
}
}
7 changes: 7 additions & 0 deletions src/types/filters.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export interface Filterable<T> {
sort?: string;
}

export interface ServiceFilter {
assigned?: boolean;
category?: number | string;
location?: number | string;
user?: number | string;
}

export interface UserFilter {
assigned?: boolean;
services?: number | number[] | string | string[];
Expand Down
7 changes: 7 additions & 0 deletions src/types/parameters.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export interface ServiceParameters {
assigned?: boolean;
category?: number | string;
location?: number | string;
user?: number | string;
}

export interface UserParameters {
assigned?: boolean;
service?: number | number[] | string | string[];
Expand Down
10 changes: 10 additions & 0 deletions src/types/resources.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export interface Resource {
get(): Promise<any>;
}

export interface ServiceResource extends Pageable {
assigned(assigned: boolean): this;

at(location: number | string): this;

by(user: number | string): this;

in(category: number | string): this;
}

export interface Sortable extends Resource {
sortBy(sortable: string): this;
}
Expand Down

0 comments on commit a95fdf1

Please sign in to comment.