Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'aws-sdk-mock' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
// tslint:disable:no-expression-statement
import * as AWS from 'aws-sdk'
import * as mockAWS from 'aws-sdk-mock'
mockAWS.setSDKInstance(AWS)
mockAWS.mock('KMS', 'decrypt', (params: any, callback: any) => {
const plain = params.CiphertextBlob.toString()
return plain === 'mockError'
? callback(null, {})
: callback(null, { Plaintext: plain })
})
mockAWS.mock(
'KMS',
'encrypt',
({ Plaintext: plaintext }: any, callback: any) => {
return plaintext === 'mockError'
? callback(null, {})
: callback(null, {
CiphertextBlob: Buffer.from(plaintext),
})
},
)
'use strict'
const { assert } = require('chai')
const path = require('path')
const aws = require('aws-sdk-mock')
aws.setSDK(path.resolve('node_modules/aws-sdk'))
const S3Deploy = require('../lib/s3_deploy')
const mockResponse = {
createBucket: { Location: 'createBucket' },
putObject: { ETag: 'putObject' }
}
var s3Deploy = null
/* global describe, it, before, after */
describe('lib/s3_deploy', () => {
before(() => {
aws.mock('S3', 'putObject', (params, callback) => {
callback(null, mockResponse.putObject)
})
aws.mock('S3', 'createBucket', (params, callback) => {
// tslint:disable:no-expression-statement
import * as AWS from 'aws-sdk'
import * as mockAWS from 'aws-sdk-mock'
mockAWS.setSDKInstance(AWS)
mockAWS.mock('KMS', 'decrypt', (params: any, callback: any) => {
const plain = params.CiphertextBlob.toString()
return plain === 'mockError'
? callback(null, {})
: callback(null, { Plaintext: plain })
})
mockAWS.mock(
'KMS',
'encrypt',
({ Plaintext: plaintext }: any, callback: any) => {
return plaintext === 'mockError'
? callback(null, {})
: callback(null, {
const AWS = require('aws-sdk-mock');
// Force aws-sdk-mock to use the same instance of the SDK that the code uses.
// This is necessary due to transpiling. See
// https://github.com/dwyl/aws-sdk-mock#setting-the-aws-sdk-object-explicitly
AWS.setSDKInstance(require('aws-sdk'));
before( (done) => {
AWS_MOCK.setSDKInstance(AWS);
AWS_MOCK.mock('DynamoDB.DocumentClient', 'get', (params, callback) => {
if (params.TableName !== tableName) {
// table name not valid
callback(resourceNotFoundError, null);
} else {
if (params.Key[defaultPartitionKeyName] === defaultPartitionKey) {
callback(null, {Item: defaultGetItemOutput });
} else if (params.Key[customPartitionKeyName] === customPartitionKey) {
callback(null, {Item: customGetItemOutput });
} else {
// item not found
callback(null, {});
}
}
});
AWS_MOCK.mock('DynamoDB.DocumentClient', 'put', (params, callback) => {
const { test } = require('tap')
const AWS = require('aws-sdk-mock')
const AWS_SDK = require('aws-sdk')
AWS.setSDKInstance(AWS_SDK);
const { build } = require('../index')
const clientParams = {
tableName: 'fooTable',
indexName: 'fooIndex',
}
test('loadEvents', async assert => {
var sentParams
const events = [
{ foo: 'bar' },
{ bar: 'baz' },
{ baz: 'bar '},
{ zab: 'foo' },
]
const _mockSetting = () => {
awsMock.mock('CloudWatchEvents', 'putRule', (params, callback) => {
callback(null, {})
})
awsMock.mock('CloudWatchEvents', 'putTargets', (params, callback) => {
callback(null, {})
})
awsMock.mock('CloudWatchLogs', 'createLogGroup', (params, callback) => {
callback(null, {})
})
awsMock.mock('CloudWatchLogs', 'putRetentionPolicy', (params, callback) => {
callback(null, {})
})
awsMock.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => {
callback(null, {})
})
awsMock.mock('S3', 'putObject', (params, callback) => {
callback(null, { test: 'putObject' })
})
Object.keys(lambdaMockSettings).forEach((method) => {
awsMock.mock('Lambda', method, (params, callback) => {
callback(null, lambdaMockSettings[method])
})
})
return require('aws-sdk')
}
beforeEach(() => {
invokeMock = jest.fn();
AWSMock.mock("Lambda", "invoke", invokeMock);
itemListener = jest.fn();
stopListener = jest.fn();
endListener = jest.fn();
startListener = jest.fn();
event = {};
context = {
functionName: "blah",
getRemainingTimeInMillis: () => 100000
};
callback = jest.fn();
});
awsMock.mock('CloudWatchEvents', 'putRule', (params, callback) => {
callback(null, {})
})
awsMock.mock('CloudWatchEvents', 'putTargets', (params, callback) => {
callback(null, {})
})
awsMock.mock('CloudWatchLogs', 'createLogGroup', (params, callback) => {
callback(null, {})
})
awsMock.mock('CloudWatchLogs', 'putRetentionPolicy', (params, callback) => {
callback(null, {})
})
awsMock.mock('S3', 'putBucketNotificationConfiguration', (params, callback) => {
callback(null, {})
})
awsMock.mock('S3', 'putObject', (params, callback) => {
callback(null, { test: 'putObject' })
})
Object.keys(lambdaMockSettings).forEach((method) => {
awsMock.mock('Lambda', method, (params, callback) => {
callback(null, lambdaMockSettings[method])
})
})
return require('aws-sdk')
}
unmockDynamoDB: function () {
'use strict';
AWSMOCK.restore('DynamoDB.DocumentClient');
},