Skip to content

Commit

Permalink
fix tests for categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Angamanga committed Jun 2, 2020
1 parent ed719ad commit 018c055
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 58 deletions.
57 changes: 55 additions & 2 deletions __tests__/categories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@ describe('Categories-constructor', () => {
});
});

describe('setToken-function', () => {
it('should set the token', () => {
expect(ushahidiCategories.token).toEqual('token');
ushahidiCategories.setToken('new token');
expect(ushahidiCategories.token).toEqual('new token');
ushahidiCategories.setToken('token');
expect(ushahidiCategories.token).toEqual('token');
});
});

describe('getCategories-function', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should return all categories', async () => {
const data = [{id: 2}, {id:3}];
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({data:{results:data}}));
await expect(ushahidiCategories.getCategories()).resolves.toEqual(data);
});
it('should return one survey', async () => {
it('should return one category', async () => {
const data = {id: 1};
mockedAxios.get.mockImplementationOnce(() => Promise.resolve({data:{result:data}}));
await expect(ushahidiCategories.getCategories('1')).resolves.toEqual(data);
Expand All @@ -47,4 +58,46 @@ describe('getCategories-function', () => {
const returnValue = await ushahidiCategories.getCategories('1');
expect(returnValue).toEqual(Error);
});
});
});

describe('saveCategory-function', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should use post-method if no id is provided', async () => {
const category = {id:null, name:'Test category'};
await ushahidiCategories.saveCategory(category);
expect(axios).toBeCalledWith({
method: 'post',
url: 'http://api.test.com/api/v4/categories/',
headers: { 'Authorization': 'Bearer token' },
data: category,
});
});

it('should use put-method if id is provided', async () => {
const category = {id:'1', name:'Test category'};
await ushahidiCategories.saveCategory(category);
expect(axios).toBeCalledWith({
method: 'put',
url: 'http://api.test.com/api/v4/categories/1',
headers: { 'Authorization': 'Bearer token' },
data: category,
});
});
});

describe('deleteCategories-function', () => {
afterEach(() => {
jest.restoreAllMocks();
});

it('should use delete-method', async () => {
await ushahidiCategories.deleteCategory('1');
expect(axios).toBeCalledWith({
method: 'delete',
url: 'http://api.test.com/api/v4/categories/1',
headers: { 'Authorization': 'Bearer token' }
});
});
});
111 changes: 55 additions & 56 deletions src/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,60 @@
import axios from 'axios';

export class Categories {
backendUrl:string;
token:string;

constructor (backendUrl:string, token?:string) {
this.backendUrl = backendUrl;
this.token = token;
}

setToken(token?:string):object {
this.token = token;
return this;
}

async getCategories(query:object, id?:string):Promise<unknown> {
try {
const url = id ? `${this.backendUrl}/api/v4/categories/${id}` : `${this.backendUrl}/api/v4/categories/`;
const config = this.token ? {
headers: {"Authorization": `Bearer ${this.token}`},
data: query
} : {}
const response = await axios.get(url, config);
return response.data.result || response.data.results;
}
catch(err) {
return err;
}
}


async saveCategory(category:{id?:string}):Promise<unknown> {
const method = category.id ? 'put' : 'post';
const url = category.id ? `${this.backendUrl}/api/v4/categories/${category.id}` : `${this.backendUrl}/api/v4/categories/`;
try {
const res = await axios({
method: method,
url: url,
headers:{
"Authorization": `Bearer ${this.token}`
},
data: category
});
return res;
} catch(err) {
return err;
}
}

async deleteCategory(id:string):Promise<unknown> {
const res = await axios({
method: 'delete',
url: `${this.backendUrl}/api/v4/categories/${id}`,
headers:{
"Authorization": `Bearer ${this.token}`
}
});
return res;
backendUrl: string;
token: string;

constructor(backendUrl: string, token?: string) {
this.backendUrl = backendUrl;
this.token = token;
}

setToken(token?: string): object {
this.token = token;
return this;
}

async getCategories(id?: string): Promise<unknown> {
try {
const url = id
? `${this.backendUrl}/api/v4/categories/${id}`
: `${this.backendUrl}/api/v4/categories/`;
const config = this.token
? {
headers: { Authorization: `Bearer ${this.token}` },
}
: {};
const response = await axios.get(url, config);
return response.data.result || response.data.results;
} catch (err) {
return err;
}
}

async saveCategory(category: { id?: string }): Promise<unknown> {
const method = category.id ? 'put' : 'post';
const url = category.id
? `${this.backendUrl}/api/v4/categories/${category.id}`
: `${this.backendUrl}/api/v4/categories/`;
const res = await axios({
method: method,
url: url,
headers: {
Authorization: `Bearer ${this.token}`,
},
data: category,
});
return res;
}

async deleteCategory(id: string): Promise<unknown> {
const res = await axios({
method: 'delete',
url: `${this.backendUrl}/api/v4/categories/${id}`,
headers: {
Authorization: `Bearer ${this.token}`,
},
});
return res;
}
}

0 comments on commit 018c055

Please sign in to comment.