Skip to content

Commit

Permalink
Implements necessary methods for form resource
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Jul 12, 2019
1 parent e8f8fc5 commit 1119232
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/constants/forms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class Forms {
static get CANCELLATION() {
return 1;
}
}
88 changes: 88 additions & 0 deletions src/resources/form.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import mockAxios from 'axios';

import Forms from '../constants/forms';
import Form from './form';

it('will set cancellation filter and necessary included resources', async () => {
const resource = new Form(mockAxios);

const expected = expect(resource.cancellations());

expected.toHaveProperty('filters', {
type: Forms.CANCELLATION,
});
expected.toHaveProperty('parameters', {
include: 'questions.options',
});
});

it('will set the page we are on', async () => {
const resource = new Form(mockAxios);

expect(resource.on(4)).toHaveProperty('page', 4);
});

it('will set the limit given', async () => {
const resource = new Form(mockAxios);

expect(resource.take(5)).toHaveProperty('limit', 5);
});

it('will set the sortable filter', async () => {
const resource = new Form(mockAxios);

expect(resource.sortBy('type,-created')).toHaveProperty('sortable', 'type,-created');
});

it('can string all filterable options together', async () => {
const resource = new Form(mockAxios);

const expected = expect(
resource
.cancellations()
.sortBy('created')
.take(5)
.on(1),
);

expected.toHaveProperty('filters', {
type: Forms.CANCELLATION,
});
expected.toHaveProperty('parameters', {
include: 'questions.options',
});
expected.toHaveProperty('sortable', 'created');
expected.toHaveProperty('limit', 5);
expected.toHaveProperty('page', 1);
});

it('can get forms without additional parameters', async () => {
const resource = new Form(mockAxios);

await resource.get();

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('forms', { params: {} });
});

it('can get forms with additional parameters', async () => {
const resource = new Form(mockAxios);

await resource
.cancellations()
.sortBy('created')
.take(5)
.on(1)
.get();

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('forms', {
params: {
'filter[type]': Forms.CANCELLATION,
include: 'questions.options',
limit: 5,
page: 1,
sort: 'created',
},
});
});
90 changes: 86 additions & 4 deletions src/resources/form.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,105 @@
import { AxiosInstance } from 'axios';

import { Resource } from '../index';
import Forms from '../constants/forms';
import { combine } from '../helpers/filters';
import { Filterable, Pageable } from '../index';

export interface FormResource extends Resource {
export interface FormFilter {
type?: number;
}

export interface FormFilterable<T> extends Filterable<T> {
include?: string;
}

export interface FormUrlParameters {
include?: string;
}

interface FormParameters {
type?: number;
}

export interface FormResource extends Pageable {
cancellations(): this;
}

export default class Form implements FormResource {
protected client: AxiosInstance;
protected filters: FormFilter;
protected page: number | null;
protected parameters: FormUrlParameters;
protected sortable: string | null;
protected limit: number | null;

constructor(client: AxiosInstance) {
this.client = client;
this.filters = {};
this.page = null;
this.parameters = {};
this.sortable = null;
this.limit = null;
}

public cancellations(): this {
this.filters.type = Forms.CANCELLATION;
this.parameters.include = 'questions.options';

return this;
}

public get(): Promise<any> {
return new Promise((resolve) => resolve());
public async get(): Promise<any> {
const parameters = this.params();
let params: FormFilterable<FormFilter> = {};

if (Object.keys(parameters).length) {
params = combine(params, parameters);
}

if (this.parameters.include) {
params.include = this.parameters.include;
}

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('forms', { params });
}

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(): FormParameters {
const params: FormParameters = {};

if (typeof this.filters.type !== 'undefined') {
params.type = this.filters.type;
}

return params;
}
}

0 comments on commit 1119232

Please sign in to comment.