Skip to content

Commit 50a8691

Browse files
committed
Added tests
1 parent 91b4cb6 commit 50a8691

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

.github/workflows/test-and-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
timeout-minutes: 20
1818
strategy:
1919
matrix:
20-
node: [ '6', '7', '8', '10', '12', '14', '16', 'lts' ]
20+
node: [10, 14, 16, lts]
2121
env:
2222
version: ${{ matrix.node }}
2323
DOCKER_LOGIN: ${{ secrets.DOCKER_USERNAME && secrets.DOCKER_AUTH_TOKEN }}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"moment": "^2.19.3",
2424
"sinon": "^2.3.2",
2525
"sinon-chai": "^2.10.0",
26-
"typescript": "^3.7.4"
26+
"typescript": "^4.0.0"
2727
},
2828
"scripts": {
2929
"lint": "if [ `node --version | cut -d'.' -f1 | cut -c 2` -ge \"8\" ]; then eslint . --fix; else echo \"eslint is not available for node < 8.0\"; fi",

packages/client/src/classes/client.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ const TWILIO_BASE_URL = 'https://email.twilio.com/';
1717

1818
// Initialize the allowed regions and their corresponding hosts
1919
const REGION_HOST_MAP = {
20-
eu: 'api.eu.sendgrid.com',
21-
global: 'api.sendgrid.com',
20+
eu: 'https://api.eu.sendgrid.com/',
21+
global: 'https://api.sendgrid.com/',
2222
};
2323
class Client {
2424
constructor() {
@@ -105,6 +105,7 @@ class Client {
105105
} else {
106106
this.setDefaultRequest('baseUrl', REGION_HOST_MAP[region]);
107107
}
108+
return this;
108109
}
109110

110111
createHeaders(data) {

packages/client/src/client.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ declare class Client {
3030
*/
3131
setDefaultRequest<K extends keyof ClientRequest>(key: K | ClientRequest, value ?: ClientRequest[K]): this;
3232

33+
/**
34+
* Sets the data residency as per region provided
35+
*/
36+
setDataResidency(region: string): this;
37+
3338
/**
3439
* Create headers for request
3540
*/

packages/client/src/client.spec.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const nock = require('nock');
33
const sgClient = require('./client');
4-
4+
const testClient = require('./client');
55
const testRequest = (request, statusCode) => {
66
const sgClient = require('./client');
77
sgClient.setApiKey('SG.API Key');
@@ -3094,10 +3094,44 @@ describe('test_whitelabel_links__link_id__subuser_post', () => {
30943094
});
30953095

30963096
describe('setDataResidency', () => {
3097-
const sgClient = require('./client');
3098-
sgClient.setDataResidency('eu');
3097+
const testClient = require('./client');
3098+
let consoleWarnSpy;
30993099

3100-
it('should have host as eu', () => {
3101-
expect(sgClient.baseUrl).to.equal('api.eu.sendgrid.com');
3100+
beforeEach(() => {
3101+
consoleWarnSpy = sinon.spy(console, 'warn');
3102+
});
3103+
afterEach(() => {
3104+
console.warn.restore();
3105+
});
3106+
3107+
it('should send to host EU', () => {
3108+
testClient.setDataResidency('eu');
3109+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
3110+
});
3111+
it('should send to host Global/default', () => {
3112+
testClient.setDataResidency('global');
3113+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
3114+
});
3115+
it('should override the existing set hostname, if data residency setter is called after', () => {
3116+
testClient.setApiKey('SG.1234567890');
3117+
testClient.setDataResidency('eu');
3118+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.eu.sendgrid.com/');
3119+
});
3120+
it('should give a warning if the provided value is not allowed', () => {
3121+
testClient.setDataResidency('');
3122+
expect(consoleWarnSpy.calledOnce).to.equal(true);
3123+
});
3124+
it('should give a warning if the provided value is null', () => {
3125+
testClient.setDataResidency(null);
3126+
expect(consoleWarnSpy.calledOnce).to.equal(true);
3127+
});
3128+
it('should give precedence to the order of execution', () => {
3129+
testClient.setDataResidency('eu');
3130+
testClient.setApiKey('SG.1234567890');
3131+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
3132+
});
3133+
it('should have default value of hostname as https://api.sendgrid.com/', () => {
3134+
expect(testClient.defaultRequest.baseUrl).to.equal('https://api.sendgrid.com/');
31023135
});
31033136
});
3137+

0 commit comments

Comments
 (0)