gas-mock-globals
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -7,11 +7,14 @@ const Properties = require('../../../src/properties/classes/Properties') | ||
beforeEach(() => { | ||
properties = Object.assign(new Properties(), { | ||
_data: { | ||
key1: 'value1', | ||
key2: 'value2', | ||
key3: 'value3', | ||
} | ||
properties = new Properties() | ||
properties.setProperties({ | ||
key1: 'value1', | ||
key2: 'value2', | ||
key3: 'value3', | ||
}) | ||
}) | ||
afterEach(() => { | ||
properties.deleteAllProperties() | ||
}) | ||
it('Should get the property by key', () => { | ||
@@ -83,2 +86,8 @@ expect(properties.getProperty('key1')).toBe('value1') | ||
}) | ||
test('Writes to one Properties should be visible to others', () => { | ||
const otherProperties = new Properties() | ||
properties.setProperty('foo', 'bar') | ||
expect(otherProperties.getProperty('foo')).toEqual('bar') | ||
}) | ||
}) |
const UrlFetchApp = require('../../src/url-fetch/UrlFetchApp') | ||
const HttpResponse = require('../../src/url-fetch/classes/HttpResponse') | ||
const UrlFetchAppStubConfiguration = require('../../src/url-fetch/classes/UrlFetchAppStubConfiguration') | ||
describe('UrlFetchApp', () => { | ||
it('Should fetch the request', () => { | ||
expect(UrlFetchApp.fetch('request1')).toBeInstanceOf(HttpResponse) | ||
UrlFetchAppStubConfiguration.when('www.google.com').return(new HttpResponse()) | ||
expect(UrlFetchApp.fetch('www.google.com')).toBeInstanceOf(HttpResponse) | ||
}) | ||
it('Should fetch multiple requests', () => { | ||
const responses = UrlFetchApp.fetchAll('request1', 'request2') | ||
UrlFetchAppStubConfiguration.when('www.google.com').return(new HttpResponse()) | ||
UrlFetchAppStubConfiguration.when('www.gmail.com').return(new HttpResponse()) | ||
const responses = UrlFetchApp.fetchAll({ url: 'www.google.com', params: {} }, { url: 'www.gmail.com', params: {} }) | ||
expect(responses).toHaveLength(2) | ||
@@ -21,2 +27,18 @@ expect(responses[0]).toBeInstanceOf(HttpResponse) | ||
}) | ||
it('Should have valid status code & context', () => { | ||
UrlFetchAppStubConfiguration.when('www.google.com').return(new HttpResponse()) | ||
UrlFetchAppStubConfiguration.when('www.gmail.com').return(new HttpResponse().setResponseCode(500)) | ||
UrlFetchAppStubConfiguration.when('www.bitbucket.com').return(new HttpResponse().setContentText('Custom Response')) | ||
expect(UrlFetchApp.fetch('www.google.com').getResponseCode()).toBe(200) | ||
expect(UrlFetchApp.fetch('www.gmail.com').getResponseCode()).toBe(500) | ||
expect(UrlFetchApp.fetch('www.google.com').getContentText()).toBe('') | ||
expect(UrlFetchApp.fetch('www.bitbucket.com').getContentText()).toBe('Custom Response') | ||
}) | ||
beforeEach(() => { | ||
UrlFetchAppStubConfiguration.reset() | ||
}) | ||
}) |
{ | ||
"name": "gas-mock-globals", | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"description": "A library that mocks Google Apps Script Services", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -59,2 +59,23 @@ # Gas Mock Globals | ||
Test UrlFetchApp: | ||
```javascript | ||
function getUser (id) { | ||
return UrlFetchApp.fetch(`https://backend-url/user/${id}`) | ||
} | ||
// test | ||
test('Test API Request to Get User', () => { | ||
const id = 348 | ||
const email = 'user@gmail.com' | ||
UrlFetchAppStubConfiguration.when(`https://backend-url/user/${id}`) | ||
.return(new HttpResponse().setContentText({ id, email })) | ||
const response = getUser(id) | ||
expect(response.getResponseCode()).toBe(200) | ||
expect(response.getContentText()).toBe({ id, email }) | ||
}) | ||
``` | ||
See more examples in [examples](./examples) directory. | ||
@@ -61,0 +82,0 @@ |
@@ -9,2 +9,3 @@ // Global Services | ||
const UrlFetchApp = require('./url-fetch/UrlFetchApp') | ||
const UrlFetchAppStubConfiguration = require('./url-fetch/classes/UrlFetchAppStubConfiguration') | ||
const Session = require('./base/classes/Session') | ||
@@ -22,1 +23,2 @@ const Logger = require('./base/classes/Logger') | ||
global.Logger = Logger | ||
global.UrlFetchAppStubConfiguration = UrlFetchAppStubConfiguration |
@@ -0,8 +1,6 @@ | ||
let data = {} | ||
class Properties { | ||
constructor () { | ||
this._data = {} | ||
} | ||
deleteAllProperties () { | ||
this._data = {} | ||
data = {} | ||
@@ -13,3 +11,3 @@ return this | ||
deleteProperty (key) { | ||
delete this._data[key] | ||
delete data[key] | ||
@@ -20,16 +18,16 @@ return this | ||
getKeys () { | ||
return Object.keys(this._data) | ||
return Object.keys(data) | ||
} | ||
getProperties () { | ||
return this._data | ||
return data | ||
} | ||
getProperty (key) { | ||
return this._data[key] || null | ||
return data[key] || null | ||
} | ||
setProperties (properties = {}, deleteAllOthers = false) { | ||
this._data = Object | ||
.assign(deleteAllOthers ? {} : this._data, properties) | ||
data = Object | ||
.assign(deleteAllOthers ? {} : data, properties) | ||
@@ -40,3 +38,3 @@ return this | ||
setProperty (key, value) { | ||
this._data[key] = value | ||
data[key] = value | ||
@@ -43,0 +41,0 @@ return this |
const Blob = require('../../base/classes/Blob') | ||
class HttpResponse { | ||
constructor () { | ||
this.statusCode = 200 | ||
this.context = '' | ||
} | ||
getAllHeaders () { | ||
@@ -26,10 +31,20 @@ return this.getHeaders() | ||
getContentText () { | ||
return 'dummy-content-text' | ||
return this.context | ||
} | ||
setContentText (context) { | ||
this.context = context | ||
return this | ||
} | ||
getResponseCode () { | ||
return 200 | ||
return this.statusCode | ||
} | ||
setResponseCode (statusCode) { | ||
this.statusCode = statusCode | ||
return this | ||
} | ||
} | ||
module.exports = HttpResponse |
const HttpResponse = require('./classes/HttpResponse') | ||
const UrlFetchAppStubConfiguration = require('./classes/UrlFetchAppStubConfiguration') | ||
class UrlFetchApp { | ||
static fetch (url, params = {}) { | ||
const data = UrlFetchAppStubConfiguration.get(url) | ||
if (data) { | ||
return data.response | ||
} | ||
if (UrlFetchAppStubConfiguration.requests.length > 0) { | ||
// if UrlFetchAppStubConfiguration has stubs, means response not found | ||
return null | ||
} | ||
return new HttpResponse() | ||
@@ -9,3 +18,3 @@ } | ||
static fetchAll (...requests) { | ||
return [...Array(requests.length)].map(() => this.fetch()) | ||
return requests.map(request => this.fetch(request.url, request.params)) | ||
} | ||
@@ -12,0 +21,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
53804
74
1702
92
0