Skip to content

Commit

Permalink
Merge pull request #207 from richardzcode/api-ajax
Browse files Browse the repository at this point in the history
add patch interface to API
  • Loading branch information
richardzcode committed Jan 31, 2018
2 parents e1eb0ab + 72e9c8b commit 7be7470
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 79 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ For React Native development, install `aws-amplify-react-native` instead of `aws
npm install aws-amplify-react-native --save
```

Unless you're react-native app was created using [Expo v25.0.0 or greater](https://blog.expo.io/expo-sdk-v25-0-0-is-now-available-714d10a8c3f7), you will need to [link](https://facebook.github.io/react-native/docs/linking-libraries-ios.html) libraries in your project for the Auth module on React Native. Follow the instructions [here](https://github.com/aws/aws-amplify/blob/master/media/quick_start.md#react-native-development).
Unless your react-native app was created using [Expo v25.0.0 or greater](https://blog.expo.io/expo-sdk-v25-0-0-is-now-available-714d10a8c3f7), you will need to [link](https://facebook.github.io/react-native/docs/linking-libraries-ios.html) libraries in your project for the Auth module on React Native. Follow the instructions [here](https://github.com/aws/aws-amplify/blob/master/media/quick_start.md#react-native-development).

## Documentation

Expand Down Expand Up @@ -155,4 +155,4 @@ With configurable settings, store content in an S3 bucket in public folders for

## Contributing

See [Contributing Guidelines](https://github.com/aws/aws-amplify/blob/master/CONTRIBUTING.md)
See [Contributing Guidelines](https://github.com/aws/aws-amplify/blob/master/CONTRIBUTING.md)
95 changes: 94 additions & 1 deletion packages/aws-amplify/__tests__/API/API-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,99 @@ describe('API test', () => {
});
});

describe('patch test', () => {
test('happy case', async () => {
const api = new API(config);
const spyon = jest.spyOn(Auth, 'currentCredentials').mockImplementationOnce(() => {
return new Promise((res, rej) => {
res('cred');
});
});
const spyon2 = jest.spyOn(RestClient.prototype, 'patch').mockImplementationOnce(() => {
return Promise.resolve();
});
const spyon3 = jest.spyOn(RestClient.prototype, 'endpoint').mockImplementationOnce(() => {
return 'endpoint';
});

await api.patch('apiName', 'path', 'init');

expect(spyon2).toBeCalledWith('endpointpath', 'init');

spyon.mockClear();
spyon2.mockClear();
spyon3.mockClear();
});

test.skip('endpoint length 0', async () => {
const api = new API(config);
const spyon = jest.spyOn(Auth, 'currentCredentials').mockImplementationOnce(() => {
return new Promise((res, rej) => {
res('cred');
});
});
const spyon2 = jest.spyOn(RestClient.prototype, 'patch').mockImplementationOnce(() => {
return Promise.resolve();
});
const spyon3 = jest.spyOn(RestClient.prototype, 'endpoint').mockImplementationOnce(() => {
return '';
});

expect.assertions(1);
try {
await api.patch('apiName', 'path', 'init');
} catch (e) {
expect(e).toBe('Api apiName does not exist');
}

spyon.mockClear();
spyon2.mockClear();
spyon3.mockClear();
});

test('cred not ready', async () => {
const api = new API(config);
const spyon = jest.spyOn(Auth, 'currentCredentials').mockImplementationOnce(() => {
return new Promise((res, rej) => {
rej('err');
});
});
const spyon2 = jest.spyOn(RestClient.prototype, 'patch').mockImplementationOnce(() => {
return Promise.resolve();
});
const spyon3 = jest.spyOn(RestClient.prototype, 'endpoint').mockImplementationOnce(() => {
return 'endpoint';
});

expect.assertions(1);
try {
await api.patch('apiName', 'path', 'init');
} catch (e) {
expect(e).toBe('No credentials');
}

spyon.mockClear();
spyon2.mockClear();
spyon3.mockClear();
});

test('no restclient instance', async () => {
const api = new API(config);
const spyon = jest.spyOn(API.prototype, 'createInstance').mockImplementationOnce(() => {
return Promise.reject('err');
});

expect.assertions(1);
try {
await api.patch('apiName', 'path', 'init');
} catch (e) {
expect(e).toBe('err');
}

spyon.mockClear();
});
});

describe('del test', () => {
test('happy case', async () => {
const api = new API(config);
Expand Down Expand Up @@ -555,4 +648,4 @@ describe('API test', () => {
spyon.mockClear();
});
});
});
});
39 changes: 38 additions & 1 deletion packages/aws-amplify/__tests__/API/RestClient-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,43 @@ describe('RestClient test', () => {
});
});

describe('patch test', () => {
test('happy case', async () => {
window.fetch = jest.fn().mockImplementationOnce((signed_params_url, signed_params) => {
return new Promise((res, rej) => {
res({
status: '200',
json: () => {
return signed_params.data;
}
});
});
});

const spyon = jest.spyOn(RestClient.prototype, 'ajax');

const apiOptions = {
headers: {},
endpoints: {},
credentials: {
accessKeyId: 'accessKeyId',
secretAccessKey: 'secretAccessKey',
sessionToken: 'sessionToken'
}
};

const restClient = new RestClient(apiOptions);

expect.assertions(3);
await restClient.patch('url', 'data');

expect(spyon.mock.calls[0][0]).toBe('url');
expect(spyon.mock.calls[0][1]).toBe('PATCH');
expect(spyon.mock.calls[0][2]).toBe('data');
spyon.mockClear();
});
});

describe('post test', () => {
test('happy case', async () => {
window.fetch = jest.fn().mockImplementationOnce((signed_params_url, signed_params) => {
Expand Down Expand Up @@ -371,4 +408,4 @@ describe('RestClient test', () => {
expect(restClient.endpoint('myApi')).toBe('endpoint of myApi');
});
});
});
});
32 changes: 20 additions & 12 deletions packages/aws-amplify/lib/API/API.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,58 @@ export default class API {
* @return - A promise of true if Success
*/
createInstance(): true | Promise<never>;
/**
* Make an patch request
* @param {string} apiName - The api name of the request
* @param {string} path - The path of the request'
* @param {json} [init] - Request extra params
* @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
*/
patch(apiName: any, path: any, init: any): Promise<any>;
/**
* Make a GET request
* @param {String} apiName - The api name of the request
* @param {JSON} path - The path of the request'
* @param {string} apiName - The api name of the request
* @param {string} path - The path of the request'
* @param {json} [init] - Request extra params
* @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
*/
get(apiName: any, path: any, init: any): Promise<any>;
/**
* Make a POST request
* @param {String} apiName - The api name of the request
* @param {String} path - The path of the request
* @param {string} apiName - The api name of the request
* @param {string} path - The path of the request
* @param {json} [init] - Request extra params
* @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
*/
post(apiName: any, path: any, init: any): Promise<any>;
/**
* Make a PUT request
* @param {String} apiName - The api name of the request
* @param {String} path - The path of the request
* @param {string} apiName - The api name of the request
* @param {string} path - The path of the request
* @param {json} [init] - Request extra params
* @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
*/
put(apiName: any, path: any, init: any): Promise<any>;
/**
* Make a DEL request
* @param {String} apiName - The api name of the request
* @param {String} path - The path of the request
* @param {string} apiName - The api name of the request
* @param {string} path - The path of the request
* @param {json} [init] - Request extra params
* @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
*/
del(apiName: any, path: any, init: any): Promise<any>;
/**
* Make a HEAD request
* @param {String} apiName - The api name of the request
* @param {String} path - The path of the request
* @param {string} apiName - The api name of the request
* @param {string} path - The path of the request
* @param {json} [init] - Request extra params
* @return {Promise} - A promise that resolves to an object with response status and JSON data, if successful.
*/
head(apiName: any, path: any, init: any): Promise<any>;
/**
* Getting endpoint for API
* @param {String} apiName - The name of the api
* @return {String} - The endpoint of the api
* @param {string} apiName - The name of the api
* @return {string} - The endpoint of the api
*/
endpoint(apiName: any): Promise<any>;
/**
Expand Down
Loading

0 comments on commit 7be7470

Please sign in to comment.