Skip to content

Commit

Permalink
Adds setting resource with accompanying tests
Browse files Browse the repository at this point in the history
  • Loading branch information
coconutcraig committed Nov 15, 2018
1 parent 88d5a90 commit 682f1bb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import OpenApi from './index';
import Setting from './resources/setting';
import User from './resources/user';

it('will be constructed with appropriate required options', async () => {
Expand Down Expand Up @@ -32,3 +33,12 @@ it('can access the user resource', async () => {
expect(instance).toHaveProperty('users');
expect(instance.users).toBeInstanceOf(User.prototype.constructor);
});

it('can access the setting resource', async () => {
const instance = new OpenApi({
domain: 'admin',
});

expect(instance).toHaveProperty('settings');
expect(instance.settings).toBeInstanceOf(Setting.prototype.constructor);
});
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { AxiosInstance } from 'axios';

import Client from './client';
import Setting from './resources/setting';
import User from './resources/user';
import { OpenApiOptions } from './types/options';
import { UserResource } from './types/resources';
import { Resource, UserResource } from './types/resources';

export default class OpenApi {
protected client: AxiosInstance;
protected options: OpenApiOptions;
protected setting: Resource;
protected user: UserResource;

constructor({ domain, version = 'v2' }: OpenApiOptions) {
this.client = Client({ domain, version });
this.options = { domain, version };
this.setting = new Setting(this.client);
this.user = new User(this.client);
}

get settings(): Resource {
return this.setting;
}

get users(): UserResource {
return this.user;
}
Expand Down
12 changes: 12 additions & 0 deletions src/resources/setting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import mockAxios from 'axios';

import Setting from './setting';

it('can get settings', async () => {
const resource = new Setting(mockAxios);

await resource.get();

expect(mockAxios.get).toHaveBeenCalledTimes(1);
expect(mockAxios.get).toHaveBeenCalledWith('settings');
});
15 changes: 15 additions & 0 deletions src/resources/setting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AxiosInstance } from 'axios';

import { Resource } from '../types/resources';

export default class Setting implements Resource {
protected client: AxiosInstance;

constructor(client: AxiosInstance) {
this.client = client;
}

public async get(): Promise<any> {
return await this.client.get('settings');
}
}
1 change: 1 addition & 0 deletions src/resources/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mockAxios from 'axios';

import User from './user';

it('will set assigned filter', async () => {
Expand Down
14 changes: 8 additions & 6 deletions src/types/resources.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
export interface Resource {
get(): Promise<any>;
export interface Pageable extends Sortable {
on(page: number): this;

sortBy(sortable: string): this;
take(limit: number): this;
}

export interface Pageable extends Resource {
on(page: number): this;
export interface Resource {
get(): Promise<any>;
}

take(limit: number): this;
export interface Sortable extends Resource {
sortBy(sortable: string): this;
}

export interface UserResource extends Pageable {
Expand Down

0 comments on commit 682f1bb

Please sign in to comment.