Last active
September 24, 2020 02:54
-
-
Save agutoli/ac1cb7d8ad1a1837b08c8c2f706cbca2 to your computer and use it in GitHub Desktop.
WIP (NOT DONE): Typescript demo test using dependence injection (DI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// demo class | |
import Foo, { FunctioDIExample } from './injection_demo'; | |
import * as sinon from 'sinon'; | |
describe('Dependence injection', () => { | |
describe('Class', () => { | |
let putObject: any; | |
let instance: Foo; | |
before(() => { | |
class FakeS3 { | |
putObject() { | |
console.log('shouldn\'t show this text'); | |
} | |
} | |
const injections = { S3: FakeS3 }; | |
putObject = sinon.stub(injections.S3.prototype, 'putObject'); | |
instance = new Foo(injections); | |
}); | |
it('calls dependency method putObject', () => { | |
instance.upload(); | |
sinon.assert.calledOnce(putObject); | |
}); | |
}); | |
describe('Function', () => { | |
let putObject: any; | |
let injections: any; | |
before(() => { | |
class FakeS3 { | |
putObject() { | |
console.log('shouldn\'t show this text'); | |
} | |
} | |
injections = { S3: FakeS3 }; | |
putObject = sinon.stub(injections.S3.prototype, 'putObject'); | |
}); | |
it('calls dependency method putObject', () => { | |
FunctioDIExample('arg1', 'arg2', injections); | |
sinon.assert.calledOnce(putObject) | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as AWS from 'aws-sdk'; | |
namespace DI { | |
export const S3 = AWS.S3; | |
}; | |
export default class Foo { | |
declare s3: AWS.S3; | |
constructor(injections: any) { | |
const { S3 } = injections; | |
this.s3 = new S3({}); | |
} | |
upload() { | |
this.s3.putObject(); | |
} | |
} | |
/** | |
* Note: Dependency should be the last parameter | |
*/ | |
export function FunctioDIExample(arg1: string, arg2: string, injections: any) { | |
const { S3 } = injections; | |
const s3 = new S3({}); | |
return s3.putObject(arg1, arg2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment